I found several resources about how to make an executable jar using maven.
I want the the executable jar file to contain all the dependencies jars that the project need .
In general the following steps are used to generate the jar file:
Create the sample eclipse project :
1: $ mvn archetype:generate -DgroupId=my.learn -DartifactId=mavenToExeJar -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2:
3: cd mavenToExeJar
4:
5: mvn eclipse:eclipse
The main class:
1: package my.learn;
2: import org.springframework.context.ApplicationContext;
3: import org.springframework.context.support.FileSystemXmlApplicationContext;
4: public class App
5: {
6: public static void main( String[] args )
7: {
8: ApplicationContext context = new FileSystemXmlApplicationContext(
9: "SpringXMLConfig.xml");
10: MyBean theMyBean = (MyBean) context.getBean("MyBean");
11: System.out.println( "Hello:" + theMyBean.getName() );
12: }
13: }
14:
The bean :
1: package my.learn;
2: public class MyBean {
3: private String mName;
4:
5: public String getName()
6: {
7: return mName;
8: }
9: public void setName(String pValue)
10: {
11: mName = pValue;
12: }
13: }
The spring context file
1: <?xml version="1.0" encoding="windows-1252"?>
2: <beans xmlns="http://www.springframework.org/schema/beans"
3: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4: xmlns:aop="http://www.springframework.org/schema/aop"
5: xmlns:c="http://www.springframework.org/schema/c"
6: xmlns:context="http://www.springframework.org/schema/context"
7: xmlns:flow="http://www.springframework.org/schema/webflow-config"
8: xmlns:jms="http://www.springframework.org/schema/jms"
9: xmlns:lang="http://www.springframework.org/schema/lang"
10: xmlns:p="http://www.springframework.org/schema/p"
11: xmlns:tx="http://www.springframework.org/schema/tx"
12: xmlns:util="http://www.springframework.org/schema/util"
13:
14: xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
15: http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
16: http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
17: http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
18: http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
19: http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
20: http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
21: http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
22: ">
23: <bean id="MyBean" class="my.learn.MyBean">
24: <property name="name" value="Abdala" />
25: </bean>
26: </beans>
The maven xml pom file :
1: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2: xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3: <modelVersion>4.0.0</modelVersion>
4: <groupId>my.learn</groupId>
5: <artifactId>mavenToExeJar</artifactId>
6: <packaging>jar</packaging>
7: <version>1.0-SNAPSHOT</version>
8: <name>mavenToExeJar</name>
9: <url>http://maven.apache.org</url>
10: <properties>
11: <jdk.version>1.6</jdk.version>
12: <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13: </properties>
14: <dependencies>
15: <dependency>
16: <groupId>junit</groupId>
17: <artifactId>junit</artifactId>
18: <version>3.8.1</version>
19: <scope>test</scope>
20: </dependency>
21:
22: <!-- Spring framework -->
23: <dependency>
24: <groupId>org.springframework</groupId>
25: <artifactId>spring-context</artifactId>
26: <version>3.2.3.RELEASE</version>
27: </dependency>
28: </dependencies>
29: <build>
30: <plugins>
31: <plugin>
32: <groupId>org.apache.maven.plugins</groupId>
33: <artifactId>maven-jar-plugin</artifactId>
34: <version>2.3</version>
35: <configuration>
36: <archive>
37: <manifest>
38: <mainClass>my.learn.App</mainClass>
39: <addClasspath>true</addClasspath>
40: </manifest>
41: </archive>
42: </configuration>
43: </plugin>
44: <plugin>
45: <groupId>org.dstovall</groupId>
46: <artifactId>onejar-maven-plugin</artifactId>
47: <version>1.4.4-SNAPSHOT</version>
48: <executions>
49: <execution>
50: <configuration>
51: <!-- Optional
52: <onejarVersion>0.97</onejarVersion>
53: Optional, use only if you need to include native libraries(dll's)
54: <directory>${project.build.directory}/dllextract</directory>
55: <include>test.dll</include>
56: </includes>
57: </fileSet>
58: </binlibs>-->
59: <!-- Optional, default is false -->
60: <attachToBuild>true</attachToBuild>
61: <!-- Optional, default is "onejar" -->
62: <classifier>onejar</classifier>
63: </configuration>
64: <goals>
65: <goal>one-jar</goal>
66: </goals>
67: </execution>
68: </executions>
69: </plugin>
70: </plugins>
71: </build>
72:
73: <pluginRepositories>
74: <pluginRepository>
75: <id>onejar-maven-plugin.googlecode.com</id>
76: <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo-snapshot</url>
77: </pluginRepository>
78: </pluginRepositories>
79: </project>
80:
The folder structure
After compiling the project :
mvn compiler:compile etc
The packing stage :
mvn package
creates the executable jar file. (don't forget to copy the spring context file to the right location )
Execute the jar file
C:\learn\java\maven\executableJar\mavenToExeJar\target>java -jar mavenToExeJar-1
.0-SNAPSHOT.one-jar.jar
and walla :
Jun 08, 2013 10:18:22 AM org.springframework.context.support.AbstractApplication
Context prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationCon
text@16fdac: startup date [Sat Jun 08 10:18:22 IDT 2013]; root of context hierar
chy
Jun 08, 2013 10:18:22 AM org.springframework.beans.factory.xml.XmlBeanDefinition
Reader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\learn\java\maven\executableJar\
mavenToExeJar\target\SpringXMLConfig.xml]
Jun 08, 2013 10:18:22 AM org.springframework.beans.factory.support.DefaultListab
leBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.
DefaultListableBeanFactory@7588c: defining beans [MyBean]; root of factory hiera
rchy
Hello:Abdalla
Resources:
http://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/
http://one-jar.sourceforge.net/