lenmom

博客园 首页 新随笔 联系 订阅 管理

1. maven-jar-plugin

configuration in pom.xml build->plugins->plugin node.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>cn.dubby.maven.jar.plugin.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

then we can use java -jar  assemble-file.jar to execute the application.

note:

the parameter addClassPath and  classpathPrefix means then package,it would copy the jars in lib folder into the package.

 

so if we have thirdparty dependencies, it's recommondated to use maven-dependency-plugin to deal with the thirdparty dependencies.

 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>compile</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

 

 

2. maven-assembly-plugin

 

configuration:

<!-- Maven Assembly Plugin -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <!-- get all project dependencies -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- MainClass in mainfest make a executable jar -->
        <archive>
            <manifest>
                <mainClass>lenmom.maven.assembly.plugin.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <!-- bind to the packaging phase -->
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

this would generate a single  executable jar file with all dependencies wrapped in it.  and we can use java -jar jar_file_full_path to execute it.

 

3.maven-shade-plugin

configuration:

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

 

posted on 2019-11-25 11:07  老董  阅读(905)  评论(0)    收藏  举报