Maven按顺序构建所有 [英] Maven build all in order
问题描述
我有一个项目,其中我所有的项目都具有如下定义的父pom:
I have a project where all of my projects have a parent pom defined like this:
<parent>
<groupId>MyProject</groupId>
<artifactId>MyApp</artifactId>
<version>1.0</version>
</parent>
但是,此父pom未在modules元素中指定模块.
However, this parent pom does NOT specify the modules in a modules element.
因此,当我运行mvn install -f parent/pom.xml
命令时,它什么也没做.
So when I run the mvn install -f parent/pom.xml
command it doesn't do anything.
还有其他一些方法可以按顺序构建整个项目 以便构建所有pom吗?
Is there some OTHER way I can build the entire project In Order so that all of the poms are built?
按顺序"是指按依存关系建立".因为几个项目依赖于其他项目,所以我们不能只是按字母顺序构建每个项目.
By "in order" i mean "built in order of dependency". Because several projects depend on others, we can't just build each project alphabetically.
推荐答案
如果您有反应器pom (具有<modules>
元素且<packaging>
定义为pom
的pom)然后将按照依赖关系顺序构建模块.无论在pom中指定模块的顺序如何,都会发生这种排序.
If you have a reactor pom (a pom with the <modules>
element, and <packaging>
defined as pom
) then the modules will be built in dependency order. This ordering happens regardless of the order the modules are specified in the pom.
有几种方法可以解决这个问题:
There's a few ways to approach this:
选项1
您的反应堆pom不必与父pom是相同的pom.因此,您可以:
Your reactor pom doesn't need to be the same pom as your parent pom. So you could have:
project/pom.xml # Reactor pom with <modules> element
project/parent/pom.xml # Parent pom
project/module-a/pom.xml # Some module 'a'
project/module-b/pom.xml # Some module 'b'
在这种情况下,反应堆pom包含:
In this case, the reactor pom contains:
<modules>
<module>parent</module>
<module>module-a</module>
<module>module-b</module>
</modules>
在顶层运行mvn install
会构建您的父pom,并按照依赖关系顺序构建两个模块.
Running mvn install
at the top level will build your parent pom, and the two modules in dependency order.
选项2
将父pom移到一个目录中,并将其用作父pom和反应堆pom,因此您的项目如下所示:
Move your parent pom up a directory, and use it as both a parent pom and a reactor pom, so your project looks like this
project/pom.xml # Parent/Reactor pom with <modules> element
project/module-a/pom.xml # Some module 'a'
project/module-b/pom.xml # Some module 'b'
其中的模块部分看起来像这样
The modules section in it would look like this
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
同样,在顶层运行mvn install
将会构建您的父pom,并且将两个模块按依赖关系顺序构建.
Again, running mvn install
at the top level will build your parent pom, and the two modules in dependency order.
选项3
将父pom保留在其中,并添加一个模块部分:
Leave your parent pom where it is, and add a modules section:
<modules>
<module>../module-a</module>
<module>../module-b</module>
</modules>
在这种情况下,运行mvn install -f parent/pom.xml将构建父pom,并按依赖关系顺序构建两个模块.
In this case, running mvn install -f parent/pom.xml will build the parent pom, and the two modules in dependency order.
结论
我通常使用Option2.这是Maven本身最常用的模式,并且我尽量避免偏离已经被广泛理解和测试的老路".
I normally use Option 2. It's the pattern that's most used in maven itself, and I try to avoid straying too far from the 'beaten path', which is widely understood and tested.
有关更多信息,请参见使用多个模块的指南.
For more information, see the Guide to Working with Multiple Modules.
这篇关于Maven按顺序构建所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!