Maven Shade Plugin does not include needed classes from dependancy - java

In a project, I use as a dependancy an artifact which is in the same groupId than this project.
The dependancy is well imported and used in my IDE, but when I compile the jar, no classes from the dependancy are copied inside the final jar. So I get exceptions at runtime...
Why ???
The dependancy POM head :
<groupId>com.example.things</groupId>
<artifactId>project-foo</artifactId>
My project POM head :
<groupId>com.example.things</groupId>
<artifactId>project-bar</artifactId>
My project POM section to declare dependancy :
<dependency>
<groupId>com.example.things</groupId>
<artifactId>example-foo</artifactId>
<version>1.2.345-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
My project POM Maven Shade Section :
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>com.example.things:project-foo</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
Thanks for helping, it seems I do something wrong...

Related

How to Include and Exclude Some of Files From Different Artifacts Using Maven Shaded Plugin

I am trying to create a uber jar using Maven Shaded plugin to do that i have created a Maven profile.I have two dependency projects related to current project.
Project-A Depends on Project-B and Project-C. All of them share same group id. we are using monolith repo structure.Below are projects .pom details
Project-A.pom
<dependency>
<groupId>com.sample.project</groupId>
<artifactId>Project-B</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sample.project</groupId>
<artifactId>Project-C</artifactId>
<version>1.6.0</version>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/LICENSE</exclude>
<exclude>LICENSE</exclude>
<exclude>*:*</exclude> // exclude all artifacts
<exclude>com.sample.manual:*</exclude> // exclude specific artifact
</excludes>
<includes>
<include>com.sample.project:*</include> //include only com.sample.project related artifacts
</includes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
My requirement is that i want to exclude all .xml files from dependent projects and only include Xml files from current project.To do that i have used following filters but i can not achieve it.
<exclude>com.sample.project:*.xml</exclude>
<include>com.sample.project:{project.artifactId}:*.xml</include>
Please let me know how to do it.

Maven shade plugin does not exclude the manifest signature files

I am using the maven shade plugin to generate a consolidate jar for my project. The jar is generated as expected and when i try to use the jar and run it, i get a
java.lang.SecurityException: Invalid signature file digest for
Manifest main attributes error.
I googled the above error message and many people have suggested to exclude the manifest signatures from the META-INF directory. Thus i have included the step to exclude these files from the directory [i see two files by the name JARSIGN_.RSA and JARSIGN_.SF], but for some strange reason, maven shade plugin is unable to exclude these files from the META-INF directory. Could anyone explain me what i might be doing wrong? My pom.xml is below and the command that i am using to generate the jar is:
mvn clean package shade:shade
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>com.abc.xyz</groupId>
<artifactId>myjar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<properties>
<!-- A few custom properties -->
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<!-- Other The dependencies are here -->
</dependencies>
<repositories>
<!-- Repository Information -->
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<!-- The below statement is not executed by shade plugin -->
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>com.google.guava:guava</include>
<include>com.google.code.gson:gson</include>
</includes>
</artifactSet>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.abc.xyz.HelloWorld</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
With shade plugin 3.2.1 the following works for me.
<!-- language: lang-xml -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
On the plugin's doc page (https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html) the whole <configuration> block is shown inside the <execution> tag. This does not work. The <configuration> block should be outside the <executions> tag as shown above.
Maybe the configuration syntax for the plugin has changed, but this worked for me in the past with version 1.5 of the shader plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>META-INF/JARSIGN_.SF</resource>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
I have not tried to use wildcards. But looking at the documentation the following should exclude all .SF files:
<resource>.SF</resource>
See this thread for another example.
I had a similar issue where the Shade plugin apparently did not exclude files from META-INF directory no matter what settings I tried. I was checking it using the following bash commands:
mvn clean install
7za x target/built-jar-6.4.0.jar -aoa -o/tmp/unpacked/
ls /tmp/unpacked/META-INF/
The problem was actually not in the shade plugin itself, but in the way I was unpacking the JAR - the unpacking command overwrites files, but it keeps the old files in place, making me think there was something wrong with my settings.

Maven Get Specific Classes

Is there a way that I can get maven to only include specific .class files when importing dependencies into uber jar (shade). I'm looking for a way to get files that contain "Client" in their name to be pulled out of the dependency jars and added to the final jar. Any help would be wonderful.
You should be able to use the maven-dependency-plugin like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId><!--dependency groupId--></groupId>
<artifactId><!--dependency artifactId--></artifactId>
<version><!--depedency version--></version>
<includes>**/*Client*.java</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
If you are using the Maven Shade Plugin, you can a filter, which will allow you to filter which artifacts get shaded, but as well as which classes to exclude or include.
Here's the example they provide:
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
</excludes>
</filter>
</filters>

configure Maven Shade minimizeJar to include class files

I am trying to minimize the UberJar's size by using Maven Shade Plugin's minimizeJar. It looks like minimizeJar only includes classes that are statically imported in the code (I suspect this because I see LogFactory.class in uber jar at org\apache\commons\logging\ but no classes of the impl package are included, hence throwing java.lang.ClassNotFoundException: org.apache.commons.logging.impl.LogFactoryImpl when I run the uber-jar).
Is there any way I can tell Maven's Shade plugin to include specified packages into the final jar no matter what the minimizeJar suggests?
Here the pom snippet of what I am trying:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>commons-logging:commons-logging</artifact>
<includes>
<include>org/apache/commons/logging/**</include>
</includes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.myproject.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
This functionality has been added to version 1.6 of the maven-shade-plugin (just released). minimizeJar will now not remove classes that have been specifically included with filters. Note that including some of an artifact's classes in a filter will exclude non-specified classes for that artifact, so be sure to include all the classes that you need.
Here's an example plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>log4j:log4j</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>commons-logging:commons-logging</artifact>
<includes>
<include>**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
I am using the 2.0 version of the Maven Shade Plugin and still I am not able to include classes after "minimizing" the JAR.
As a workaround the only thing that comes to my mind is to create references to the needed classes to avoid the minimization code to get rid of them. ie:
/*
* This block prevents the Maven Shade plugin to remove the specified classes
*/
static {
#SuppressWarnings ("unused") Class<?>[] classes = new Class<?>[] {
JButton.class,
JMenu.class,
JMenuBar.class,
JMenuItem.class
};
}
I hope Maven developers implement a way to handle this situation (which I guess is very common).

How to exclude project from multi module Maven License Plugin aggregation?

I'm having trouble trying to ignore some modules when I add headers to my files using maven license plugin. The way I am trying to do is to put the modules path in the excludes tag. Here is the plugin in pom.xml:
<plugin>
<groupId>com.mycila.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
<version>1.10.b1</version>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
<phase>validate</phase>
<configuration>
<header>header.txt</header>
<aggregate>true</aggregate>
<excludes>
<exclude>**/a/**</exclude>
<exclude>**/b/**</exclude>
<exclude>**/c/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
This is the way I'm trying to ignore the projects inside this parent pom.xml. I have already tried also with
${project.basedir}/a/
and
${basedir}/a/
Thank you in advance.
Please try:
<excludes>
<exclude>a/**</exclude>
<exclude>b/**</exclude>
<exclude>c/**</exclude>
</excludes>

Categories