How to exclude dependencies from maven assembly plugin : jar-with-dependencies? - java

Maven's assembly plugin enables the creation of a big jar including all dependencies with descriptorRef jar-with-dependencies.
How can one exclude some of these dependencies? It seems like it does not have such a configuration? Is there another solution?

Add the <scope>provided</scope> to the dependencies you don't want included in the jar-with-dependencies, e.g.
<dependency>
<groupId>storm</groupId>
<artifactId>storm</artifactId>
<version>0.6.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

You should use the excludes option available in dependencySet.
Follow below.:
Example in your pom.xml:
...
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<finalName>../final/${project.artifactId}</finalName>
<archive>
<manifest>
<addClasspath>false</addClasspath>
<mainClass>com.entrerprise.App</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/jar-with-deps-with-exclude.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
...
Now in your new file jar-with-deps-with-exclude.xml (where dependencySet lives):
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>jar-with-dependencies-and-exclude-classes</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<excludes>
<exclude>junit:junit</exclude>
<exclude>commons-cli:commons-cli</exclude>
<exclude>org.apache.maven.wagon:wagon-ssh</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>${project.build.outputDirectory}</directory>
</fileSet>
</fileSets>
</assembly>
That's all.

This example indicates one way to do this:
<dependencySets>
<dependencySet>
....
<excludes>
<exclude>commons-lang:commons-lang</exclude>
<exclude>log4j:log4j</exclude>
</excludes>
</dependencySet>
....
</dependencySets>
Essentially we would use the excludes option available in dependencySet.
See also: https://maven.apache.org/plugins/maven-assembly-plugin/assembly-component.html

Another alternative is to switch to the much more feature rich maven-shade-plugin which can do this without any external assembly files or marking as "provided" (which may not be what you want as it forces users to have that dependency in their pom). Here is an example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<artifactSet>
<excludes>
<exclude><group-id>:<artifact-id>:<classifier></exclude>
<exclude>org.apache.logging.log4j:log4j-core</exclude>
</excludes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
https://maven.apache.org/plugins/maven-shade-plugin/index.html
Note that this wont make a jar-with-dependencies but rather an original-jar... and then just the regular output jar. This plugin even lets you filter out specific classes.

Related

Maven assembly plugin not building files from descriptor

