Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'll try to answer the question as I understand it: </p> <blockquote> <p>How to package a jar containing a spring configuration such as I just need to use <code>java -jar myjar.jar</code>?</p> </blockquote> <p>The code snippet you have in your question simply works. You don't have to parameterise the <code>context.xml</code>. You just need to bundle your code and its dependencies (spring, etc.) in a single jar with a proper manifest entry for the main class in a jar file.</p> <p>I personaly use <a href="http://maven.apache.org" rel="nofollow noreferrer">maven 2</a> and here is a <a href="http://maven.apache.org/pom.html" rel="nofollow noreferrer">pom.xml</a> I would use that do just that:</p> <pre><code>&lt;project&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;com.stackoverflow&lt;/groupId&gt; &lt;artifactId&gt;stackoverflow-autostart-spring-app&lt;/artifactId&gt; &lt;version&gt;0.1&lt;/version&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring&lt;/artifactId&gt; &lt;version&gt;2.5.2&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;archive&gt; &lt;manifest&gt; &lt;mainClass&gt;com.stackoverflow.spring.autostart.Autostart&lt;/mainClass&gt; &lt;/manifest&gt; &lt;/archive&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt; &lt;executions&gt; &lt;execution&gt; &lt;phase&gt;package&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;shade&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;/project&gt; </code></pre> <p>This is assuming some package name for the java code, the source code being in the <code>src/main/java</code> directory and the file <code>context.xml</code> in the <code>src/main/resources</code> directory.</p> <p>So in this <code>pom.xml</code> there are several important points:</p> <ol> <li>the spring dependency (speaks for itself I believe)</li> <li>the configuration of the <a href="http://maven.apache.org/plugins/maven-jar-plugin/" rel="nofollow noreferrer">maven jar plugin</a>, that adds the main class as a manifest entry</li> <li>the <a href="http://maven.apache.org/plugins/maven-shade-plugin/" rel="nofollow noreferrer">maven shade plugin</a>, which is the plugin responsible for gathering all the dependencies/classes and packaging them into one single jar.</li> </ol> <p>The executable jar will be available at <code>target\stackoverflow-autostart-spring-app-0.1.jar</code> when running <code>mvn package</code>.</p> <p><strike>I have this code all working on my box but just realised that I can't attach a zip file here. Anyone know of place I could do so and link here?</strike></p> <p>I created a <a href="http://github.com/sleberrigaud/sof" rel="nofollow noreferrer">git repository at github</a> with the code related to this question if you want to check it out.</p> <p>Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload