maven-shade-plugin doesn't replace the original jar - java

It is weird that my maven-shade-plugin doesn't replace the original jar with the shaded jar. Does anyone know what could be the reason?
Here's my plugin in pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${plugin.shade.version}</version>
<configuration>
<artifactSet>
<excludes>
<!-- Leave slf4j unshaded so downstream users can configure logging. -->
<exclude>org.slf4j:slf4j-api</exclude>
<exclude>org.slf4j:slf4j-log4j12</exclude>
<!-- Leave commons-logging unshaded so downstream users can configure logging. -->
<exclude>commons-logging:commons-logging</exclude>
<!-- Leave commons-exec unshaded so downstream users can use ProcessLauncher. -->
<exclude>org.apache.commons:commons-exec</exclude>
<!-- Leave log4j unshaded so downstream users can configure logging. -->
<exclude>log4j:log4j</exclude>
</excludes>
</artifactSet>
<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.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>NOTICE.txt</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/LICENSE.txt</resource>
<file>${basedir}/../../LICENSE.txt</file>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/NOTICE.txt</resource>
<file>${basedir}/../../NOTICE.txt</file>
</transformer>
</transformers>
<relocations>
<relocation>
<pattern>org</pattern>
<shadedPattern>${shaded.dependency.prefix}.org</shadedPattern>
<excludes>
<exclude>org/apache/zeppelin/*</exclude>
<exclude>org/apache/zeppelin/**/*</exclude>
<exclude>org/apache/thrift/*</exclude>
<exclude>org/apache/thrift/**/*</exclude>
<exclude>org/slf4j/*</exclude>
<exclude>org/slf4j/**/*</exclude>
<exclude>org/apache/commons/logging/*</exclude>
<exclude>org/apache/commons/logging/**/*</exclude>
<exclude>org/apache/commons/exec/*</exclude>
<exclude>org/apache/commons/exec/**/*</exclude>
<exclude>org/apache/log4j/*</exclude>
<exclude>org/apache/log4j/**/*</exclude>
<exclude>org/sonatype/*</exclude>
<exclude>org/sonatype/**/*</exclude>
<exclude>**/pom.xml</exclude>
<!-- Not the org/ packages that are a part of the jdk -->
<exclude>org/ietf/jgss/*</exclude>
<exclude>org/omg/**/*</exclude>
<exclude>org/w3c/dom/*</exclude>
<exclude>org/w3c/dom/**/*</exclude>
<exclude>org/xml/sax/*</exclude>
<exclude>org/xml/sax/**/*</exclude>
</excludes>
</relocation>
<relocation>
<pattern>com.google</pattern>
<shadedPattern>${shaded.dependency.prefix}.com.google</shadedPattern>
</relocation>
<relocation>
<pattern>io</pattern>
<shadedPattern>${shaded.dependency.prefix}.io</shadedPattern>
</relocation>
<relocation>
<pattern>com.esotericsoftware</pattern>
<shadedPattern>${shaded.dependency.prefix}.com.esotericsoftware</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

Shaded plugin by default save original file as -original.jar, if you want to replace original file with the new generated (shaded), put this line in your configuration plugin section:
<configuration>
...
<outputFile>${output.directory}\${project.artifactId}-${project.version}.jar</outputFile>
...
</configuration>
Replace output.directory with your shade plugin outputDirectory.
Check this post with more details: post

Looks like Configuration tag should come inside execution tag in the hierarchy, as shown below. Please re-organize executions and execution tag and include your configuration under it as shown below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.plugin}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.apache*</pattern>
<shadedPattern>shaded.org.apache*</shadedPattern>
</relocation>
<relocation>
<pattern>com.cookie*</pattern>
<shadedPattern>shaded.com.cookie*</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
For more information, please refer the maven documentation http://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html

Related

How to configure in war type project of java to use Bytebuddy

For a jar-type project which does have main-class I am using the below configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<id>shade-my-jar</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.example.demo.SamplesJavaApplication</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
How to configure in war-type project, since i don't have main-class.

Deploy JAR and WAR to Nexus

