简体   繁体   English

Maven多模块项目:我可以依赖吗?

[英]maven multimodule project: can I jar-with-dependencies?

I have a maven project, with a main project A and modules B and C. The children inherit from A's pom. 我有一个maven项目,其中有一个主项目A以及模块B和C。孩子继承自A的pom。

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

It already builds jars for all the modules. 它已经为所有模块构建了jar。 Is there a way to include the dependencies in those jars? 有没有办法在这些jar中包含依赖项? Eg so I get B-1.0-with-dependencies.jar and C-1.0-with-dependencies.jar ? 例如,我得到B-1.0-with-dependencies.jarC-1.0-with-dependencies.jar吗? I have tried setting 我尝试设置

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

In the parent pom, but it doesn't seem to do anything: the build succeeds, but I get regular, no-dependency jars. 在父pom中,它似乎什么也没做:构建成功,但是我得到了常规的,无依赖的jar。

I'd like to avoid putting something in each child pom, as in reality I have more than 2 modules. 我想避免在每个子pom中放入内容,因为实际上我有两个以上的模块。 I'm sure there is some way to do this, but can't seem to work it out from the maven docs. 我敢肯定有某种方法可以做到这一点,但似乎无法从Maven文档中解决。 Thanks! 谢谢!

This is how I made it work. 这就是我的工作方式。
In the aggregator/parent pom I configured: 在聚合器/父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>

Note the skip.assembly property, which is set by default to true . 请注意skip.assembly属性,默认情况下将其设置为true That means the assembly would not be executed on the parent, which makes sense since the parent doesn't provide any code (having packaging pom ). 这意味着该程序集将不会在父级上执行,这是有道理的,因为父级不提供任何代码(具有包装pom )。

Then, in each module I configured simply the following: 然后,在每个模块中,我仅配置以下内容:

<properties>
    <skip.assembly>false</skip.assembly>
</properties>

Which means in each submodule the skip is disabled and the assembly is executed as configured in the parent. 这意味着在每个子模块中,将禁用跳过,并按照父级中的配置执行程序集。 Moreover, with such a configuration, you can also easily skip the assembly for a certain module (if required). 此外,通过这种配置,您还可以轻松跳过某个模块的装配(如果需要)。

Please also note the assembly configuration on the parent, I added an execution on top of the configuration you provided so that the assembly plugin is triggered automatically when invoking mvn clean package (or mvn clean install ). 还请注意父级上的程序集配置,我在您提供的配置之上添加了一个execution ,以便在调用mvn clean package (或mvn clean install )时自动触发程序集插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM