Create jar with dependencies and excluding specific source files - java

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>

Related

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>

Maven shade plugin: why original jar contains all the dependency (just like the shaded jar)

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

Maven Shade overlapping classes warning

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>

"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.

Maven shade copy WEB-INF

Using maven-shade-plugin I am trying to create a project file structure as follows:
The problem is that the following pom configuration is not creating the WEB-INF directory.
<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>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<createDependencyReducedPom>true</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.my.package.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Basically I need to copy my web.xml file, classes and lib files into a directory called WEB-INF in the root of the project.
Update: SORRY, I copied the wrong plugin from my pom into the original question!
Update: If I add the packaging from the pom declaration: <packaging>war</packaging> The WEB-INF file structure is completed as per the OQ. However, with this declaration the com directory now does not contain my packages so the main class cannot be found.
How it looks with the <packaging>war</packaging>:
How it should look:
maven-assembly plugin allows much more flexibility, it would be easily accomplished by using assembly plugin
See
assembly descriptor examples
There are lots of possible answer to achieve the desired effect with maven. However, based on the update I gave I found that adding the maven-antrun-plugin with the following solved my issue:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>move-main-class</id>
<phase>compile</phase>
<configuration>
<tasks>
<copy todir="${project.build.directory}/${project.artifactId}">
<fileset dir="${project.build.directory}/classes/">
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Categories

Resources