11) 生成可执行jar文件 maven-shade-plugin

时间:2022-12-29 23:49:40

搜索 site:maven.apache.org maven-assembly-plugin

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

All goals except assembly:single have been deprecated.

以上不能使用,以下可以。

http://maven.apache.org/plugins/maven-shade-plugin/

examples

Executable JAR

1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.zno</groupId>
    <artifactId>executable</artifactId>
    <version>1.0</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>cn.zno.commandline.Test</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 2. Test.java

package cn.zno.commandline;

import java.lang.management.ManagementFactory;

public class Test {
    public static void main(String[] args) {
        System.out.println("VM arguments:" + ManagementFactory.getRuntimeMXBean().getInputArguments());
        System.out.println("Program arguments:");
        for (String arg : args) {
            System.out.println(arg);
        }
    }
}

打包

mvn install 

we will see an entry like this in the META-INF/MANIFEST.MF file of the resulting JAR:

Manifest-Version: 1.0
Build-Jdk: 1.7.0_15
Built-By: Administrator
Created-By: Apache Maven
Main-Class: cn.zno.commandline.Test
Archiver-Version: Plexus Archiver

测试可执行jar文件

java [-options] -jar jarfile [args...]

E:\e\workspace\executable\target>java -Xms32m -Xmx32m -jar executable-1.0.jar a b
VM arguments:[-Xms32m, -Xmx32m]
Program arguments:
a
b