maven 多模块项目:我可以使用依赖项 jar 吗?

Wis*_*rew 4 java jar maven maven-assembly-plugin multi-module

我有一个maven项目,有一个主项目A和模块B和C。子项目继承自A的pom。

A
|
|----B
|    |----pom.xml
|
|----C
|    |----pom.xml
| 
|----pom.xml
Run Code Online (Sandbox Code Playgroud)

它已经为所有模块构建了 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>
Run Code Online (Sandbox Code Playgroud)

在父 pom 中,但它似乎没有做任何事情:构建成功,但我得到常规的、无依赖项的 jar。

我想避免在每个子 pom 中放入一些东西,因为实际上我有两个以上的模块。我确信有某种方法可以做到这一点,但似乎无法从 Maven 文档中解决。谢谢!

A_D*_*teo 7

这就是我让它发挥作用的方法。
在我配置的聚合器/父 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>
Run Code Online (Sandbox Code Playgroud)

请注意该skip.assembly属性,默认设置为true。这意味着程序集不会在父级上执行,这是有道理的,因为父级不提供任何代码(具有打包pom)。

然后,在每个模块中我简单配置了以下内容:

<properties>
    <skip.assembly>false</skip.assembly>
</properties>
Run Code Online (Sandbox Code Playgroud)

这意味着在每个子模块中,跳过都被禁用,并且程序集按照父模块中的配置执行。而且,通过这样的配置,您还可以轻松地跳过某个模块的组装(如果需要)。

另请注意父级上的程序集配置,我execution在您提供的配置之上添加了一个,以便在调用mvn clean package(或mvn clean install)时自动触发程序集插件。