我有一个多模块的maven项目(比如project-xxx)。假设它由5个模块组成:
project-xxx (war)
--> module-1 (jar)
--> module-2 (jar)
--> module-3 (jar)
--> module-4 (jar)
--> module-5 (jar)在构建maven项目时,将生成war文件,并包含这5个模块的jar文件。
现在,出于不同的目的(即部署到分布式缓存,以便我们可以从命令行运行查询),我还想生成一个“`jar”文件,其中包括来自所有模块的java类。我知道,生成多个工件违背了maven的哲学,我在上面阅读了这个博客帖子和一个其他几个 问题。
但是创建这个单独的jar文件将大大简化我的项目中的其他一些事情。生成这个jar文件的最佳方法是什么?
发布于 2015-12-30 19:54:28
我非常赞成每个Maven项目约定中的一个工件。尽管如此,如果您需要一个包含所有模块的所有类的单一工件,那么就创建一个专用的单个项目来完成此任务:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>your-group-id</groupId>
<artifactId>one-jar-to-rule-them-all</artifactId>
<version>your-version</version>
<dependencies>
<dependency>
<groupId>your-group-id</groupId>
<artifactId>module-1</artifactId>
<version>your-version</version>
</dependency>
.
.
.
<dependency>
<groupId>your-group-id</groupId>
<artifactId>module-5</artifactId>
<version>your-version</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<!--
This restricts the jar to classes from your group;
you may or may not want to do this.
-->
<artifactSet>
<includes>
<include>your-group-id</include>
</includes>
</artifactSet>
<createDependencyReducedPom>true</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>这个示例项目依赖于每个模块,然后使用maven-shade-plugin将所有这些模块组合到一个jar工件中。您还可以将其作为父project-xxx的子模块,以便由反应堆构建。这样,您可以同时拥有war和uber jar,但仍然保持标准Maven构建的模块化。
发布于 2015-12-30 17:51:25
我认为您应该考虑引入第一个单独的配置文件,这样profile1就会包含配置,从而产生一个正确的war打包。Profile2可以包含使用maven阴影插件的配置,以便从现有模块中创建一个UBER jar。配置文件是分离不同关注点的一种非常干净和简单的方法。
有关maven配置文件,请参见这里。有关maven-阴影插件,请参见这里。
希望这能有所帮助。
https://stackoverflow.com/questions/34534116
复制相似问题