使用maven构建胖jar

ogq8wdun  于 2021-08-20  发布在  Java
关注(0)|答案(6)|浏览(563)

我有一个代码库,我想作为jar分发。它还依赖于外部jar,我想将其打包到最终的jar中。
我听说这可以通过使用 maven-assembly-plug-in ,但我不明白怎么做。有人能给我举几个例子吗。
现在,我正在使用fat-jar打包最后一个jar。我想用maven实现同样的目标。

fsi0uk1n

fsi0uk1n1#

注意:如果您是spring引导应用程序,请阅读答案的末尾
将以下插件添加到您的 pom.xml 最新版本可在

  1. ...
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-assembly-plugin</artifactId>
  7. <version>CHOOSE LATEST VERSION HERE</version>
  8. <configuration>
  9. <descriptorRefs>
  10. <descriptorRef>jar-with-dependencies</descriptorRef>
  11. </descriptorRefs>
  12. </configuration>
  13. <executions>
  14. <execution>
  15. <id>assemble-all</id>
  16. <phase>package</phase>
  17. <goals>
  18. <goal>single</goal>
  19. </goals>
  20. </execution>
  21. </executions>
  22. </plugin>
  23. </plugins>
  24. </build>
  25. ...

配置此插件后,运行 mvn package 将生成两个jar:一个只包含项目类,另一个fat jar包含后缀为“-jar with dependencies”的所有依赖项。
如果你想要正确的 classpath 在运行时安装,然后还添加以下插件

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <configuration>
  5. <archive>
  6. <manifest>
  7. <addClasspath>true</addClasspath>
  8. <mainClass>fully.qualified.MainClass</mainClass>
  9. </manifest>
  10. </archive>
  11. </configuration>
  12. </plugin>

对于spring启动应用程序,只需使用以下插件(选择合适的版本)

  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. <configuration>
  5. <fork>true</fork>
  6. <mainClass>${start-class}</mainClass>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <goals>
  11. <goal>repackage</goal>
  12. </goals>
  13. </execution>
  14. </executions>
  15. </plugin>
展开查看全部
cgfeq70w

cgfeq70w2#

您可以使用maven shade插件。
在构建中配置shade插件后,请执行以下命令 mvn package 将创建一个jar,并将所有依赖项合并到其中。

siotufzp

siotufzp3#

也许你想要 maven-shade-plugin ,绑定依赖项,最小化未使用的代码并隐藏外部依赖项以避免冲突。

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-shade-plugin</artifactId>
  6. <version>3.1.1</version>
  7. <executions>
  8. <execution>
  9. <phase>package</phase>
  10. <goals>
  11. <goal>shade</goal>
  12. </goals>
  13. <configuration>
  14. <minimizeJar>true</minimizeJar>
  15. <createDependencyReducedPom>true</createDependencyReducedPom>
  16. <dependencyReducedPomLocation>
  17. ${java.io.tmpdir}/dependency-reduced-pom.xml
  18. </dependencyReducedPomLocation>
  19. <relocations>
  20. <relocation>
  21. <pattern>com.acme.coyote</pattern>
  22. <shadedPattern>hidden.coyote</shadedPattern>
  23. </relocation>
  24. </relocations>
  25. </configuration>
  26. </execution>
  27. </executions>
  28. </plugin>
  29. </plugins>
  30. </build>

参考资料:
http://maven.apache.org/plugins/maven-shade-plugin/plugin-info.html
http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html

展开查看全部
dly7yett

dly7yett4#

实际上,添加

  1. <archive>
  2. <manifest>
  3. <addClasspath>true</addClasspath>
  4. <packageName>com.some.pkg</packageName>
  5. <mainClass>com.MainClass</mainClass>
  6. </manifest>
  7. </archive>

对maven jar插件的声明不会为我将主类条目添加到清单文件中。我不得不将它添加到maven程序集插件中,以便在清单中获得它

anauzrmj

anauzrmj5#

您可以使用onejar maven插件进行打包。基本上,它将您的项目及其依赖项组装为一个jar,不仅包括您的项目jar文件,还包括所有外部依赖项作为“jar的jar”,例如。

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>com.jolira</groupId>
  5. <artifactId>onejar-maven-plugin</artifactId>
  6. <version>1.4.4</version>
  7. <executions>
  8. <execution>
  9. <goals>
  10. <goal>one-jar</goal>
  11. </goals>
  12. </execution>
  13. </executions>
  14. </plugin>
  15. </plugins>
  16. </build>

注1:项目主页上提供了配置选项。
注2:出于这样或那样的原因,onejar maven插件项目没有在maven central发布。然而,jolira.com跟踪原始项目并使用groupid将其发布到 com.jolira .

展开查看全部
yqlxgs2m

yqlxgs2m6#

另一种方法是使用maven shade插件构建 uber-jar .

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-shade-plugin</artifactId>
  4. <version> Your Version Here </version>
  5. <configuration>
  6. <!-- put your configurations here -->
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <phase>package</phase>
  11. <goals>
  12. <goal>shade</goal>
  13. </goals>
  14. </execution>
  15. </executions>
  16. </plugin>
展开查看全部

相关问题