【问题标题】:Maven Multi-Module clean cannot find dependencyMaven Multi-Module clean 找不到依赖项
【发布时间】:2021-12-29 18:13:24
【问题描述】:

问题

mvn package 成功,但 mvn clean 失败,因为在多模块项目中找不到依赖项。

详情

我是 Maven 新手,到目前为止我的 Google foo 还没有成功。我正试图通过一个宠物项目来学习它,而且它大部分都很好。我正在尝试使用 Maven 同时构建自定义插件和我的主应用程序。两者目前都是通用的,已使用 mvn archetype:generate 构建插件和快速入门。到目前为止,运行mvn package 工作得很好。我可以看到我的插件正在构建,并且在构建我的主应用程序的过程中,我的插件被执行。这正是我想看到的。主应用程序生成的 jar 工作正常,并且示例插件生成的 touch.txt 文件也存在。

但是,当我尝试mvn clean 时,maven reactor 首先清理了我的插件,因此当它清理我的主应用程序时,它无法找到它刚刚在上一步中删除并失败的插件的 jar 文件。我找不到应该在哪里指定这个顺序,因为我认为反应器应该自己解决这个问题。再说一次,也许主应用程序根本不应该寻找插件 jar。我需要在其中一个阶段排除它吗?我尝试通过在我的父 pom.xml 文件中更改模块的顺序来交换模块的顺序,这似乎没有什么区别,但我也不知道是否需要清除在交换之前取出缓存会有任何影响。我觉得我在对 Maven 的理解中缺少一些基本的东西。无论哪种方式,下面都是过于冗长的细节,我们将不胜感激。

mvn --version:

Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 13.0.7, vendor: Private Build, runtime: /usr/lib/jvm/java-13-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.15.5-76051505-generic", arch: "amd64", family: "unix"

项目目录结构

在构建任何东西之前:

maven-test
    ├── build-args
    │   ├── pom.xml
    │   └── src
    │       ├── it
    │       │   ├── settings.xml
    │       │   └── simple-it
    │       │       ├── pom.xml
    │       │       └── verify.groovy
    │       ├── main
    │       │   └── java
    │       │       └── com
    │       │           └── ecorp
    │       │               └── MyMojo.java
    │       └── test
    │           ├── java
    │           │   └── com
    │           │       └── ecorp
    │           │           └── MyMojoTest.java
    │           └── resources
    │               └── project-to-test
    │                   └── pom.xml
    ├── call-me
    │   ├── pom.xml
    │   └── src
    │       ├── main
    │       │   └── java
    │       │       └── com
    │       │           └── ecorp
    │       │               └── App.java
    │       └── test
    │           └── java
    │               └── com
    │                   └── ecorp
    │                       └── AppTest.java
    └── pom.xml

构建后

maven-test
    ├── build-args
    │   ├── pom.xml
    │   ├── src
    │   │   ├── it
    │   │   │   ├── settings.xml
    │   │   │   └── simple-it
    │   │   │       ├── pom.xml
    │   │   │       └── verify.groovy
    │   │   ├── main
    │   │   │   └── java
    │   │   │       └── com
    │   │   │           └── ecorp
    │   │   │               └── MyMojo.java
    │   │   └── test
    │   │       ├── java
    │   │       │   └── com
    │   │       │       └── ecorp
    │   │       │           └── MyMojoTest.java
    │   │       └── resources
    │   │           └── project-to-test
    │   │               └── pom.xml
    │   └── target
    │       ├── build-args-1.0-SNAPSHOT.jar
    │       ├── classes
    │       │   ├── com
    │       │   │   └── ecorp
    │       │   │       ├── HelpMojo.class
    │       │   │       └── MyMojo.class
    │       │   └── META-INF
    │       │       └── maven
    │       │           ├── com.ecorp
    │       │           │   └── build-args
    │       │           │       └── plugin-help.xml
    │       │           └── plugin.xml
    │       ├── generated-sources
    │       │   ├── annotations
    │       │   └── plugin
    │       │       └── com
    │       │           └── ecorp
    │       │               └── HelpMojo.java
    │       ├── generated-test-sources
    │       │   └── test-annotations
    │       ├── maven-archiver
    │       │   └── pom.properties
    │       ├── maven-plugin-help.properties
    │       ├── maven-status
    │       │   └── maven-compiler-plugin
    │       │       ├── compile
    │       │       │   └── default-compile
    │       │       │       ├── createdFiles.lst
    │       │       │       └── inputFiles.lst
    │       │       └── testCompile
    │       │           └── default-testCompile
    │       │               ├── createdFiles.lst
    │       │               └── inputFiles.lst
    │       ├── surefire-reports
    │       │   ├── com.ecorp.MyMojoTest.txt
    │       │   └── TEST-com.ecorp.MyMojoTest.xml
    │       └── test-classes
    │           ├── com
    │           │   └── ecorp
    │           │       ├── MyMojoTest$1.class
    │           │       └── MyMojoTest.class
    │           └── project-to-test
    │               ├── pom.xml
    │               └── target
    │                   └── touch.txt
    ├── call-me
    │   ├── pom.xml
    │   ├── src
    │   │   ├── main
    │   │   │   └── java
    │   │   │       └── com
    │   │   │           └── ecorp
    │   │   │               └── App.java
    │   │   └── test
    │   │       └── java
    │   │           └── com
    │   │               └── ecorp
    │   │                   └── AppTest.java
    │   └── target
    │       ├── call-me-1.0-SNAPSHOT.jar
    │       ├── classes
    │       │   └── com
    │       │       └── ecorp
    │       │           └── App.class
    │       ├── generated-sources
    │       │   └── annotations
    │       ├── generated-test-sources
    │       │   └── test-annotations
    │       ├── maven-archiver
    │       │   └── pom.properties
    │       ├── maven-status
    │       │   └── maven-compiler-plugin
    │       │       ├── compile
    │       │       │   └── default-compile
    │       │       │       ├── createdFiles.lst
    │       │       │       └── inputFiles.lst
    │       │       └── testCompile
    │       │           └── default-testCompile
    │       │               ├── createdFiles.lst
    │       │               └── inputFiles.lst
    │       ├── surefire-reports
    │       │   ├── com.ecorp.AppTest.txt
    │       │   └── TEST-com.ecorp.AppTest.xml
    │       ├── test-classes
    │       │   └── com
    │       │       └── ecorp
    │       │           └── AppTest.class
    │       └── touch.txt
    └── pom.xml

