简体   繁体   中英

Maven Multi-Module clean cannot find dependency

Problem

mvn package is successful but mvn clean fails due to dependency artifact not being found in a multi-module project.

Details

I am new to Maven and thus far my Google foo has not been successful. I'm attempting to learn it through a pet project and its mostly coming along fine. I am attempting to use Maven to build both a custom plugin and my main App at the same time. Both are currently generic having been build using the mvn archetype:generate for a plugin and a quickstart. Thus far, running mvn package works just fine. I can see my plugin being built, and during the process of building my main App, my plugin is executed. This is exactly what I wanted to see. The resulting jar from the main app works fine and the touch.txt file that the sample plugin generates is also present.

However, when I attempt mvn clean , maven reactor cleans up my plugin first, so when it then cleans up my main App it can't locate the plugin's jar file that it just deleted in the previous step and fails. I can't find where I should specify this order as I thought reactor was supposed to figure this out on its own. Then again, maybe the main App shouldn't be looking for the plugin jar at all. Do I need to exclude it during one of the phases? I've attempted swapping the order of the modules by changing their order in my parent pom.xml file and that didn't seem to make a difference but I also don't know if I need to clear out a cache before the swap would have any effect. I feel like I'm missing something fundamental here in my understanding of Maven. Either way, overly verbose details are below and any help would be greatly appreciated.

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"

Project Directory Structure

Before anything is built:

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

After Build

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

Package

If I manually clean everything up by deleting all of the target folders and then run 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]

Clean

mvn clean exert:

[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 Files

Parent

Here is a copy of my maven-test pom.xml file:

<?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>

Plugin

Here is a copy of my build-args plugin 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>

Main App

Here is a copy of my call-me pom.xml main app:

<?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>

As pointed out by @The5thcolumnmouse, instead of using mvn package , if I use mvn install then the resulting jar files are installed into the.m2 repository. Then when mvn clean is executed, my main app no longer complains about not being able to find my plugin.

M2 Repository

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

Clean

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] ------------------------------------------------------------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM