maven multimodule项目:我可以依赖jar吗?

时间:2015-12-09 20:59:21

标签: java maven jar maven-assembly-plugin multi-module

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

A
|
|----B
|    |----pom.xml
|
|----C
|    |----pom.xml
| 
|----pom.xml

它已经为所有模块构建了jar。有没有办法在这些罐子中包含依赖项?例如。所以我得到B-1.0-with-dependencies.jarC-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中,它似乎没有做任何事情:构建成功,但我得到了常规的,无依赖的jar。

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

1 个答案:

答案 0 :(得分:4)

这就是我的工作方式。
在我配置的聚合器/父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>

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

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