Don't copy parent pom when copying child modules

877 views Asked by At

I want to exclude the parent pom (where the following code is located) from being copied when the package:copy goal is executed and I can't find examples or figure it out on my own:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <type>${project.packaging}</type>
                        <classifier>shaded</classifier>
                        <destFileName>${project.name}.${project.packaging}</destFileName>
                        <excludes>*.pom</excludes> <!-- not working -->
                    </artifactItem>
                </artifactItems>
                <outputDirectory>${rootdir}/target/modules</outputDirectory>
                <silent>true</silent>
                <overWriteReleases>true</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
        </execution>
    </executions>
</plugin>

Regardless of the <excludes> setting inside artifactItem, it still includes my parent projects NiftyParent.pom. I want to exclude the file from being copies to the ${rootdir}/target/modules directory.

Incase anyone asks, the ${rootdir} property just points to the parent project directory without hardcoding relative/absolute paths (for sake of argument its ~/Workspace/Nifty.

1

There are 1 answers

2
A_Di-Matteo On BEST ANSWER

You could use the skip configuration element of the plugin together with a Maven property defined in the parent and its modules in order to skip the execution.

In your parent pom you can configure it as following:

<properties>
    <skip-dependency-copy>true</skip-dependency-copy>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>${project.groupId}</groupId>
                                <artifactId>${project.artifactId}</artifactId>
                                <version>${project.version}</version>
                                <type>${project.packaging}</type>
                                <classifier>shaded</classifier>
                                <destFileName>${project.name}.${project.packaging}</destFileName>
                            </artifactItem>
                        </artifactItems>
                        <outputDirectory>${rootdir}/target/modules</outputDirectory>
                        <silent>false</silent>
                        <overWriteReleases>true</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                        <skip>${skip-dependency-copy}</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note the additional property and the skip element configuration.

Then, in each module you could configure simply the following:

<properties>
    <skip-dependency-copy>false</skip-dependency-copy>
</properties>

As such, we are actually switching it on/off based on parent/module location. Additional advantage of this approach is that you would also be able to skip it for some modules (if required) and be able to switch it on/off for all (in your case off) from command line (overriding defined properties values) via for instance:

mvn package -Dskip-dependency-copy=true

This approach is not using the plugin configuration, but it's often used whenever a skip is required in many other plugins and cases.