71 directories, 40 files
pizza@hut:~/maven/maven-test$ java -jar call-me/target/call-me-1.0-SNAPSHOT.jar 
Hello World!
pizza@hut:~/maven/maven-test$ cat call-me/target/touch.txt 
touch.txt

如果我通过删除所有目标文件夹手动清理所有内容,然后运行mvn package

[INFO] Reactor Build Order:
[INFO] 
[INFO] maven-test                                                         [pom]
[INFO] build-args Maven Plugin                                   [maven-plugin]
[INFO] call-me                                                            [jar]
[INFO] 
[INFO] ------------------------< com.ecorp:maven-test >------------------------
[INFO] Building maven-test 1.0-SNAPSHOT                                   [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] ------------------------< com.ecorp:build-args >------------------------
[INFO] Building build-args Maven Plugin 1.0-SNAPSHOT                      [2/3]
.........
[INFO] Building call-me 1.0-SNAPSHOT                                      [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- build-args:1.0-SNAPSHOT:touch (default) @ call-me ---
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ call-me ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /somepath/maven-test/call-me/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ call-me ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /somepath/maven-test/call-me/target/classes
.........
[INFO] Reactor Summary for maven-test 1.0-SNAPSHOT:
[INFO] 
[INFO] maven-test ......................................... SUCCESS [  0.005 s]
[INFO] build-args Maven Plugin ............................ SUCCESS [  4.101 s]
[INFO] call-me ............................................ SUCCESS [  0.574 s]

清洁

mvn clean发挥:

[INFO] Reactor Build Order:
[INFO] 
[INFO] maven-test                                                         [pom]
[INFO] build-args Maven Plugin                                   [maven-plugin]
[INFO] call-me                                                            [jar]
[INFO] 
[INFO] ------------------------< com.ecorp:maven-test >------------------------
[INFO] Building maven-test 1.0-SNAPSHOT                                   [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ maven-test ---
[INFO] 
[INFO] ------------------------< com.ecorp:build-args >------------------------
[INFO] Building build-args Maven Plugin 1.0-SNAPSHOT                      [2/3]
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ build-args ---
[INFO] Deleting /somepath/maven-test/build-args/target
[INFO] 
[INFO] -------------------------< com.ecorp:call-me >--------------------------
[INFO] Building call-me 1.0-SNAPSHOT                                      [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for maven-test 1.0-SNAPSHOT:
[INFO] 
[INFO] maven-test ......................................... SUCCESS [  0.159 s]
[INFO] build-args Maven Plugin ............................ SUCCESS [  0.030 s]
[INFO] call-me ............................................ FAILURE [  0.005 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.303 s
[INFO] Finished at: 2021-12-29T12:29:55-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin com.ecorp:build-args:1.0-SNAPSHOT or one of its dependencies could not be resolved: Could not find artifact com.ecorp:build-args:jar:1.0-SNAPSHOT -> [Help 1]

POM 文件

家长

这是我的 maven-test pom.xml 文件的副本:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>com.ecorp</groupId>
  <artifactId>maven-test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>maven-test</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
      
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
      
  <packaging>pom</packaging>
  
  <build>
    <pluginManagement>
      <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <archive>
              <manifest>
                <mainClass>com.ecorp.App</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
    
  <modules>
    <module>call-me</module>
    <module>build-args</module>
  </modules>
  
</project>

插件

这是我的 build-args 插件 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>
  
  <parent>
    <artifactId>maven-test</artifactId>
    <groupId>com.ecorp</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>com.ecorp</groupId>
  <artifactId>build-args</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>maven-plugin</packaging>

  <name>build-args Maven Plugin</name>

  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <prerequisites>
    <maven>${maven.version}</maven>
  </prerequisites>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.version>3.3.9</maven.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>${maven.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-core</artifactId>
      <version>${maven.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-artifact</artifactId>
      <version>${maven.version}</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-compat</artifactId>
      <version>${maven.version}</version>
      <scope>test</scope>
    </dependency> 
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.6.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-testing</groupId>
      <artifactId>maven-plugin-testing-harness</artifactId>
      <version>3.3.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <!-- <goalPrefix>maven-archetype-plugin</goalPrefix> -->
          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
        </configuration>
        <executions>
          <execution>
            <id>mojo-descriptor</id>
            <goals>
              <goal>descriptor</goal>
            </goals>
          </execution>
          <execution>
            <id>help-goal</id>
            <goals>
              <goal>helpmojo</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>run-its</id>
      <build>

        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-invoker-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
              <debug>true</debug>
              <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
              <pomIncludes>
                <pomInclude>*/pom.xml</pomInclude>
              </pomIncludes>
              <postBuildHookScript>verify</postBuildHookScript>
              <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
              <settingsFile>src/it/settings.xml</settingsFile>
              <goals>
                <goal>clean</goal>
                <goal>test-compile</goal>
              </goals>
            </configuration>
            <executions>
              <execution>
                <id>integration-test</id>
                <goals>
                  <goal>install</goal>
                  <goal>integration-test</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

主应用

这是我的 call-me 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>
  <parent>
    <artifactId>maven-test</artifactId>
    <groupId>com.ecorp</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>com.ecorp</groupId>
  <artifactId>call-me</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>call-me</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.ecorp</groupId>
      <artifactId>build-args</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>
  
  <build>
  <plugins>
    <plugin>
      <groupId>com.ecorp</groupId>
      <artifactId>build-args</artifactId>
      <version>1.0-SNAPSHOT</version>
      <executions>
        <execution>
          <goals>
            <goal>touch</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
  </build>
</project>

【问题讨论】:

  • 你可以尝试mvn install而不是mvn package然后运行mvn clean吗?
  • @The5thcolumnmouse 这行得通。我不敢相信我没有想到这一点。不管出于什么原因,我不认为mvn install 也会构建我的主应用程序,但确实如此。谢谢!
  • 不客气!构建工件是不够的,您必须将其安装到本地 .m2 存储库中。

标签: java maven multi-module


【解决方案1】:

正如@The5thcolumnmouse 所指出的,如果我使用mvn install 而不是使用mvn package,那么生成的jar 文件将安装到.m2 存储库中。然后当mvn clean 执行时,我的主应用不再抱怨找不到我的插件。

M2 存储库

pizza@hut:~/maven/maven-test$ tree ~/.m2/repository/com/ecorp/
/somepath/.m2/repository/com/ecorp/
├── build-args
│   ├── 1.0-SNAPSHOT
│   │   ├── build-args-1.0-SNAPSHOT.jar
│   │   ├── build-args-1.0-SNAPSHOT.pom
│   │   ├── maven-metadata-local.xml
│   │   └── _remote.repositories
│   └── maven-metadata-local.xml
├── call-me
│   ├── 1.0-SNAPSHOT
│   │   ├── call-me-1.0-SNAPSHOT.jar
│   │   ├── call-me-1.0-SNAPSHOT.pom
│   │   ├── maven-metadata-local.xml
│   │   └── _remote.repositories
│   └── maven-metadata-local.xml
├── maven-metadata-local.xml
└── maven-test
    ├── 1.0-SNAPSHOT
    │   ├── maven-metadata-local.xml
    │   ├── maven-test-1.0-SNAPSHOT.pom
    │   └── _remote.repositories
    └── maven-metadata-local.xml

清洁

mvn clean

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] maven-test                                                         [pom]
[INFO] build-args Maven Plugin                                   [maven-plugin]
[INFO] call-me                                                            [jar]
[INFO] 
[INFO] ------------------------< com.ecorp:maven-test >------------------------
[INFO] Building maven-test 1.0-SNAPSHOT                                   [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ maven-test ---
[INFO] 
[INFO] ------------------------< com.ecorp:build-args >------------------------
[INFO] Building build-args Maven Plugin 1.0-SNAPSHOT                      [2/3]
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ build-args ---
[INFO] Deleting /somepath/maven-test/build-args/target
[INFO] 
[INFO] -------------------------< com.ecorp:call-me >--------------------------
[INFO] Building call-me 1.0-SNAPSHOT                                      [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ call-me ---
[INFO] Deleting /somepath/maven-test/call-me/target
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for maven-test 1.0-SNAPSHOT:
[INFO] 
[INFO] maven-test ......................................... SUCCESS [  0.159 s]
[INFO] build-args Maven Plugin ............................ SUCCESS [  0.037 s]
[INFO] call-me ............................................ SUCCESS [  0.015 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.322 s
[INFO] Finished at: 2021-12-29T15:11:12-05:00
[INFO] ------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2014-11-20
    相关资源
    最近更新 更多