mvn package recorded

1.mvn package

mvn package arranged in pom.xml, according to project needs, we want to play packet structure

 

2. ways excerpt

2.1 maven-jar-plugin and maven-dependency-plugin plugin package

    <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 part for generating a META-INF / MANIFEST.MF file, <mainClass> com.xxg.Main </ mainClass> specified in the Main-Class MANIFEST.MF, <addClasspath> true </ addClasspath> will add MANIFEST.MF Class-Path key and configuration dependencies, <classpathPrefix> lib / </ classpathPrefix> dependencies specified directory.

maven-dependency-plugin widget for dependencies copied to the <outputDirectory> $ {project.build.directory} / lib </ outputDirectory> specified location, i.e., directory lib. After the configuration, the instruction packing mvn package, the package will be generated in the jar target directory, and the dependencies are copied to the target / lib directory,

Specifies the Main-Class, with dependencies, it can be run directly xxx.jar jar package through java -jar.

In this way generating jar package has a drawback, is that too many packets generated jar is not easy to manage, only two ways to generate a jar file that contains the code for the project itself, resources and all dependencies.

2.2 using maven-assembly-plugin plugin package

    <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>  

Package command: mvn Package Penalty for Assembly: SINGLE 

After the package will generate a xxx-jar-with-dependencies.jar file in the target directory, this document not only contains its own code in the project and resources, but also contains all the dependencies of content. It can be run directly by java -jar.

In addition to package directly through mvn package, without having to assembly: single, but need to add some configuration:

    <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>  

Wherein <phase> package </ phase>, <goal> single </ goal> means that, when executed package packed, performing assembly: single, direct mvn package can be packaged.

However, if the project uses the Spring  Framework, playing this way out to be wrong when the package runs.

2.3 maven-shade-plugin package widget

 

    <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, run mvn package to package. In the target directory will generate two jar package, attention is not original-xxx.jar file, but the other one. And maven-assembly-plugin, like, generated jar file contains all the dependencies, so you can run directly.

If the project is used in the Spring Framework, will depend hit a jar package, will read the XML schema file run-time error. The reason is that a plurality of the Spring Framework jar package contains the same files and spring.handlers spring.schemas, if a generated packet override each jar. In order to avoid the effects of each other, can be used to append AppendingTransformer merge the contents of the file:

    <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>  

 

Guess you like

Origin www.cnblogs.com/zhanghao1799/p/11951222.html