Maven: Package runnable jar Packages (Java application) and dependency processing

Source: Internet
Author: User

IDE environment, you can run Java application directly with the Exec-maven-plugin plug-in, similar to the following:

1 <plugin>2     <groupId>Org.codehaus.mojo</groupId>3     <Artifactid>Exec-maven-plugin</Artifactid>4     <version>1.2.1</version>5     <executions>6         <Execution>7             <Goals>8                 <goal>Exec</goal>9             </Goals>Ten         </Execution> One     </executions> A     <Configuration> -         <executable>Java</executable> -         <arguments> the             <argument>-classpath</argument> -             <Classpath> -             </Classpath> -             <argument>Ctas.importer.reader.app.Program</argument> +         </arguments> -     </Configuration> + </plugin>

18 lines, change to their own Main-class class, and then run with the mvn exec:exec , but when deployed in a production environment, the server usually does not have a MAVEN environment, only with Java-jar Xxx.jar This way to run, here are some processing details:

I. Processing of dependencies

Java application Runtime needs to find the dependent third-party jar, if the lookup classpath failed, will be error, you can first use

MVN Dependency:copy-dependencies-doutputdirectory=target/lib

command to export all dependent jar packages to the Target/lib directory.

Second, the use of Maven-jar-plugin modified meta-inf\manifest. MF manifest file

In the final jar of Java application, with the Unzip tool open, you can see an important manifest file MANIFEST under the Meta-inf directory. MF, this can be specified main-class and classpath, the structure is as follows:

1 manifest-version:1.02built-by:jimmy3 build-jdk:1.7. 0_09 4 class-Path: Lib/dataentity-1.0.jar ... 5 Created-by:apache Maven 3.2.36 Main-class:ctas.importer.reader.app.  Program 7 Archiver-version:plexus archiver

Where line 4th specifies the classpath, where the jar is dependent, and the 6th line represents the entry class of the main function, by default MVN the jar package generated by the clean package, the manifest file does not have these 2 lines, and you need to add the plugin in Pom.xml

1 <plugin>2     <groupId>Org.apache.maven.plugins</groupId>3     <Artifactid>Maven-jar-plugin</Artifactid>4     <Configuration>5         <Archive>6             <Manifest>7                 <MainClass>Ctas.importer.reader.app.Program</MainClass>8                 <Addclasspath>True</Addclasspath>9                 <Classpathprefix>lib/</Classpathprefix>Ten             </Manifest> One         </Archive> A         <classesdirectory> -         </classesdirectory> -     </Configuration> the </plugin>

Line 7th specifies Main-class, and line 9th specifies the relative path of the classpath, so that after mvn the package, Main-class and Class-path are automatically added to the two items in the manifest file.

OK, the deployment of the jar package and the Lib directory, are uploaded to the server, and then test, smooth words Java-jar Xxx.jar can be, if you want to run in the background, the front plus nohup

Maven: Package runnable jar Packages (Java application) and dependency processing

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.