如何在maven中构建模块时构建依赖项目

Anu*_*ake 15 dependencies build maven

当子项目由maven构建时,如何构建依赖项目.举个例子,我有2个项目叫A,B.项目B取决于项目A.我想在使用maven构建项目B时构建项目A. 我该怎么办?

Yog*_*h_D 16

看看可以传递给mvn的这些选项:

Options:
 -am,--also-make                        If project list is specified, also
                                        build projects required by the
                                        list
 -amd,--also-make-dependents            If project list is specified, also
                                        build projects that depend on
                                        projects on the list
Run Code Online (Sandbox Code Playgroud)

我相信你的情况你必须使用-amd

编辑:如果您需要通过pom完成.您只需要创建另一个模块C,它只列出子模块A和B.当您构建C时,maven reactor将自动构建两个模块.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>ParentModuleC</artifactId>
  <packaging>pm</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>ParentModuleC</name>
  <dependencies>
  </dependencies>
  <build>
  </build>
  <modules>
    <module>ModuleA</module>
    <module>ModuleB</module>
  </modules>
</project>
Run Code Online (Sandbox Code Playgroud)

在ModuleA和B中,您需要添加以下内容:

<parent>
    <groupId>com.test</groupId>
    <artifactId>ParentModuleC</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
Run Code Online (Sandbox Code Playgroud)

您的目录结构如下所示:

ParentModuleC
    |-pom.xml
    |----------->ModuleA
    |               |->pom.xml
    |----------->ModuleB
    |               |->pom.xml
Run Code Online (Sandbox Code Playgroud)

看一下这个简单的例子:http: //books.sonatype.com/mvnex-book/reference/multimodule.html

  • 答案刚刚被指出是重复问题的答案(http://stackoverflow.com/questions/41849997/make-maven-use-the-current-code-and-not-the-installed-version-when-编译志)。抱歉,这个答案不是很清楚...答案中引用了“amd”,但在依赖于多模块概念的建议解决方案中并未使用。您应该删除对“amd”的引用,或者解释为什么它在这种情况下不适用,否则可能会产生误导。 (2认同)