lets say i have 3 packages and a jar needs to be created for every package containing only the contents in the current package. My attempt is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>first-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>first-jar</classifier>
<excludes>
<exclude>/maven.task.3/src/main/java/third/ThirdMain.java
</exclude>
<exclude>/maven.task.3/src/main/java/second/SecondMain.java
</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>second-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>second-jar</classifier>
<excludes>
<exclude>/maven.task.3/src/main/java/first/FirstMain.java
</exclude>
<exclude>/maven.task.3/src/main/java/third/ThirdMain.java
</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
And this does indeed create different jars, however the content inside is the same, meaning the exclude clauses don't work. I tried excluding only the class(relative/absolute path) and only the package. Don't ask why i do this its for homework, it does not make a lot of sense!
This is the way i attempt of doing it, if there is any other more efficient way please feel free to share it with me!
EDIT: Modular structure must not be used, it must be one single project.
Thanks in advance
It's already been answered in the comments but here's an example.
A parent pom:
<?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>
<groupId>com.essexboy</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>jar1</module>
<module>jar2</module>
</modules>
</project>
And 2 child/modules poms, only the artifactId is different:
<?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>parent</artifactId>
<groupId>com.essexboy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jar1</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
</build>
</project>
Extra info
I created the parent with the command:
mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE
Then created 2 modules with the command
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE
If you must have a single jar project (no modules and parent) you can do it with the shade-plugin:
<?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>
<groupId>com.essexboy</groupId>
<artifactId>double-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<name>double-jar</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>1</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jar1</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/essexboy/App2*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
<execution>
<id>2</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jar2</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/essexboy/App1*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I have accepted the above answer as right since it's the correct way to do it, but if some crazy kid like me tries to do it the wrong way, here's how:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>first-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>first-jar</classifier>
<includes>
<include>first/FirstMain.class</include>
<include>log4j.properties</include>
<include>pictures/12px-Commons-logo.svg.png</include>
<include>textFiles/first.txt</include>
</includes>
</configuration>
</execution>
</plugin>
Notice to use class instead of java, since it uses the compiled extention! As for the other files, they are resources on the classpath
Related
I have the following pom.xml:
<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>org.example</groupId>
<artifactId>foo-war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.jasig.portlet</groupId>
<artifactId>CalendarPortlet</artifactId>
<version>2.6.2</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.6.RELEASE</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<overlays>
<overlay>
<groupId>org.jasig.portlet</groupId>
<artifactId>CalendarPortlet</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I run mvn package the resulting repackaged war file contains the original war itself under WEB-INF/lib/CalendarPortlet-2.6.2.war.
Why does spring-boot add the war during repackaging and how do I prevent this?
In my real project I want to replace some resources of an already existing war and add the build-info for display by the actuator. But doubling the size of the artifact is not feasible just for that.
EDIT:
The problem also appears when repackaging jars.
Preventing the original artifact being included in the repackaged artifact can be achieved by manually specifying an exlclude for that artifact in the config of the spring-boot-maven plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.6.RELEASE</version>
<configuration>
<excludes>
<exclude>
<groupId>org.jasig.portlet</groupId>
<artifactId>CalendarPortlet</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
But I still don't know why that plugin includes the original artifact in the repackaged one.
I have a maven project and I want to use jacoco for code coverage. Here is the relevant section of my pom
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>pre-my-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-my-test</id>
<phase>post-my-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
So I am able to run my tests just fine and also build the project just fine. Then I run
mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent
I keep getting errors like
ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.7.5.201505241946:
report (post-my-test) on project my-project:
An error has occurred in JaCoCo Test report generation.
Error while creating report:
Error while analyzing class /path/tp/my/project/target/classes/META-INF/
bundled-dependencies/some-third-party-1-jar-with-dependencies.jar#org/slf4j/event/
EventConstants.class. Can't add different class with same name:
org/slf4j/event/EventConstants -> [Help 1]
some-third-party-1-jar-with-dependencies.jar is an external dependency that I have. This is an uber/shaded jar. So I thought that some-third-party-1-jar-with-dependencies.jar must also have org.slf4j, hence the conflict. So I made a change to the pom as
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<excludes>
<exclude>**/org/slf4j/event/EventConstants.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${project.build.directory}/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
but I still get the same error. How can I make sure that jacoco ignores all duplicate dependencies? I also tried
<dependency>
<groupId>some.third.party</groupId>
<artifactId>some-third-party</artifactId>
<version>1</version>
<classifier>jar-with-dependencies</classifier>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
but that did not work. Is it possible to have exclusions from shaded/uber jars?
Moreover, why does jacoco care? Is there any way to fix or ignore these errors? How can I resolve this issue?
Property excludes of report goal specifies which files should be excluded from analysis during generation of report. In case of /path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar#org/slf4j/event/EventConstants.class file is /path/tp/my/project/target/classes/META-INF/bundled-dependencies/some-third-party-1-jar-with-dependencies.jar , and the rest is about class in JAR file.
Therefore as one of examples of correct configuration:
<configuration>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
As a proof having 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
</plugin>
</plugins>
</build>
</project>
src/main/java/Example.java
public class Example {
}
and src/test/java/ExampleTest.java
public class ExampleTest {
#org.junit.Test
public void test() {
}
}
Execution of
mvn clean jacoco:prepare-agent package
mkdir target/classes/META-INF/
cp ~/.m2/repository/org/slf4j/slf4j-api/1.4.2/slf4j-api-1.4.2.jar target/classes
cp ~/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar target/classes/META-INF
mvn jacoco:report
will fail with message
Error while analyzing /private/tmp/j/target/classes/slf4j-api-1.4.2.jar#org/slf4j/helpers/BasicMarker.class. Can't add different class with same name: org/slf4j/helpers/BasicMarker
while same execution with pom.xml containing exclusion of META-INF/**
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.1</version>
<configuration>
<excludes>
<exclude>META-INF/**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
will succeed.
As a side note: semantic of property excludes of prepare-agent goal is different - it specifies class names (regardless of their on-disk location) that should be excluded from instrumentation during execution of tests.
I solved the duplication issue by adding <phase>prepare-package</phase> to the report execution task:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
/** is for traversing into underlying directories and different than * which is a character wild card for names. Avoiding ** can result in undesirable effect.
I have project (TeamCity plugin), written in Scala, packaged by maven and running on TeamCity(which uses Spring).
There is the dependency com.ullink.slack.simpleslackapi in my project. Now I try to update it (from 0.5.2 to 1.2.0) and become an error at runtime.
Error java.lang.NoClassDefFoundError: Could not initialize class
com.ullink.slack.simpleslackapi.impl.SlackWebSocketSessionImpl
The code that caused this exception is pretty simple:
val session = SlackSessionFactory.createWebSocketSlackSession(config.oauthKey)
My build/pom.xml file looks like
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>***</artifactId>
<groupId>***</groupId>
<version>1.0.0</version>
</parent>
<artifactId>build</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>***</groupId>
<artifactId>***-server</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>***</finalName>
<outputDirectory>${project.parent.build.directory}</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>plugin-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I've tried to append this dependency to this file and tried maven-shade-plugin — it does not help.
Could you advice what else can I try?
I am using the maven-shade-plugin to combine two seperate jars into a single combined jar.
One of the jars is signed, while the other is not.
If I use the default configuration of the plugin, it will build a broken jar as the new manifest is missing the digest needed by the signature.
I could "fix" the jar by excluding the signature files, but this will of course result in a completely unsigned jar.
I am looking for a way to create a combined jar with all the signed classes remaining signed and valid. - The jar format allows those kind of jars but I could not find an easy way to tell the shade plugin to do so.
Should I write my own transformer to do a proper merging of manifest files or is there already a suitable option in the shade-plugin I did not find, yet?
Example:
pom.xml (defining two modules "foo" and "bar")
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mygroup</groupId>
<artifactId>myparent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>foo</module>
<module>bar</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
foo/pom.xml: (combine signed bar into unsigned foo)
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>mygroup</groupId>
<artifactId>myparent</artifactId>
<version>1.0</version>
</parent>
<artifactId>foo</artifactId>
<dependencies>
<dependency>
<groupId>mygroup</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>Foo</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
bar/pom.xml: (create signed bar)
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>mygroup</groupId>
<artifactId>myparent</artifactId>
<version>1.0</version>
</parent>
<artifactId>bar</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<keystore>${user.home}/keystore-name</keystore>
<alias>alias-name</alias>
<storepass>123456</storepass>
</configuration>
<executions>
<execution>
<id>sign</id>
<goals>
<goal>sign</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If both modules contain a single hello-word class, I would expect java -jar foo/target/foo-1.0.jar to work and jarsigner -verify foo/target/foo-1.0.jar to tell about the presence of signed classes.
The maven shade plugin does not play nicely with signed jars. I would recommend you take a look at Capsule that is way better to do this kind of job.
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>
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>
The maven-shade-plugin doesn't have a parameter to skip. Often the shade-plugin isn't there just for fun, so you might wonder if you really want to skip this. If you think it is still valid, you have to create a profile with activation like this:
<activation>
<property>
<name>skipShade</name>
<value>!true</value>
</property>
</activation>
This way it is activated by default, unless you add -DskipShade or -DskipShade=true.
Maven 3.6.1 gives you a new approach.
In the superPom you can define a profile for your shading configuration:
<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-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>
<profiles>
<profile>
<id>shade</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<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>
</plugins>
</build>
</profile>
</profiles>
In the user's settings.xml under .m2 you can add a profile of the same id to enable the shade profile configuration of your superPom. This gives you the option to simple toggling the shading from inside your IDE like Intellij IDEA (only tested in Intellij).
<settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<!-- toggle shading from inside Intellij IDEA -->
<profiles>
<profile>
<id>shade</id>
</profile>
</profiles>
<!-- Shade Profile has to be activeProfile to be
able to explicitly disable shading -->
<activeProfiles>
<activeProfile>shade</activeProfile>
</activeProfiles>
</settings>
In the child project you can add a .mvn/maven.config file to your child project template to predefine shading for the project by default. (Requires a CVS template that is used to predefine a company standard.)
The approach using a maven.config is useful if some of your team members do not have the profile in their settings.xml file and you have to take care that shading will be done most of the time.
.mvn/maven.config:
-Pshading
The profile can also be activated by default using jenkinsfile for Jenkins by passing -Pshade. It will overwrite the maven.config setting. To disable use -P!shade
Please note if you are using maven.config file in Intellij (2020.2.2): The .mvn/maven.config file must exists in the subdirectory of the root aggregator pom folder. Building a subproject form the IDE does not respect a .mvn/maven.config file on the subproject level at the moment. Running a mvn command from the command line in the subproject folder will repespect both, the child project .mvn/maven.config and the parent .mvn/maven.config.
Disabling the maven shade plugin worked for me. The build was stock trying to produce the dependency reduced pom file before I disabled the Maven shade plugin.
The skip option was introduced in version 3.3.0 of the shade plugin, so now skipping can be done dynamically using, for example, properties:
<properties>
....
<skipShaded>true</skipShaded>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<skip>${skipShaded}</skip>
...
</configuration>
</execution>
</executions>
</plugin>
In the above the default is to skip, and this can be overridden with passing -DskipShaded=false to mvn.