Maven build executable jar files

Source: Internet
Author: User
  • Part 1: Identify the manifest. MF file in jar

1. Basic configuration information of the manifest. MF file:

1. General attributes:

Manifest-version: defines the version of the manifest file.

Created-by: Specifies the creator of the file, which is generally generated by the jar command line tool.

Signature-version: defines the signature version of the JAR file (for details about the signature of the JAR file later)

Class-path: (the specific role is described below)

2. Application-related attributes:

Main-class: defines the entry class of the JAR file (this class must be an executable class). After this attribute is defined, you can use Java-jar XX. jar to execute this jar file.

3. Extended identity attributes:

Extension-Name: identifies a jar file.

4. Package extension attributes:

Implementation-title: Define the title of the extension implementation

Implementation-version: defines the version of the extension implementation.

Implementation-vendor: defines the organization of the extension implementation

Implementation-vendor-ID: ID of the Organization that defines the extension implementation

Implementation-URL: defines

Specification-title: Define the title of the extension Specification

Specification-version: defines the version of the extension specification.

Specification-vendor: defines the organization that maintains the Specification

Sealed: defines whether to seal the JAR file. The value is true or false.

5. Signature-related attributes and custom attributes (omitted)

II. Introduction to external packages in executable jar files

In the manifest. MF file under the directory META-INF in the jar package (file), there are two attributes closely related to the executable JAR file, namely main-Class and Class-path:

1. The main-class attribute defines the entry to the executable JAR file. Its value must be a Java class with the main method.

2. The class-path attribute defines the storage path and JAR File Name of Third-Party Jar files dependent on executable jar files (separated by spaces)

The benefit of defining this attribute is that when you execute the JAR file, you can directly use the Java-jar XX. Jar command without specifying the-classpath option of the Java command.

  • Part 2: Use Maven plug-ins Maven-jar-plugin and Maven-dependency-plugin to generate executable jar files

1. The role of the maven-jar-plugin plug-in (mainly refers to the role in generating executable jar files)

The configuration in the Pom. xml file is as follows:

 1 <plugin> 2     <groupId>org.apache.maven.plugins</groupId> 3     <artifactId>maven-jar-plugin</artifactId> 4     <configuration> 5         <archive> 6             <manifest> 7                 <addClasspath>true</addClasspath> 8                 <classpathPrefix>lib/</classpathPrefix> 9                 <mainClass>mainclass_full_name</mainClass>10             </manifest>11         </archive>12     </configuration>13 </plugin>        

 

 

Mainclass specifies manifest. the value of the main-class attribute in the MF file (full name of the class with the main method), addclasspath and classpathprefix specify manifest. in the MF file, the Class-path attribute (addclasspath is true, indicating that. add the class-path attribute to the MF file. The value is classpathprefix and all jar files under Lib-Maven dependencies ).

2. Functions of the maven-dependency-plugin plug-in

 1 <plugin> 2     <groupId>org.apache.maven.plugins</groupId> 3     <artifactId>maven-dependency-plugin</artifactId> 4     <executions> 5         <execution> 6             <id>copy</id> 7             <phase>install</phase> 8             <goals> 9                 <goal>copy-dependencies</goal>10             </goals>11             <configuration>12                 <outputDirectory>${project.build.directory}/lib</outputDirectory>13             </configuration>14         </execution>15     </executions>16 </plugin>

The configuration means to copy all jar files under Lib-Maven dependencies to the lib directory under the Project Build directory (this can be changed to other values, note with manifest. in the MF file, the value of class-path is the same)

  • Part 3: reference links to websites
  1. Maven-jar-plugin Official Website: http://maven.apache.org/plugins/maven-jar-plugin/
  2. Manifest. MF Baidu Baike: http://baike.baidu.com/link? Url = required _
  3. Csdn blog: http://blog.csdn.net/e5945/article/details/6215644

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.