I am trying to create a tar.gz with the name my-project-2.0.tar.gz and inside that file there is a zip file with all my java classes called my-project.zip. The reason for this is due to some script files that look for this type of format.
I have set up the maven assembly plug in my pom file and the descriptor files.
This issue is that when I run my build the plug in and descriptors are ignored.
The goal in eclipse is clean package.
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<phase>package</phase>
<id>assemble</id>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/prep.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<phase>package</phase>
<id>bundle</id>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>my-project-2.0</finalName>
<descriptors>
<descriptor>src/assembly/bundle.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
This is the bundle descriptor:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bundle-id</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target</directory>
<includes>
<include>my-project.zip</include>
</includes>
<outputDirectory></outputDirectory>
</fileSet>
<fileSet>
<directory>src/assembly</directory>
<includes>
<include>place.sh</include>
</includes>
<lineEnding>unix</lineEnding>
<fileMode>0755</fileMode>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
This is the prep descriptor
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>prep-id</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
Ive tried running clean assembly:assembly but I get a `No assembly descriptors found' error
Ive tried rewritting my plug in as:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<finalName>my-project-2.0</finalName>
<descriptors>
<descriptor>src/assembly/bundle.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<id>assemble</id>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
But I dont know how to name the inside zip as my-project.zip
When the zip file is created with prep.xml the name of the zip file should be ${project.artifactId}-${project.version}.zip.
So in your case my-project-${project.version}.zip.
Try to edit the bundle.xml the following way:
<fileSet>
<directory>target</directory>
<includes>
<include>my-project-${project.version}.zip</include>
</includes>
<outputDirectory></outputDirectory>
</fileSet>
The main reason why my build was ignoring my descriptors is because I had all my plugins wrapped around a <pluginManagement> </pluginManagement>.
This solved my issue

Maven dependency plugin adds artifact named directory

I want to copy files from some artifact. but it always adds a directory with the name of that artifact.
the pom of the artifact to copy from:
<groupId>some.group</groupId>
<artifactId>scheduler-common-test-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Scheduler common test resources</name>
<description>A scheduler test resources</description>
<packaging>pom</packaging>
.
.
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>lib/assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
the assembly file:
<assembly>
<id>json</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>resources/db</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>alterTables.sql</include>
<include>createTables.sql</include>
<include>insertsIntoReminders.sql</include>
</includes>
<excludes>
<exclude>pom.xml</exclude>
</excludes>
</fileSet>
</fileSets>
the item to be copied in the artifact pom:
<artifactItem>
<groupId>some.group</groupId>
<artifactId>scheduler-common-test-resources</artifactId>
<version>1.0.0-SNAPSHOT</version>
<outputDirectory>${project.build.directory}/test-classes/db/</outputDirectory>
<type>tar.gz</type>
<overWrite>false</overWrite>
</artifactItem>
result:
its get copied to test-classes/db/scheduler-common-test-resources-1.0.0-SNAPSHOT/
how can i remove the directory with the artifact name?
The assembly-plugin will by default add a baseDirectory, which will, also by default, be ${project.build.finalName}.
In your case, you just have to indicate the plugin that you don't need that directory by adding:
<includeBaseDirectory>false</includeBaseDirectory>
in the assembly descriptor (assembly.xmlfor you). See assembly descriptor documentation.

How to set the project to get all dependencies from specific folder (not a folder as a classpath)

I built a maven project, that it copy dependencies jar files in classpath folder (/lib folder near jar file) and look for required dependencies in lib/ folder and use them (set <classpathPrefix>lib/</classpathPrefix> in manifest.mf). it works correctly.
Now I want to change project that it doesn't dependent on dependencies version. I want to have a project that when a dependency required, pick it from lib folder. Because in future, when I need to change the dependency version, I don't have to rebuild the jar file with new pom.xml, and just change old jar file with new one in /lib.
Is there any way to do this ?
Edit:
My pom.xml:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>first.PathGetter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5</version>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
assembly.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>/lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>

How do you use the maven shade plugin to include only specific classes from a dependency with scope "provided"?

I am using the maven shade plugin to package my application into a jar file. One of my dependencies is to Tomcat:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.59</version>
<scope>provided</scope>
</dependency>
The scope of this dependency is provided as the container itself will supply its JAR files. However, I do need to add a few single classes from this dependency to my JAR file. I tried adding a filter and specifying the name of the class that is to be added, but it seems that provided dependencies are ignored by the shade plugin.
<build>
<plugins>
<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>org.apache.tomcat:tomcat-catalina</artifact>
<includes>
<include>org/apache/catalina/deploy/LoginConfig.class</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any ideas how what I need can be achieved?
The configuration setting you need is <useDependencyReducedPomInJar>true</useDependencyReducedPomInJar>. This will create a file named dependency-reduced-pom.xml with the references to the shaded jar(s) removed. You can also use <promoteTransitiveDependencies>true</promoteTransitiveDependencies> if the shaded jars have their own transitive dependencies to add them to the modified pom.
I had the same issue and If I can suggest a solution it would be to use the maven-assembly-plugin which will address your problem :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.mypackage.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
And here is a sample of the assembly.xml which will allow you include "provided" scope artifacts:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
</dependencySet>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
<scope>provided</scope>
</dependencySet>
</dependencySets>
I'am just having classpath issues in the generated artifact in the Manifest file!
I'm not very familiar with this plugin, but the best place for finding solutions to these kinds of problems is to have a look at the plugin/ goal documentation.
I guess the parameter keepDependenciesWithProvidedScope is what you are looking for.
I had the same issue, but I found we misunderstood how this plug-in works. If some dependencies are bundled into the shaded jar, they will be removed from the dependencies in the pom.xml in the shaded jar. So you should change nothing, just left their scope runtime.

With Maven, how can I build a distributable that has my project's jar and all of the dependent jars?

I have a project (of type 'jar') that (obviously) builds a jar. But that project has many dependencies. I'd like Maven to build a "package" or "assembly" that contains my jar, all the dependent jars, and some scripts (to launch the application, etc.)
What's the best way to go about this? Specifically, what's the best way to get the dependents into the assembly?
For a single module, I'd use an assembly looking like the following one (src/assembly/bin.xml):
<assembly>
<id>bin</id>
<formats>
<format>tar.gz</format>
<format>tar.bz2</format>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/command</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>*.sh</include>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>
To use this assembly, add the following configuration to your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
In this sample, start/stop scripts located under src/main/command and are bundled into bin, dependencies are bundled into lib. Customize it to suit your needs.
Here is my solution to create a distributable .zip (or .tar.gz / .tar.bz2) including all dependencies in a lib folder. It will:
Create a jar with a Manifest including the dependencies of the lib directory as the classpath and the main class to run when executing the jar.
Copy all dependent jars to the target/lib directory.
Create the distributable `zip with the main jar and all dependent jars of the lib directory.
Excerpt from pom.xml:
<!-- create distributable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>full.path.to.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/resources/dist.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
dist.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<formats>
<format>zip</format>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/lib</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/site</directory>
<outputDirectory>docs</outputDirectory>
</fileSet>
</fileSets>
</assembly>
The dist.xml was derived from the bin descriptor format here: http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html#bin
I have used the maven assembly plugin to packages everything in one single jar. you can find info here
http://maven.apache.org/plugins/maven-assembly-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
HTH.

Categories

Resources