I'll try to answer the question as I understand it:
How to package a jar containing a spring configuration such as I just need to use java -jar myjar.jar
?
The code snippet you have in your question simply works. You don't have to parameterise the context.xml
. 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.
I personaly use maven 2 and here is a pom.xml I would use that do just that:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>stackoverflow-autostart-spring-app</artifactId>
<version>0.1</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.stackoverflow.spring.autostart.Autostart</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is assuming some package name for the java code, the source code being in the src/main/java
directory and the file context.xml
in the src/main/resources
directory.
So in this pom.xml
there are several important points:
- the spring dependency (speaks for itself I believe)
- the configuration of the maven jar plugin, that adds the main class as a manifest entry
- the maven shade plugin, which is the plugin responsible for gathering all the dependencies/classes and packaging them into one single jar.
The executable jar will be available at target\stackoverflow-autostart-spring-app-0.1.jar
when running mvn package
.
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?
I created a git repository at github with the code related to this question if you want to check it out.
Hope this helps.