Maven 打包的三种方式 和 Springboot 分离jar包

蔚落 2022-06-02 00:08 604阅读 0赞

http://blog.csdn.net/daiyutage/article/details/53739452

aven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现”no main manifest attribute, in xxx.jar”(没有设置Main-Class)、ClassNotFoundException(找不到依赖包)等错误。

要想jar包能直接通过java -jar xxx.jar运行,需要满足:

1、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,这样才能确定程序的入口在哪里;

2、要能加载到依赖包。

使用Maven有以下几种方法可以生成能直接运行的jar包,可以根据需要选择一种合适的方法。

方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包

在pom.xml中配置:

[html] view plain copy

  1. <**build**>
  2. <**plugins**>
  3. <**plugin**>
  4. <**groupId**>org.apache.maven.plugins</**groupId**>
  5. <**artifactId**>maven-jar-plugin</**artifactId**>
  6. <**version**>2.6</**version**>
  7. <**configuration**>
  8. <**archive**>
  9. <**manifest**>
  10. <**addClasspath**>true</**addClasspath**>
  11. <**classpathPrefix**>lib/</**classpathPrefix**>
  12. <**mainClass**>com.xxg.Main</**mainClass**>
  13. </**manifest**>
  14. </**archive**>
  15. </**configuration**>
  16. </**plugin**>
  17. <**plugin**>
  18. <**groupId**>org.apache.maven.plugins</**groupId**>
  19. <**artifactId**>maven-dependency-plugin</**artifactId**>
  20. <**version**>2.10</**version**>
  21. <**executions**>
  22. <**execution**>
  23. <**id**>copy-dependencies</**id**>
  24. <**phase**>package</**phase**>
  25. <**goals**>
  26. <**goal**>copy-dependencies</**goal**>
  27. </**goals**>
  28. <**configuration**>
  29. <**outputDirectory**>${project.build.directory}/lib</**outputDirectory**>
  30. </**configuration**>
  31. </**execution**>
  32. </**executions**>
  33. </**plugin**>
  34. </**plugins**>
  35. </**build**>

maven-jar-plugin用于生成META-INF/MANIFEST.MF文件的部分内容,com.xxg.Main指定MANIFEST.MF中的Main-Class,true会在MANIFEST.MF加上Class-Path项并配置依赖包,lib/指定依赖包所在目录。

例如下面是一个通过maven-jar-plugin插件生成的MANIFEST.MF文件片段:

[plain] view plain copy

  1. Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar
  2. Main-Class: com.xxg.Main

只是生成MANIFEST.MF文件还不够,maven-dependency-plugin插件用于将依赖包拷贝到${project.build.directory}/lib指定的位置,即lib目录下。

配置完成后,通过mvn package指令打包,会在target目录下生成jar包,并将依赖包拷贝到target/lib目录下,目录结构如下:

20150807174107439

指定了Main-Class,有了依赖包,那么就可以直接通过java -jar xxx.jar运行jar包。

这种方式生成jar包有个缺点,就是生成的jar包太多不便于管理,下面两种方式只生成一个jar文件,包含项目本身的代码、资源以及所有的依赖包。

方法二:使用maven-assembly-plugin插件打包

在pom.xml中配置:

[html] view plain copy

  1. <**build**>
  2. <**plugins**>
  3. <**plugin**>
  4. <**groupId**>org.apache.maven.plugins</**groupId**>
  5. <**artifactId**>maven-assembly-plugin</**artifactId**>
  6. <**version**>2.5.5</**version**>
  7. <**configuration**>
  8. <**archive**>
  9. <**manifest**>
  10. <**mainClass**>com.xxg.Main</**mainClass**>
  11. </**manifest**>
  12. </**archive**>
  13. <**descriptorRefs**>
  14. <**descriptorRef**>jar-with-dependencies</**descriptorRef**>
  15. </**descriptorRefs**>
  16. </**configuration**>
  17. </**plugin**>
  18. </**plugins**>
  19. </**build**>

打包方式:

[plain] view plain copy

  1. mvn package assembly:single

打包后会在target目录下生成一个xxx-jar-with-dependencies.jar文件,这个文件不但包含了自己项目中的代码和资源,还包含了所有依赖包的内容。所以可以直接通过java -jar来运行。

此外还可以直接通过mvn package来打包,无需assembly:single,不过需要加上一些配置:

[html] view plain copy

  1. <**build**>
  2. <**plugins**>
  3. <**plugin**>
  4. <**groupId**>org.apache.maven.plugins</**groupId**>
  5. <**artifactId**>maven-assembly-plugin</**artifactId**>
  6. <**version**>2.5.5</**version**>
  7. <**configuration**>
  8. <**archive**>
  9. <**manifest**>
  10. <**mainClass**>com.xxg.Main</**mainClass**>
  11. </**manifest**>
  12. </**archive**>
  13. <**descriptorRefs**>
  14. <**descriptorRef**>jar-with-dependencies</**descriptorRef**>
  15. </**descriptorRefs**>
  16. </**configuration**>
  17. <**executions**>
  18. <**execution**>
  19. <**id**>make-assembly</**id**>
  20. <**phase**>package</**phase**>
  21. <**goals**>
  22. <**goal**>single</**goal**>
  23. </**goals**>
  24. </**execution**>
  25. </**executions**>
  26. </**plugin**>
  27. </**plugins**>
  28. </**build**>

其中packagesingle即表示在执行package打包时,执行assembly:single,所以可以直接使用mvn package打包。

不过,如果项目中用到spring Framework,用这种方式打出来的包运行时会出错,使用下面的方法三可以处理。

方法三:使用maven-shade-plugin插件打包

在pom.xml中配置:

[html] view plain copy

  1. <**build**>
  2. <**plugins**>
  3. <**plugin**>
  4. <**groupId**>org.apache.maven.plugins</**groupId**>
  5. <**artifactId**>maven-shade-plugin</**artifactId**>
  6. <**version**>2.4.1</**version**>
  7. <**executions**>
  8. <**execution**>
  9. <**phase**>package</**phase**>
  10. <**goals**>
  11. <**goal**>shade</**goal**>
  12. </**goals**>
  13. <**configuration**>
  14. <**transformers**>
  15. <**transformer implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>**
  16. <**mainClass**>com.xxg.Main</**mainClass**>
  17. </**transformer**>
  18. </**transformers**>
  19. </**configuration**>
  20. </**execution**>
  21. </**executions**>
  22. </**plugin**>
  23. </**plugins**>
  24. </**build**>

配置完成后,执行mvn package即可打包。在target目录下会生成两个jar包,注意不是original-xxx.jar文件,而是另外一个。和maven-assembly-plugin一样,生成的jar文件包含了所有依赖,所以可以直接运行。

如果项目中用到了Spring Framework,将依赖打到一个jar包中,运行时会出现读取XML schema文件出错。原因是Spring Framework的多个jar包中包含相同的文件spring.handlers和spring.schemas,如果生成一个jar包会互相覆盖。为了避免互相影响,可以使用AppendingTransformer来对文件内容追加合并:

[html] view plain copy

  1. <**build**>
  2. <**plugins**>
  3. <**plugin**>
  4. <**groupId**>org.apache.maven.plugins</**groupId**>
  5. <**artifactId**>maven-shade-plugin</**artifactId**>
  6. <**version**>2.4.1</**version**>
  7. <**executions**>
  8. <**execution**>
  9. <**phase**>package</**phase**>
  10. <**goals**>
  11. <**goal**>shade</**goal**>
  12. </**goals**>
  13. <**configuration**>
  14. <**transformers**>
  15. <**transformer implementation=”org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>**
  16. <**mainClass**>com.xxg.Main</**mainClass**>
  17. </**transformer**>
  18. <**transformer implementation=”org.apache.maven.plugins.shade.resource.AppendingTransformer”>**
  19. <**resource**>META-INF/spring.handlers</**resource**>
  20. </**transformer**>
  21. <**transformer implementation=”org.apache.maven.plugins.shade.resource.AppendingTransformer”>**
  22. <**resource**>META-INF/spring.schemas</**resource**>
  23. </**transformer**>
  24. </**transformers**>
  25. </**configuration**>
  26. </**execution**>
  27. </**executions**>
  28. </**plugin**>
  29. </**plugins**>
  30. </**build**>

SpringBoot 打包一般会自动打成一个统一的jar包,这样觉得比较臃肿,所以习惯上把配置文件和lib分离出来,便于修改替换。

参考文档:https://www.jianshu.com/p/dbdece9062b3

下面给出pom.xml样例

  1. <build>
  2. <plugins>
  3. <!-- <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>-->
  7. <!--打包jar-->
  8. <plugin>
  9. <groupId>org.apache.maven.plugins</groupId>
  10. <artifactId>maven-jar-plugin</artifactId>
  11. <configuration>
  12. <!-- 指定打出的jar的名字,下面打出来的名字就是atc.jar -->
  13. <finalName>atc</finalName>
  14. <archive>
  15. <manifest>
  16. <addClasspath>true</addClasspath>
  17. <!--MANIFEST.MF 中 Class-Path 加入前缀-->
  18. <classpathPrefix>lib/</classpathPrefix>
  19. <!--jar包不包含唯一版本标识-->
  20. <useUniqueVersions>false</useUniqueVersions>
  21. <!--指定入口类-->
  22. <mainClass>com.hdkg.atc.AtcApplication</mainClass>
  23. </manifest>
  24. <manifestEntries>
  25. <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
  26. <Class-Path>./resources/</Class-Path>
  27. </manifestEntries>
  28. </archive>
  29. <excludes>
  30. <exclude>*.xml</exclude>
  31. <exclude>*.properties</exclude>
  32. </excludes>
  33. <outputDirectory>${project.build.directory}/dis</outputDirectory>
  34. </configuration>
  35. </plugin>
  36. <!--拷贝依赖 copy-dependencies-->
  37. <plugin>
  38. <groupId>org.apache.maven.plugins</groupId>
  39. <artifactId>maven-dependency-plugin</artifactId>
  40. <executions>
  41. <execution>
  42. <id>copy-dependencies</id>
  43. <phase>package</phase>
  44. <goals>
  45. <goal>copy-dependencies</goal>
  46. </goals>
  47. <configuration>
  48. <outputDirectory>
  49. ${project.build.directory}/dis/lib/
  50. </outputDirectory>
  51. </configuration>
  52. </execution>
  53. </executions>
  54. </plugin>
  55. <!--拷贝资源文件 copy-resources-->
  56. <plugin>
  57. <artifactId>maven-resources-plugin</artifactId>
  58. <executions>
  59. <execution>
  60. <id>copy-resources</id>
  61. <phase>package</phase>
  62. <goals>
  63. <goal>copy-resources</goal>
  64. </goals>
  65. <configuration>
  66. <resources>
  67. <resource>
  68. <directory>src/main/resources</directory>
  69. </resource>
  70. </resources>
  71. <outputDirectory>${project.build.directory}/dis/resources</outputDirectory>
  72. </configuration>
  73. </execution>
  74. </executions>
  75. </plugin>
  76. </plugins>
  77. </build>

发表评论

表情:
评论列表 (有 0 条评论,604人围观)

还没有评论,来说两句吧...

相关阅读