[From] https://blog.csdn.net/tearsky253/article/details/75948721

问题

在使用“mvn package”命令编译application之后,生成的.jar文件不能直接被“java -jar”命令运行,一般都是因为: 
1. Manifest中没有主清单属性。 
2. 依赖项在.jar文件中不存在。

这两个问题可以通过在pom.xml中增加build plugin来解决。

方法1

使用maven-assembly-plugin来打包fat-jar。

假设我们的application的Main Class是practice.spring.sprice.App,那么我们需要在pom.xml中增加如下一段:

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>practice.spring.sprice.App</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>

这种方法打包出来的.jar中: 
1. practice.spring.sprice.App会被作为主类。 
2. 包含所依赖的所有jar的内容(这些jar包会被解开)。

方法2

使用spring-boot-maven-plugin来打包fat-jar。 
在pom.xml中增加如下一段:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

这种方法打包出来的.jar中: 
1. org.springframework.boot.loader.JarLauncher会被作为主类,它会自动调用应用程序中的main方法。 
2. 包含所依赖的所有的jar包(这些jar包不会被解开)

最新文章

  1. xampp3.2下mysql中文乱码终极解决方案
  2. LVS + KEEPAlived 配置 DIR模式
  3. 例子:Alarm Clock with voice Commands Sample
  4. NeHe OpenGL教程 第四十四课:3D光晕
  5. Android中解析JSON形式的数据
  6. Php AES加密、解密与Java互操作的问题
  7. WISPr1.0
  8. 【CSS3】transform-origin原点旋转
  9. Netty 5.0源码分析之综述
  10. JS消化理解
  11. 分享一个单例模型类Singleton代码
  12. hdu_1019Least Common Multiple(最小公倍数)
  13. android adb shell and monkey 学习记录
  14. 网站开发进阶(十五)JS基础知识充电站
  15. Javascript高级编程学习笔记(77)—— 表单(5)过滤输入
  16. Shell中变量扩展操作
  17. NPOI导入excel文件为DataTable,使用SqlBulkCopy添加到数据库表
  18. 【学习笔记】Spring AOP注解使用总结
  19. JDBC是什么?
  20. 【校招面试 之 剑指offer】第16题 数值的整数次方

热门文章

  1. Mockplus设计大赛获奖选手专访 | Intimate:你的专属密友音乐播放器
  2. nuget get-package id显示不全
  3. LoadRunner出现error问题及解决方法总结
  4. 白盒测试实践--Day0
  5. &#39;for&#39; loop initial declarations are only allo
  6. 单例模式、双检测锁定DCL、volatile(转)
  7. [Lua快速了解一下]Lua的model
  8. _I、_O、_IO的含义
  9. elasticsearch CriteriaQuery查询例子
  10. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)