如何使用Windows命令行构建最简单的多模块Maven项目?

How to build the simplest multi module Maven project using Windows command line?

我有一个多模块项目非常简单

目录结构:

   C:\acme-project\parent
   C:\acme-project\alpha   
   C:\acme-project\beta

逻辑结构:

     parent
    /     \
 alpha <- beta

我无法构建 beta,因为它依赖于 alpha。 Meven 不想在测试版构建过程中构建 alpha

C:\acme-project\beta> mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building beta 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.company:alpha:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.263 s
[INFO] Finished at: 2017-03-28T15:00:34+03:00
[INFO] Final Memory: 16M/220M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project beta: Could not resolve dependencies for project com.company:beta:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: com.company:alpha:jar:1.0-SNAPSHOT: Could not find artifact com.company:alpha:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

我的 POM:

C:\acme-project\parent\parent.pom :

<?xml version='1.0' encoding='UTF-8'?>
<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.company</groupId>
  <artifactId>parent</artifactId>
  <version>1</version>
  <packaging>pom</packaging>

  <modules>
    <module>../alpha</module>
    <module>../beta</module>    
  </modules>
</project>

C:\acme-project\alpha\alpha.com :

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <parent>
    <artifactId>parent</artifactId>
    <groupId>com.company</groupId>
    <version>1</version>
    <relativePath>../parent/pom.xml</relativePath>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>alpha</artifactId>
  <version>1.0-SNAPSHOT</version>

</project>

C:\acme-project\beta\beta.pom :

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.company</groupId>
    <artifactId>parent</artifactId>
    <version>1</version>
    <relativePath>../parent/pom.xml</relativePath>
  </parent>

  <artifactId>beta</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>com.company</groupId>
      <artifactId>alpha</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

我可以在 C:...\parent 中构建 acme-project 运行 mvn package。但如果项目更复杂,parent 可能有 100 个子模块。显然我不想构建 98 个模块(beta 不依赖的其他模块)只是为了让 Maven 开心。

如果您只想构建 beta 所依赖的项目,请转到根聚合器(即在您的情况下包含所有 <modules>; parent 的项目)然后要求 Maven 仅构建 beta 它在当前反应器中的依赖项:

cd parent
mvn package --projects com.company:beta --also-make

仅供参考,这可以缩写为:

mvn package -pl :beta -am

在您的最小示例中,上述命令仍将构建所有内容(因为 beta 取决于 alpha 并隐式地依赖于 parent),但在较大的反应器中,它只会构建构建 beta.

所需的最小子集