Three Maven packaging methods) and three maven packaging methods

Source: Internet
Author: User

Three Maven packaging methods (conversion) and maven packaging methods

Maven can use the mvn package command to package the project. If Java-jar xxx is used. when running the jar file, "no main manifest attribute, in xxx. jar "(Main-Class is not set), ClassNotFoundException (dependent package cannot be found), and other errors.

To run a jar package directly through java-jar xxx. jar, you must meet the following requirements:

1. Specify Main-Class in the META-INF/MANIFEST. MF in the jar package to determine where the program entry is;

2. You must be able to load the dependency package.

Maven can be used to generate a jar package that can be directly run in the following ways. You can select an appropriate method as needed.

Method 1: Use the maven-jar-plugin and maven-dependency-plugin plug-ins to package

Configure in pom. xml:

<build>      <plugins>            <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-jar-plugin</artifactId>              <version>2.6</version>              <configuration>                  <archive>                      <manifest>                          <addClasspath>true</addClasspath>                          <classpathPrefix>lib/</classpathPrefix>                          <mainClass>com.xxg.Main</mainClass>                      </manifest>                  </archive>              </configuration>          </plugin>          <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-dependency-plugin</artifactId>              <version>2.10</version>              <executions>                  <execution>                      <id>copy-dependencies</id>                      <phase>package</phase>                      <goals>                          <goal>copy-dependencies</goal>                      </goals>                      <configuration>                          <outputDirectory>${project.build.directory}/lib</outputDirectory>                      </configuration>                  </execution>              </executions>          </plugin>        </plugins>  </build>  

Maven-jar-plugin is used to generate META-INF/MANIFEST. part of the MF file, <mainClass> com. xxg. main </mainClass> specifies MANIFEST. in MF, Main-Class, <addClasspath> true </addClasspath> is stored in MANIFEST. MF adds the Class-Path item and configures the dependent package. <classpathPrefix> lib/</classpathPrefix> specifies the directory where the dependent package is located.

For example, the following is a MANIFEST. MF file segment generated by the maven-jar-plugin plug-in:

Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar  Main-Class: com.xxg.Main

Only generate MANIFEST. the MF file is not enough. The maven-dependency-plugin is used to copy the dependency package to <outputDirectory >$ {project. build. directory}/lib </outputDirectory>.

After the configuration is complete, the mvn package command is used to package the package. The jar package is generated under the target directory, and the dependent package is copied to the target/lib directory. The directory structure is as follows:

If the Main-Class and dependent package are specified, you can run the jar package directly through java-jar xxx. jar.

The disadvantage of generating a jar package in this way is that the generated jar package is too many to facilitate management. The following two methods only generate one jar file, contains the project code, resources, and all dependent packages.

Method 2: Use the maven-assembly-plugin plug-in to package

Configure in pom. xml:

<build>      <plugins>            <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-assembly-plugin</artifactId>              <version>2.5.5</version>              <configuration>                  <archive>                      <manifest>                          <mainClass>com.xxg.Main</mainClass>                      </manifest>                  </archive>                  <descriptorRefs>                      <descriptorRef>jar-with-dependencies</descriptorRef>                  </descriptorRefs>              </configuration>          </plugin>        </plugins>  </build>  

Packaging method:

mvn package assembly:single 

After packaging, a xxx-jar-with-dependencies.jar file is generated under the target directory, which contains not only the code and resources in your project, but also the contents of all the dependent packages. Therefore, you can run it directly through java-jar.

In addition, the mvn package can be directly used for packaging, without the need for assembly: single, but some configuration needs to be added:

<build>      <plugins>            <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-assembly-plugin</artifactId>              <version>2.5.5</version>              <configuration>                  <archive>                      <manifest>                          <mainClass>com.xxg.Main</mainClass>                      </manifest>                  </archive>                  <descriptorRefs>                      <descriptorRef>jar-with-dependencies</descriptorRef>                  </descriptorRefs>              </configuration>              <executions>                  <execution>                      <id>make-assembly</id>                      <phase>package</phase>                      <goals>                          <goal>single</goal>                      </goals>                  </execution>              </executions>          </plugin>        </plugins>  </build>  

<Phase> package </phase> and <goal> single </goal> indicate that assembly: single is executed during package packaging. Therefore, mvn package can be directly used for packaging.

However, if the spring Framework is used in the project, an error occurs when running the package in this way. You can use method 3 below to handle this problem.

Method 3: Use the maven-shade-plugin plug-in to package

Configure in pom. xml:

<build>      <plugins>            <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-shade-plugin</artifactId>              <version>2.4.1</version>              <executions>                  <execution>                      <phase>package</phase>                      <goals>                          <goal>shade</goal>                      </goals>                      <configuration>                          <transformers>                              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                  <mainClass>com.xxg.Main</mainClass>                              </transformer>                          </transformers>                      </configuration>                  </execution>              </executions>          </plugin>        </plugins>  </build>  

After the configuration is complete, run the mvn package to package. Two jar packages are generated under the target directory, note that it is not a original-xxx.jar file, but another one. Like maven-assembly-plugin, the generated jar file contains all dependencies and can be run directly.

If the Spring Framework is used in the project, the dependency is hit into a jar package. An error occurred while reading the XML schema file. The reason is that multiple jar packages of Spring Framework contain the same files spring. handlers and spring. schemas. If a jar package is generated, it will overwrite each other. To avoid mutual influence, AppendingTransformer can be used to append and merge the file content:

<build>      <plugins>            <plugin>              <groupId>org.apache.maven.plugins</groupId>              <artifactId>maven-shade-plugin</artifactId>              <version>2.4.1</version>              <executions>                  <execution>                      <phase>package</phase>                      <goals>                          <goal>shade</goal>                      </goals>                      <configuration>                          <transformers>                              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                  <mainClass>com.xxg.Main</mainClass>                              </transformer>                              <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">                                  <resource>META-INF/spring.handlers</resource>                              </transformer>                              <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">                                  <resource>META-INF/spring.schemas</resource>                              </transformer>                          </transformers>                      </configuration>                  </execution>              </executions>          </plugin>        </plugins>  </build>  

 

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.