I am using commons-io in my project and want to shade it. I am running into a warning that I can't seem to figure out:
[WARNING] commons-io-2.7.jar, murder-1.0-SNAPSHOT.jar define 180 overlapping classes and resources:
...
I find this strange, as murder-1.0-SNAPSHOT.jar is the jar I am trying to build and which should include the commons-io jar.
I define my commons-io dependency as such:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
<scope>compile</scope>
</dependency>
(I thought I was supposed to use the runtime scope, but then I can't run package because it complains that FileUtils cannot be found)
Here's my config for the shade plugin:
<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>
<artifactSet>
<includes>
<include>commons-io:commons-io</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
If I remove the <filters> entirely, I only get this warning:
commons-io-2.7.jar, murder-1.0-SNAPSHOT.jar define 1 overlapping resource
[WARNING] - META-INF/MANIFEST.MF
Everything still seems to work fine, but I want to get rid of this warning when I package.
EDIT:
If I run mvn clean first, the next mvn package produces no such warning. Subsequent runs however introduces the warning again.
In this end, this is what I ended up with that seems to work and produces no warnings:
<properties>
<!-- replace this with wherever you want your shaded dependencies to end up -->
<shaded-dependencies>io.vapidlinus.shade</shaded-dependencies>
</properties>
....
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>commons-io:commons-io</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.MF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>org.apache.commons.io</pattern>
<shadedPattern>${shaded-dependencies}.org.apache.commons.io</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
Related
I created a general shade plugin instance below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<filters>
<filter>
<artifact>*:*<artifact/>
<excludes>
<exclude>META-INF/*.gif</exclude>
<excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
However when executing mvn clean package I found the original- jar is the same size of the shade jar and contains all the dependency library, what could be the cause of such incident
original-myapp-0.0.1-SNAPSHOT.jar 168Mb
myapp-0.0.1-SNAPSHOT.jar 168Mb
I would like to generate a jar that contains only few classes from my project and one of its dependencies.
schema of what I would like to achieve
I currently managed to do that with a jardesc file containing the path of all the classes/packages I need since the dependency is also one of my projects.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jardesc>
<jar path="C:/temp/myJar-4.0.0.jar"/>
<options buildIfNeeded="true" compress="true" descriptionLocation="/ProjectB/src/main/resources/exportJar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="true" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
<selectedProjects/>
<manifest generateManifest="true" manifestLocation="" manifestVersion="1.0" reuseManifest="false" saveManifest="false" usesManifest="true">
<sealing sealJar="false">
<packagesToSeal/>
<packagesToUnSeal/>
</sealing>
</manifest>
<selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
<javaElement handleIdentifier="=projectA/src\/main\/java<com.stckvrflw.parsingTools"/>
<javaElement handleIdentifier="=projectA/src\/main\/java<com.stckvrflw.utils{ClassA.java"/>
<javaElement handleIdentifier="=projectB/src\/main\/java<com.test.myPackage{ClassE.java"/>
<javaElement handleIdentifier="=projectB/src\/main\/java<com.test.myPackage{ClassF.java"/>
</selectedElements>
</jardesc>
Is it possible to do that with Maven without duplicating classes ?
The jar needs to have it's own artifactId and version.
You can use maven-shade-plugin and configure it like below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<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>
Source: maven-shade-plugin
I have an Apache Maven Java project that imports the class:
com.sforce.soap.enterprise.EnterpriseConnection
When running the project from IntelliJ, I have no problems executing the code. However, I want to be able to run the project from the command line as a shaded jar. I have been shading the jar using 'mvn package'. When I run the jar from the command line I am getting the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sforce/soap/enterprise/EnterpriseConnection
My pom includes dependencies:
<dependency>
<groupId>qa-integration.sfdc</groupId>
<artifactId>enterprise</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/enterprise-1.0.0.jar</systemPath>
</dependency>
<dependency>
<groupId>qa-integration.sfdc</groupId>
<artifactId>metadata</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/metadata-1.0.0.jar</systemPath>
</dependency>
My shade execution looks like the following:
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.sunrun.integration.sfdc.metadata.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
When I list the contents of the jar, I see no salesforce files at all.
I had this problem and my solution was to add the following snippet to my pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>corrigo.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-jar-plugin</artifactId>-->
<!--<version>2.4</version>-->
<!--<configuration>-->
<!--<archive>-->
<!--<manifest>-->
<!--<mainClass>corrigo.Main</mainClass>-->
<!--</manifest>-->
<!--</archive>-->
<!--</configuration>-->
<!--</plugin>-->
</plugins>
</build>
Then you just run mvn package.
Here is a reliable resource:
https://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/
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>
I would like to use Maven to create a jar that includes a specific dependent artifact and excludes some of the source java files.
Currently I use this snippet:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shadedJar</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedClassifierName>classifier</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>false</createDependencyReducedPom>
<artifactSet>
<includes>
<include>com.google.guava:guava</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
Thus, the jar does contain the 'guava' artifact files, however I would also like to exclude some source files (for example a specific file under src/main/java/my.package/), How can I do that?
I use shade to create a jar, but I do not need some files of eclipse, txt and other things that they are relevant only to development.
This is the code I have, maybe you can addapt it and use it.
<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>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>.settings/**</exclude>
<exclude>*.classpath</exclude>
<exclude>*.project</exclude>
<exclude>*.txt</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>