多模块项目在单个模块打包时,maven 反应堆无法找到指定的模块。

新手上路,请多包涵

问题描述

在使用maven进行多模块打包时,提示在反应堆中找不到指定的模块。
使用命令: mvn clean package -pl maven-package-module2 -am

[Error] Could not find the selected project in the reactor: maven-package-module2

项目目录与相关信息

以下是我的项目结构:
image.png

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
maven-package pom.xml:

<?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>
    <groupId>org.example</groupId>
    <artifactId>maven-package</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <modules>
        <module>maven-package-module1</module>
        <module>maven-package-module3</module>
    </modules>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.locales>zh_CN</project.build.locales>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

maven-package-module1 pom.xml:

<?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>maven-package</artifactId>
        <groupId>org.example</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>maven-package-module1</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>maven-package-module2</module>
    </modules>
</project>

maven-package-module2 pom.xml:

<?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>maven-package-module1</artifactId>
        <groupId>org.example</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>maven-package-module2</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>maven-package-module3</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

maven-package-module3 pom.xml:

<?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>maven-package</artifactId>
        <groupId>org.example</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>maven-package-module3</artifactId>
</project>

你期待的结果是什么?实际看到的错误信息又是什么?

maven-package目录下,即父目录下:
命令:mvn clean package
正常打包。如图
image.png
maven-package目录下,也是父目录:
命令:mvn clean package -pl maven-package-module2 -am
提示错误。如图
image.png

以上代码是我在实际的项目中遇到了问题,自己搭建的一个demo,很简单,就是不知为何单独打包时反应堆会找不到指定模块。希望有人能帮我解答一下。我只想单独打包一个特定的模块。

阅读 22.8k
2 个回答
✓ 已被采纳新手上路,请多包涵

多日之后,来自问自答一下。在 pom 文件中,可以定义 artifactId 的名称,但是在使用命令时,使用的不是 artifactId 名称,而是目录结构。所以在 maven-package 目录下使用命令:

mvn clean package -pl maven-package-module1\maven-package-module2 -am

即可成功裁剪反应堆完成打包。
image.png

<modules>

    <module>maven-package-module1/maven-package-module2</module>
</modules>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进