我有一个 maven 项目,有一个主项目 A 和模块 B 和 C。 children 继承自 A 的 pom。
A
|
|----B
| |----pom.xml
|
|----C
| |----pom.xml
|
|----pom.xml
它已经为所有模块构建了 jar。有没有办法在这些 jar 中包含依赖项?例如。所以我得到 B-1.0-with-dependencies.jar
和 C-1.0-with-dependencies.jar
?我试过设置
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
在父 pom 中,但它似乎没有做任何事情:构建成功,但我得到了常规的、无依赖性的 jars。
我想避免在每个子 pom 中放置一些东西,因为实际上我有 2 个以上的模块。我确定有某种方法可以做到这一点,但似乎无法从 Maven 文档中解决。谢谢!
最佳答案
这就是我让它工作的方式。
在我配置的聚合器/父 pom 中:
<properties>
<skip.assembly>true</skip.assembly>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<skipAssembly>${skip.assembly}</skipAssembly>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
请注意 skip.assembly
属性,默认设置为 true
。这意味着程序集不会在父级上执行,这是有道理的,因为父级不提供任何代码(具有包装 pom
)。
然后,在每个模块中,我只配置了以下内容:
<properties>
<skip.assembly>false</skip.assembly>
</properties>
这意味着在每个子模块中,跳过被禁用,程序集按照父模块中的配置执行。此外,通过这样的配置,您还可以轻松地跳过某个模块的程序集(如果需要)。
另请注意父级上的程序集配置,我在您提供的配置之上添加了一个执行
,以便在调用mvn clean package
时自动触发程序集插件>(或 mvn clean install
)。
关于java - Maven 多模块项目 : can I jar-with-dependencies?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34188861/