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

时间:2017-03-28 12:26:58

标签: maven maven-2 maven-3

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

目录结构:

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

逻辑结构:

     parent
    /     \
 alpha <- beta

我无法构建beta,因为它取决于alpha。并且Meven 在beta的构建过程中不想构建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中构建运行mvn package的acme项目。但是如果项目更复杂parent可能有100个子模块。显然我不想构建98个模块(beta不依赖的其他模块)只是为了让Maven满意。

1 个答案:

答案 0 :(得分:4)

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

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

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

mvn package -pl :beta -am

在您的最小示例中,上面的命令仍将构建所有内容(因为beta依赖于alpha并且隐含地依赖于parent),但是在较大的反应器中它将构建最小的构建beta所必需的子集。