java - Building executable jar with maven? -
i trying generate executable jar small home project called "logmanager" using maven, this:
how can create executable jar dependencies using maven?
i added snippet shown there pom.xml, , ran mvn assembly:assembly. generates 2 jar files in logmanager/target: logmanager-0.1.0.jar, , logmanager-0.1.0-jar-with-dependencies.jar. error when double-click on first jar:
could not find main class: com.gorkwobble.logmanager.logmanager. program exit.
a different error when double-click jar-with-dependencies.jar:
failed load main-class manifest attribute from: c:\eclipseprojects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar
i copied , pasted path , classname, , checked spelling in pom. main class launches fine eclipse launch configuration. can me figure out why jar file won't run? also, why there 2 jars begin with? let me know if need more information.
here full pom.xml
, reference:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.gorkwobble</groupid> <artifactid>logmanager</artifactid> <name>logmanager</name> <version>0.1.0</version> <description>systematically renames specified log files on scheduled basis. designed manage mushclient logging , prevent long, continuous log files.</description> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-jar-plugin</artifactid> <version>2.2</version> <!-- nothing here --> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-assembly-plugin</artifactid> <version>2.2-beta-4</version> <configuration> <descriptorrefs> <descriptorref>jar-with-dependencies</descriptorref> </descriptorrefs> <archive> <manifest> <mainclass>com.gorkwobble.logmanager.logmanager</mainclass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> <dependencies> <!-- commons-lang --> <dependency> <groupid>commons-lang</groupid> <artifactid>commons-lang</artifactid> <version>2.4</version> </dependency> <!-- quartz scheduler --> <dependency> <groupid>opensymphony</groupid> <artifactid>quartz</artifactid> <version>1.6.3</version> </dependency> <!-- quartz 1.6.0 depends on commons collections --> <dependency> <groupid>commons-collections</groupid> <artifactid>commons-collections</artifactid> <version>3.1</version> </dependency> <!-- quartz 1.6.0 depends on commons logging --> <dependency> <groupid>commons-logging</groupid> <artifactid>commons-logging</artifactid> <version>1.1</version> </dependency> <!-- quartz 1.6.0 requires jta in non j2ee environments --> <dependency> <groupid>javax.transaction</groupid> <artifactid>jta</artifactid> <version>1.1</version> <scope>runtime</scope> </dependency> <!-- junitx test assertions --> <dependency> <groupid>junit-addons</groupid> <artifactid>junit-addons</artifactid> <version>1.4</version> <scope>test</scope> </dependency> <!-- junit dependency; fixme: make separate pom --> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.1</version> </dependency> </dependencies> <dependencymanagement> </dependencymanagement> </project>
actually, think answer given in question mentioned wrong (update - 20101106: fixed it, this answer refers version preceding edit) , explains, @ least partially, why run troubles.
it generates 2 jar files in logmanager/target: logmanager-0.1.0.jar, , logmanager-0.1.0-jar-with-dependencies.jar.
the first 1 jar of logmanager module generated during package
phase jar:jar
(because module has packaging of type jar
). second 1 assembly generated assembly:assembly
, should contain classes current module , dependencies (if used descriptor jar-with-dependencies
).
i error when double-click on first jar:
could not find main class: com.gorkwobble.logmanager.logmanager. program exit.
if applied suggested configuration of link posted reference, configured jar plugin produce executable artifact, this:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-jar-plugin</artifactid> <configuration> <archive> <manifest> <addclasspath>true</addclasspath> <mainclass>com.gorkwobble.logmanager.logmanager</mainclass> </manifest> </archive> </configuration> </plugin>
so logmanager-0.1.0.jar
indeed executable 1. not want (because doesn't have dependencies) , 2. doesn't contain com.gorkwobble.logmanager.logmanager
(this error saying, check content of jar).
a different error when double-click jar-with-dependencies.jar:
failed load main-class manifest attribute from: c:\eclipseprojects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar
again, if configured assembly plugin suggested, have this:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-assembly-plugin</artifactid> <configuration> <descriptorrefs> <descriptorref>jar-with-dependencies</descriptorref> </descriptorrefs> </configuration> </plugin>
with setup, logmanager-0.1.0-jar-with-dependencies.jar
contains classes current module and dependencies but, according error, meta-inf/manifest.mf
doesn't contain main-class
entry (its not same manifest.mf in logmanager-0.1.0.jar). jar not executable, again not want.
so, suggestion remove configuration
element maven-jar-plugin , configure maven-assembly-plugin this:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-jar-plugin</artifactid> <version>2.2</version> <!-- nothing here --> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-assembly-plugin</artifactid> <version>2.2-beta-4</version> <configuration> <descriptorrefs> <descriptorref>jar-with-dependencies</descriptorref> </descriptorrefs> <archive> <manifest> <mainclass>org.sample.app</mainclass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
of course, replace org.sample.app
class want have executed. little bonus, i've bound assembly:single
package
phase don't have run assembly:assembly
anymore. run mvn install
, assembly produced during standard build.
so, please update pom.xml configuration given above , run mvn clean install
. then, cd target
directory , try again:
java -jar logmanager-0.1.0-jar-with-dependencies.jar
if error, please update question , post content of meta-inf/manifest.mf
file , relevant part of pom.xml
(the plugins configuration parts). please post result of:
java -cp logmanager-0.1.0-jar-with-dependencies.jar com.gorkwobble.logmanager.logmanager
to demonstrate it's working fine on command line (regardless of eclipse saying).
edit: java 6, need configure maven-compiler-plugin. add pom.xml:
<plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin>
Comments
Post a Comment