Maven- 使用Maven构建一个可执行jar

2018/03/03 10:15
阅读数 89

How to Create an Executable JAR with Maven

1.最重要的是使用jar类型,<packaging>jar</packaging>。当然不指定的话,默认Maven使用的就是jar。

2.利用maven-dependency-plugin来手动创建(方法一)

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

①. goal被指定为copy-dependencies,意思是将所有的依赖拷贝到指定的outputDirectory中。例子中是在项目构建文件夹(通常是target文件夹)中创建一个libs文件夹。

②. 使用对①中依赖的连接,创建可执行的、类路径感知的jar。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libs/</classpathPrefix>
                <mainClass>
                    org.baeldung.executable.ExecutableMavenJar
                </mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

manifest配置,追加一个前缀为libs的classpath,提供了main class的信息——main class的完全限定名。

评价

优点:透明的过程使得我们可以在这里指定每一步

缺点:手动做,依赖不在最终的jar中。意味着只有在libs文件夹对于生成的jar可访问并可见时,这个jar才能运行。

2.2 Apache Maven Assembly Plugin(方法二)

Apache Maven Assembly Plugin让用户汇总项目的输出到一个可执行包中,包括它的依赖,模块,站点文档,其他文件。

主要的goal是single,用来创建所有的assemblies。

 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                <manifest>
                    <mainClass>
                        org.baeldung.executable.ExecutableMavenJar
                    </mainClass>
                </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

也需要提供main class的信息。不同的是它会自动拷贝所有需要的依赖到jar文件中。

descriptorRef提供了一个名字,它被用于加到项目名上。

评价

优点:依赖在jar文件中,只有一个文件。

缺点:打包artifact的基本控制,例如,没有类重定位支持。

2.3 Apache Maven Shade Plugin (方法三)

Apache Maven Shade Plugin可以打包artifact到一个uber-jar,它包含运行这个项目的所有需要的依赖。并且,它只是shading——也就是重命名——一些依赖的包。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation=
                      "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
                </transformer>
            </transformers>
        </configuration>
        </execution>
    </executions>
</plugin>

这个配置有三个主要部分:

①. shadedArtifactAttached 标记所有的依赖被打包到jar中。

②. 需要指定transformer implementation;例子中用的是标准实现。

③. 需要指定应用的main class。

评价

优点:jar中的依赖,打包artifact的高级控制,重命名和类重定位。

缺点:复杂的配置(特别是如果你要使用高级特性)

2.4 One Jar Maven Plugin (方法四)

One Jar Maven Plugin提供自定义类加载器,它知道如何从一个archive内的jars中加载类和资源,而不是从文件系统中的jars。

<plugin>
    <groupId>com.jolira</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
                <attachToBuild>true</attachToBuild>
                <filename>
                  ${project.build.finalName}.${project.packaging}
                </filename>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

需要指定main class,将所有依赖附加到build中。

也提供输出文件名。goal是one-jar。注意One Jar是一个商业解决方案,这将使依赖jar在运行时不会扩展到文件系统中。

评价

优点:干净的委托模式,允许类位于One Jar的顶层,支持外部jar,并且可以支持Native库

缺点:自2012年以来没有积极支持

2.5. Spring Boot Maven Plugin(方法五)

Spring Boot Maven Plugin打包可执行jar或war archives,并就地运行应用。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>spring-boot</classifier>
                <mainClass>
                  org.baeldung.executable.ExecutableMavenJar
                </mainClass>
            </configuration>
        </execution>
    </executions>
</plugin>

Spring插件和其他的插件有两个不同,执行的goal是repackage,classifier是spring-boot。

注意,为了使用这个插件,我们并不需要是Spring boot应用。

评价

优点:依赖在一个jar文件中,在每个可访问的位置都能运行它,打包artifact的高级控制,从jar文件中排除依赖,等等。也能打包war文件。

缺点:增加可能不必要的Spring和Spring Boot相关类。

2.6. Web Application with Executable Tomcat

在jar中打包独立的web应用。

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <path>/</path>
                <enableNaming>false</enableNaming>
                <finalName>webapp.jar</finalName>
                <charset>utf-8</charset>
            </configuration>
        </execution>
    </executions>
</plugin>

goal是exec-war-only,配置标签中指定path到你的服务,还有其他的属性,想finalName,charset等等。为了构建jiar,运行man package,它会在target目录中创建webapp.jar。

要运行这个应用,在console中写:java -jar target/webapp.jar,在浏览器中指定localhost:8080/来测试它。

评价

优点:一个文件,易部署和运行

缺点:由于在war中打包了Tomcat嵌入版本,文件尺寸更大。

要注意一servlet的依赖,scope设为provided。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

 

可参考Links:

1. maven-assembly-plugin/usage

2. Pre-defined Descriptor Files

3. Spring Boot Maven Plugin

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部