Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip the maven antrun copy/shade:shade goal dynamically?

I am using maven for configuration of an application consisting of multiple small services. Most of the services developed in java share the same maven configuration, as in the same build lifecycle, some shared resources (like spring AMQP).

So I have organized the shared resources in a SuperPom.

While the shade plugin doesn't really seem to disturb the install process, the antrun plugin of course won't find any of the files it should copy, due to there not being created any jar files by the shade plugin.

As I'd like the configuration of the shade/antrun plugin to be abstracted in the SuperPom, I need to skip the shade/copy goal.

I have tried mvn clean install -Dmaven.shade.skip=true, mvn clean install -Dmaven.copy.skip=true, mvn clean install -Dmaven.shade.shade.skip=true

Here is a small sample for you to play with:

<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>Test</groupId>
    <artifactId>SuperTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <log4j.version>1.2.17</log4j.version>
        <destination>pleasedeleteme</destination>
        <mainpackage>com.uk.cc.it.info.gov.test.xxx</mainpackage>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${mainpackage}.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${groupId}</groupId>
                                    <artifactId>${artifactId}</artifactId>
                                    <version>${version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${destination}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>

</project>
like image 291
minime Avatar asked Dec 25 '22 06:12

minime


1 Answers

Did you try setting the phase of maven-shade-plugin to none in the super-pom and then overriding this in the client poms?

So in the parent pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <id>shade</id>
            <phase>none</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!-- ... -->
            </configuration>
        </execution>
    </executions>
</plugin>

And in the child poms that need it:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <!-- no need to specify version -->
    <executions>
        <execution>
            <id>shade</id>
            <phase>package</phase>
            <!-- no need to specify configuration -->
        </execution>
    </executions>
</plugin>
like image 136
Kristof Neirynck Avatar answered Dec 28 '22 07:12

Kristof Neirynck