Maven hits an executable jar package

Source: Internet
Author: User
Tags unpack

        <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>mav                    En-dependency-plugin</artifactid> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> &L                    T;goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputdirectory>${project.build.directory}/lib</outputdire Ctory> <overWriteReleases>false</overWriteReleases> <overw Ritesnapshots>false</overwritesnapshots> <overwriteifnewer>true</overwriteifnewer > </configuration> </execution> </executions> &lt ;/plugin> <Plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>maven-jar-plugi                        n</artifactid> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/< /classpathprefix> <mainClass>theMainClass</mainClass> </manife st> </archive> </configuration> </plugin>

Alternatively use "${project.build.directory}/classes/lib" as OutputDirectory-integrate all jar-files into the main jar Need to add custom classloading code to load the jars.

As above, you can copy the dependent jar package to the lib/directory below target. Then build the jar package.
Note that your jar package just contains your own code and does not put the jar in the lib/directory into your jar.
This time, you can execute Java-jar themainclass to run your program.

If you want to pack all the dependent jars into your jar, become a single jar.
Then you can set the output of Dependency-plugin to "${project.build.directory}/classes/lib"
This makes it possible to make a single jar package.
However, this time, you perform Java-jar themainclass to run, is not able to run the OH. Will report ClassNotFound.
As for the reasons see below:

Are you using the Java-jar parameter to run the packaged jar application and find that the application cannot find the content of the third-party class library set up under Classpath? No matter how to set the Classpath parameter is useless, always will report classnotfound error? Then this post can help you get rid of trouble:)
When using Java-jar Yourjarexe.jar to run a packaged application, you will find out how to set the-classpath parameter application to find the corresponding third-party class, reporting classnotfound errors. This is actually due to the fact that when running with the-jar parameter, the Java VM masks all external classpath, and only uses the internal class of the Yourjarexe.jar itself as the search scope for the class.
via:http://hi.baidu.com/daniel_tu/item/12e5434215c4052310ee1e8e
As for the solution, it is mentioned in the above article. Personally, it's better to write a custom ClassLoader to load the class in the lib/directory inside the jar package.


So far
1). You can use Maven to hit an executable jar package, or choose not to put the dependent jar package together and choose to place it in the lib/directory outside the jar package. This is possible.
2). You can also put the dependency jar into the jar package inside, as a single jar, can be done, but through the Java-jar method will not find the class, you need to customize the ClassLoader.


Introduce the third way of thinking, allow to play a separate jar package, but also can be executed through Java-jar.
The idea is to unpack all the dependent jar packages into class and into our jar package. Pom is as follows:

<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>theMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>



Fourth, consider Maven's other plugins (not limited to dependency, jars), such as: assembly, Shade.
These two tried it out, the executable jar package, the same as the above unpack way, all the classes untied. The POM is attached here, which can be used in each.

<plugin>
<groupId>Org.apache.maven.plugins</groupId>
<artifactId>Maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>Package</phase>
<goals>
<goal>Shade</goal>
</goals>
<CONFIGURATION>
<TRANSFORMERS>
<transformer
implementation= "Org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" >
<mainclass></MAINCLASS>
</ Transformer>
</TRANSFORMERS>
</CONFIGURATION>
</EXECUTION>
</EXECUTIONS>
</PLUGIN>


Or, shade, configure the following:

<plugin>
<artifactId>Maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.hgst.checkalertgroup.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Maven hits an executable jar package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.