Java Maven 模块 + 构建单个特定模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1114026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven Modules + Building a Single Specific Module
提问by Brian Ferris
I have a multi-module Maven project with a parent project P
and three sub-modules A
, B
, and C
. Both B
and C
are war projects and both depend on A
.
我有一个父项目多模块Maven项目P
和三个子模块A
,B
和C
。这两个B
和C
是战项目,都依赖于A
。
I can type mvn compile
in P
and have all of the sub-modules properly compiled. The problem comes when I want to do operations for specific modules.
我可以输入mvn compile
在P
和拥有所有的子模块正确的编译的。当我想对特定模块进行操作时,问题就来了。
I'd like to be able to package a war for project B
, but when I run the package command from B
's directory, it complains that it can't find the dependencies for A
.
我希望能够为 project 打包一个 war B
,但是当我从B
的目录运行 package 命令时,它抱怨找不到A
.
I understand from this question: Maven and dependent modulesthat perhaps Maven isn't really designed for this type of dependency resolution, but that begs the question of how do I package B
?
我从这个问题中了解到:Maven 和依赖模块,也许 Maven 并不是真正为这种类型的依赖解析而设计的,但这引出了我如何打包的问题B
?
Do I have to run
mvn package
for the entire project hierarchy when I really just wantB
?Do I have to install snapshots of A into my local repository every time I want to package
B
?
mvn package
当我真的想要时,我是否必须运行整个项目层次结构B
?每次要打包时,是否都必须将 A 的快照安装到本地存储库中
B
?
This second scenario isn't much fun when A
is still under active development.
当A
仍在积极开发中时,第二个场景并不是很有趣。
Any best practices here?
这里有什么最佳实践吗?
采纳答案by Pascal Thivent
Any best practices here?
这里有什么最佳实践吗?
Use the Maven advanced reactor options, more specifically:
使用 Maven高级反应器选项,更具体地说:
-pl, --projects
Build specified reactor projects instead of all projects
-am, --also-make
If project list is specified, also build projects required by the list
So just cd
into the parent P directory and run:
所以只需cd
进入父 P 目录并运行:
mvn install -pl B -am
And this will build B and the modules required by B.
这将构建 B 和 B 所需的模块。
Note that you need to use a colon if you are referencing an artifactId
which differs from the directory name:
请注意,如果您要引用artifactId
与目录名称不同的an ,则需要使用冒号:
mvn install -pl :B -am
As described here: https://stackoverflow.com/a/26439938/480894
回答by Rich Seller
If you have previously run mvn installon project B it will have been installed to your local repository, so when you build package A Maven can resolve the dependency. So as long as you install project B each time you change it your builds for project A will be up to date.
如果您之前在项目 B上运行过mvn install,它将已安装到您的本地存储库,因此当您构建包 A 时,Maven 可以解决依赖关系。因此,只要每次更改项目 B 时都安装项目 B,项目 A 的构建将是最新的。
You can define a multi-module project with an aggregator pom to build a set of projects.
你可以定义一个带有聚合器 pom 的多模块项目来构建一组项目。
It's also worthwhile mentioning m2eclipse, it integrates Maven into Eclipse and allows you to (optionally) resolve dependencies from the workspace. So if you are hacking away on multiple projects, the workspace content will be used for compilation. Once you are happy with your changes, run mvn install (on each project in turn, or using an aggregator) to put them in your local repository.
还值得一提的是m2eclipse,它将 Maven 集成到 Eclipse 中,并允许您(可选)从工作区解析依赖项。因此,如果您在多个项目上进行黑客攻击,工作区内容将用于编译。一旦您对更改感到满意,请运行 mvn install(依次在每个项目上,或使用聚合器)将它们放入您的本地存储库中。
回答by victor hugo
Maven absolutely was designed for this type of dependency.
Maven 绝对是为这种类型的依赖而设计的。
mvn package
won't install anything in your local repository it just packages the project and leaves it in the target folder.
mvn package
不会在本地存储库中安装任何东西,它只是打包项目并将其留在目标文件夹中。
Do mvn install
in parent project (A), with this all the sub-modules will be installed in your computer's Maven repository, if there are no changes you just need to compile/package the sub-module (B) and Maven will take the already packaged and installed dependencies just right.
不要mvn install
在父项目(A),这一切的子模块将被安装在您的计算机的Maven仓库,如果没有,你只需要编译/打包子模块(B)和Maven将已经打包的变化并正确安装了依赖项。
You just need to a mvn install
in the parent project if you updated some portion of the code.
mvn install
如果您更新了代码的某些部分,您只需要在父项目中添加一个。
回答by Zac Thompson
You say you "really just want B", but this is false. You want B, but you also want an updated A if there have been any changes to it ("active development").
你说你“真的只想要B”,但这是错误的。您需要 B,但如果 A 有任何更改(“积极开发”),您还需要更新的 A。
So, sometimes you want to work with A, B, and C. For this case you have aggregator project P. For the case where you want to work with A and B (but do notwant C), you should create aggregator project Q.
因此,有时您想要使用 A、B 和 C。对于这种情况,您有聚合器项目 P。对于您想要使用 A 和 B(但不想要 C)的情况,您应该创建聚合器项目 Q .
Edit 2016: The above information was perhaps relevant in 2009. As of 2016, I highly recommend ignoring this in most cases, and simply using the -am
or -pl
command-line flags as described in the accepted answer. If you're using a version of maven from before v2.1, change that first :)
2016 年编辑:上述信息可能与 2009 年相关。截至 2016 年,我强烈建议在大多数情况下忽略这一点,并仅使用已接受答案中描述的-am
或-pl
命令行标志。如果您使用的是 v2.1 之前的 maven 版本,请先更改它:)
回答by deterb
Take a look at my answer Maven and dependent modules.
看看我的回答Maven 和相关模块。
The Maven Reactor pluginis designed to deal with building part of a project.
在Maven的Reactor插件旨在应对建设项目的一部分。
The particular goal you'll want to use it reactor:make
.
您要使用它的特定目标reactor:make
。
回答by Waqas Ahmed
Say Parent pom.xml contains 6 modules and you want to run A, Band F.
假设 Parent pom.xml 包含 6 个模块,并且您想要运行A、B和F。
<modules>
<module>A</module>
<module>B</module>
<module>C</module>
<module>D</module>
<module>E</module>
<module>F</module>
</modules>
1- cd into parent project
1- cd 进入父项目
mvn --projects A,B,F --also-make clean install
OR
或者
mvn -pl A,B,F -am clean install
OR
或者
mvn -pl A,B,F -amd clean install
Note:When you specify a project with the -amoption, Maven will build all of the projects that the specified project depends upon (either directly or indirectly). Maven will examine the list of projects and walk down the dependency tree, finding all of the projects that it needs to build.
注意:当您使用-am选项指定项目时,Maven 将构建指定项目所依赖的所有项目(直接或间接)。Maven 将检查项目列表并沿着依赖关系树向下走,找到它需要构建的所有项目。
While the -amcommand makes all of the projects required by a particular project in a multi-module build, the -amdor --also-make-dependentsoption configures Maven to build a project and any project that depends on that project. When using --also-make-dependents, Maven will examine all of the projects in our reactor to find projects that depend on a particular project. It will automatically build those projects and nothing else.
虽然-am命令在多模块构建中生成特定项目所需的所有项目,但-amd或--also-make-dependents选项将 Maven 配置为构建一个项目和任何依赖于该项目的项目。使用--also-make-dependents 时,Maven 将检查我们的 reactor 中的所有项目以查找依赖于特定项目的项目。它会自动构建这些项目,没有别的。