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

最新文章

  1. 在CentOS或RHEL上安装Nux Dextop仓库
  2. ABP总体介绍
  3. Markdown常用语法
  4. http响应状态码大全
  5. [ACM_几何] The Deadly Olympic Returns!!! (空间相对运动之最短距离)
  6. GitHub在Visual Studio 2015中获得TFS/VSO同等地位
  7. Servlet概述及其生命周期
  8. OpenCV中Delaunay三角网算法例子
  9. Hastiness
  10. java之URL类
  11. [Java] JavaMail 发送 html 格式、带附件的邮件
  12. 编程实现任意长度整数的加法(整数可以长度超出C++中int范围)
  13. Windows笔记目录
  14. 11 个超棒的 jQuery 分步指引插件
  15. jQuery计算文本宽度和input标签根据输入字符动态自适应宽度的实现
  16. vim技巧3
  17. 把myeclipse的自动验证和自动构建都关掉
  18. java内存泄漏与内存溢出
  19. 继承自NSObject的不常用又很有用的函数(1)
  20. (转)在公司的局域网使用git或github 设置代理

热门文章

  1. seo-mask -- 为单页应用创建一个适合蜘蛛爬取的seo网站
  2. 深入剖析tomcat的类加载机制
  3. 551. Student Attendance Record I【easy】
  4. Java中有几种类型的流?以及常见的实现类都有哪些?
  5. python统计订单走势
  6. linux跨主机复制文件
  7. SQL中使用视图的优点和缺点是什么
  8. Yarn源码分析之事件异步分发器AsyncDispatcher
  9. Windows 下tomcat安装及将多个tomcat注册为Windows服务
  10. English 好的报纸