Maven shade plugin configuration not replacing the package - maven

Based on the requirement at Maven dependency incompatible library class, I have tried shade plugin as like below, but went in vain.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.lib:Encoder</artifact>
<includes>
<include>x/y/z/**</include>
</includes>
<excludes>
<exclude>a/b/c/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
My target here is to replace the package with of a.b.c structure with x.y.z of classes.
Did I miss any crucial configurations here?

To replace the package a.b.c with x.y.z on your shaded jar you should add the relocations entry as follow on maven-shade-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>x.y.z</pattern>
<shadedPattern>a.b.c</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>

Related

How to provide different set of properties to the different executions of a plugin?

I am using maven-assembly-plugin to assemble different artifacts as following:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>configuration-staging</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>configuration-production</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
In assembly.xml, I enabled template filtering:
<fileSets>
<fileSet>
<filtered>true</filtered>
This works great. For example, if I enter ${name} in one of the resources to be assembled, this is replaced by the name of the project. I could also define properties in pom.xml, which will be replaced by the plugin.
Now, I would like to have different properties for each execution of maven-assembly-plugin. For example, I would like to introduce a ${url} which holds the URL to be used on the target environment (staging and production in the example above).
Is this possible? How?
Apparently, it is possible to pass different properties for each execution in the maven-assembly-plugin as following:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>configuration-staging</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>staging</finalName>
<filters>
<filter>src/main/assembly/staging.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>configuration-production</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>production</finalName>
<filters>
<filter>src/main/assembly/production.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
Although this does not answer the generic question, it answers the question specifically for maven-assembly-plugin.
More can be found on https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html.
You can try with the properties Maven plugin
https://www.mojohaus.org/properties-maven-plugin/index.html
that allows you read properties from files or URLs.

Running maven-shade-plugin only for package phase

Within my pom.xml I define following plugins - maven-shade, maven-compiler and maven-failsafe:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>shade-main</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>target/assembly/${project.artifactId}.jar</outputFile>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
When I run mvn package it nicely compiles the project and builds a jar with shade-plugin. However when I run mvn verify the shade-plugin should not be run, but it does and it takes unnecessary time before the tests are executed.
How can I prevent the shade plugin to run on mvn verify?
EDIT:
After some comments I want to clarify I still need to do the package phase, I just want to skip the execution of shade plugin.

Attaching only a contents of the folder to the jar

I am using maven-jar-plugin to generate a sources jar.
I have the following folder structure:
folder/foo/baz/A.java
folder/bar/baz/B.java
I want the sources jar to contain the following:
baz/A.java
baz/B.java
I am using the following configuration:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>folder</classesDirectory>
<includes>
<include>foo/**</include>
<include>bar/**</include>
</includes>
<finalName>sources</finalName>
</configuration>
</execution>
</executions>
</plugin>
But this creates the jar like this:
folder/foo/baz/A.java
folder/bar/baz/B.java
How can i modify the code to get my desired structure in the jar?
I could not solve this with the maven-jar-plugin, i had to use maven-resources-plugin, too:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/sources/</outputDirectory>
<resources>
<resource>
<directory>folder/foo/</directory>
</resource>
<resource>
<directory>folder/bar/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.directory}/sources</classesDirectory>
<includes>
<include>**/*</include>
</includes>
<finalName>sources</finalName>
</configuration>
</execution>
</executions>
</plugin>

How can I combine jars using Maven?

The following snippet creates 2 JARS:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-dependency</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dependency</classifier>
<classesDirectory>${project.build.directory}\dependency</classesDirectory>
<includes>
<include>${dependancyInclude}</include>
</includes>
</configuration>
</execution>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>module</classifier>
<classesDirectory>${project.build.directory}\classes</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
I would like to combine these two JARs into one using the assembly plugin, currently I have the following:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<executions>
<!-- Combine all the JARs in the /target folder into one JAR -->
<execution>
<id>make-assembly</id>
<phase>compile</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>true</attach>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
Currently only one of the two JARS in included in the final JAR that is created by the assembly plugin.
If I understand you correctly, you actually want to merge the jar files into one big jar file. This can be achieved using the maven-shade-plugin (instead of the maven-assembly-plugin).
For example:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<configuration>
<!-- Put your configuration here, if you need any extra settings.
(You should be okay with the defaults). -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

unpack dependency and repack classes using maven?

I am trying to unpack a maven artifact A and repack it into a new jar file in the maven project B.
Unpacking class files from artifact A into:
<my.classes.folder>${project.build.directory}/staging</my.classes.folder>
works fine using this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.test</groupId>
<artifactId>mvn-sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${my.classes.folder}</outputDirectory>
<includes>**/*.class,**/*.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
In the same pom I now want to generate an additional jar containing the classes just unpacked:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesdirectory>${my.classes.folder}</classesdirectory>
<classifier>sample</classifier>
</configuration>
</execution>
</executions>
</plugin>
A new jar is created but it does not contain the classes from the:
${my.classes.folder}
its simply a copy of the default project jar. Any ideas?
I have tried to follow this guide:
http://jkrishnaraotech.blogspot.dk/2011/06/unpack-remove-some-classes-and-repack.html
but its not working.
I would suggest you to use the maven-shade-plugin instead.
That will make the unpack-repack in the same invocation.
You could do something like this for example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.test:mvn-sample:1.0.0-SNAPSHOT</artifact>
<includes>
<include>**/*.class</include>
<include>**/*.xml</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In the sample you have <classesdirectory>, the docs have the element as <classesDirectory>. Case sensitivity matters, I think.

Resources