I'm working on a JAVA based application. The POM has been so configured that the packaging phase generates an executable JAR (with the maven-shade-plugin) and a WAR (with the war-plugin). The main packaging in the POM is set to jar.
When i execute locally a
mvn clean package
I obtain in my target folder the expected result (WAR + shaded JAR). But for some reason, in the Jenkins CI, in the deploy phase, only the JAR is being send to Nexus, the WAR is ignored.
Is there anything specific I should do to have the WAR sent to NEXUS ?
Here below the plugin-configs:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
-->
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
and
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.build.finalName}-jar-with-dependencies</finalName>
</configuration>
</execution>
</executions>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/Log4j2Plugins.dat</exclude>
<exclude>module-info.class</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<shadedArtifactAttached>false</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>my.main.class</mainClass>
</transformer>
</transformers>
</configuration>
</plugin>

NO shaded jar found in target after maven shade plugin packages the project

On running mvn package:shade shade, the log has entry which says:Replacing XYZ.jar with XYZ-shaded.jar
but in my target directory, I cannot find shaded jar
This is my pom regarding maven shade
After trying , this pom helped me
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<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>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>akkaexample.Akkmain</Main-Class>
</manifestEntries>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>akkaexample.Akkmain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Have your checked whether the output jar file is the shaded one?
The Shade plugin is probably replacing the original file here, meaning overwriting it with the shaded version.
If you wish to get both jar files (shaded and unshaded) at the end, you probably need to set the shadedArtifactAttached property to true:
Defines whether the shaded artifact should be attached as classifier to the original artifact. If false, the shaded jar will be the main artifact of the project
See Maven Shade Mojo Documentation.

"Deleting" the generated default jar in Maven

There are solutions available to "disable" the default jar that is generated by the maven-jar-plugin. However, since my final artifacts probably depend on this jar (I am relatively new to maven, so not entirely sure if it is actually the case, but it seems so) when I disable it, my build fails with the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.3:shade (build-cli) on project putwb: Failed to create shaded artifact, project main artifact does not exist. -> [Help 1]
Otherwise, it produces an empty jar along with the other jars I am building through my scripts. Can somebody suggest a way to "delete" the default jar, after the build is complete? Alternatively, can someone point out how I can disable that yet my build succeeds?
I am posting the relevant part of my pom.xml below:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>readme-md</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>README.md</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<!-- Build the CLI version -->
<phase>package</phase>
<id>build-cli</id>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<!-- Don't include the UI code -->
<filter>
<artifact>*:*</artifact>
<excludes>in/ac/iitk/cse/putwb/ui/**</excludes>
</filter>
</filters>
<finalName>putwb-cli-${project.version}</finalName>
<transformers>
<!-- Don't include images -->
<transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>.png</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>in.ac.iitk.cse.putwb.experiment.PUTExperiment</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
<execution>
<!-- Build the UI version -->
<phase>package</phase>
<id>build-ui</id>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>putwb-ui-${project.version}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>in.ac.iitk.cse.putwb.ui.PUTWb</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- If I add this to the script, the build fails -->
<!--
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
You can use ant-run plugin's delete task. You can execute this plugin in any phase that is later than package phase in the life cycle.(because it will be created again in package phase. Check which one suites you better.) Execute the maven command you choose as this plugins phase (like mvn verify if you choose verify phase)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete file="${project.build.directory}/YourJarName.jar"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
Check Build life cycle https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
thanks to #markthegrea for pointing the correct folder.

Jena java.lang.ExceptionInInitializerError

My java application perfectly fine. But when running it, i am getting the following error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at fr.emse.opensensingcity.configuration.ConfigurationFactory.createConfiguration(ConfigurationFactory.java:25)
at fr.emse.opensensingcity.main.main(main.java:75)
Caused by: java.lang.NullPointerException
at org.apache.jena.tdb.sys.EnvTDB.processGlobalSystemProperties(EnvTDB.java:33)
at org.apache.jena.tdb.TDB.init(TDB.java:248)
at org.apache.jena.tdb.sys.InitTDB.start(InitTDB.java:29)
at org.apache.jena.system.JenaSystem.lambda$init$43(JenaSystem.java:119)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:194)
at org.apache.jena.system.JenaSystem.forEach(JenaSystem.java:171)
at org.apache.jena.system.JenaSystem.init(JenaSystem.java:117)
at org.apache.jena.riot.RDFDataMgr.<clinit>(RDFDataMgr.java:79)
... 2 more
I am having the same problem as documented in this question but the solution does not solve my problem. The solution says to add some details with the Shade plugin, here is an extract of my pom file about the Shade plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<!-- Some jars are signed but shading breaks that.
Don't include signing files.
-->
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<!--<phase /><!- - Switch off -->
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

Categories

Resources