Artifact has not been packaged yet - maven-depende

2020-07-02 09:38发布

问题:

When I build a multi module maven project(using mvn clean compile) where one dependency(part of the build reactor) is copied into another using dependency:copy, then maven complains with the below error.

Artifact has not been packaged yet. When used on reactor artifact, copy should be executed after packaging: see MDEP-187 is thrown

This is perfectly fine, Maven can't copy the dependent jar because it has not been packaged yet and the dependency has to be resolved from the local project and not from the repository.

Lets say project A is being copied into project B using the dependency:copy goal.

Now if I import the projects into eclipse,with the lifecycle mapping set in such a way that the maven-jar-plugin is executed on project A(which means that A is packaged), still project B complains with the same error. How can I get rid of this.I can't ignore dependency:copy in the m2e lifecycle as it's a crucial phase in the build.

ProjectA's pom file :

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

    <artifactId>projecta</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.coderplus.tests</groupId>
        <artifactId>projectparent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-jar-plugin</artifactId>
                                        <versionRange>[2.4,)</versionRange>
                                        <goals>
                                            <goal>jar</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Project B's pom file

<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>
    <artifactId>projectb</artifactId>
    <packaging>jar</packaging>


    <parent>
        <groupId>com.coderplus.tests</groupId>
        <artifactId>projectparent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-plugins</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>projecta</artifactId>
                                    <version>${project.version}</version>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}/classes/jars</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[2.8,)</versionRange>
                                        <goals>
                                            <goal>copy-dependecies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute/>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

and the Parent pom wrapping these 2 modules

<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.coderplus.tests</groupId>
    <artifactId>projectparent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <modules>
        <module>projecta</module>
        <module>projectb</module>
    </modules>
</project>

I need a ProjectB's jar to contain ProjectA's jar in the jars directory(This is required by a custom class-loader part of a home-grown framework)

回答1:

With m2eclipse, the artifactItems or dependencies will be resolved to the outputDirectory of the corresponding workspace project if the Resolve dependencies from workspace Projects setting is turned on. This is the root cause for the error in my question.

I ended up forking the existing m2e configurator for this plugin so that it can fetch these dependencies/artifactItems from the respository instead of picking it from the workspace project's outputDirectories .

Though this works with few tests which I had done, it is pretty much a work in progress and in case someone wants to help or contribute, the GitHub URL is

https://github.com/coderplus/m2e-maven-dependency-plugin