15

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)

coderplus
  • 5,793
  • 5
  • 34
  • 54
  • First it would be helpfull to see your pom files and what is the purpose to use dependency:copy to copy a dependency for what purpose? – khmarbaise Sep 29 '14 at 17:32
  • Why do you use dependency-plugin to copy jar files into a folder `classes/jars` which does not make sense. Why not using them as dependencies instead? If you need projecta in projeb simply define a dependency to it. Furthermore doing on the root level will not work cause the root (parent) will be executed first in reactor build order so it can't copy projecta cause it's not existing at this time. – khmarbaise Sep 30 '14 at 08:35
  • The reactor build order will be projectparent, projecta, projectb. So I don't think that's an issue. I'm copying the jar into projectb.jar as this is used by a custom framework which can load jars from jars.(that's the way it has been designed and I can't alter it). – coderplus Sep 30 '14 at 08:42
  • Than use projectb.jar as a dependency in projecta. If you really need to go this way just add the dependency of projecta in your pom of project b this will make sure that the order is always correct independent of the order of your modules list (it's automactically calculated by Maven). – khmarbaise Sep 30 '14 at 09:06
  • I had done that in my original project, but that doesn't help with the m2eclipse issues :( – coderplus Sep 30 '14 at 09:43
  • Does this work on command line? – khmarbaise Sep 30 '14 at 10:59
  • 1
    yes it does work if I do a `mvn package` – coderplus Sep 30 '14 at 11:04
  • Facing similar problem....let me try if below answer work for me. – Sheel Oct 03 '17 at 17:00

1 Answers1

12

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

coderplus
  • 5,793
  • 5
  • 34
  • 54