Assembly plugin generating 2 jars. One with classfiles. One with dependencies - maven

I am having trouble building a jar using the assembly plugin. I have a bunch of system jar files that I want bundled alongside class files of the project.
I was looking to do something like:
final
- lib
- system.jar
- system1.jar
- com
- servlet
-- etc
I have tried to use the assembly plugin but it generates 2 jars. One with just the lib with my jars and another with the class files.
I have read through the assembly docs but I must be missing something.
Here is the build element I have in my pom.xml
<dependency>
<groupId>jackson-mapper-asl</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jackson-mapper-asl-1.9.4-sc1.jar</systemPath>
</dependency>
//... etc
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>final-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This is my assembly file (assembly.xml)
<assembly>
<id>distribution</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>system</scope>
<outputDirectory>lib</outputDirectory>
<includes />
</dependencySet>
</dependencySets>
</assembly>
Any idea how I can merge these jars together?

You would usually have a much simpler layout, such as:
final
- lib
-system.jar
-system1.jar
-project.jar
I.e. the project itself is a jar too, and included in the lib. In this case just add <useProjectArtifact>true/>useProjectArtifact> to the dependencySet.
If you really, really want to have classes copied in directly, try adding a fileset to the assembly, copying from target/classes into your root location
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>

Related

Maven building jar: merge duplicate resources from dependencies

I'm getting this problem:
I have a project with spring-boot. It works perfectly on eclipse, but I need to generate a jar with dependencies, so I add the maven-assembly-plugin configuration to the pom.
Some spring dependencies has in the META-INF a file called spring.schemas, and I need to merge all spring.schemas into one (spring-context, spring-beans, etc)
I tried this solution using maven-shade-pluggin and AppendingTransformer, and it merges all spring.schemas perfectly... But it has an issue, when I execute the jar, it fails with:
java.lang.IllegalStateException: Unable to open nested entry 'lib/spring-boot-starter-batch-1.2.4.RELEASE.jar'.
It has been compressed and nested jar files must be stored without compression.
Please check the mechanism used to create your executable jar file
So, shade plugin compress the jar, and spring-boot doesn't like it, and there's no way to turn off the compression in shade. I manually copy the shade spring.schemas generated by shade and I put it in the maven-assembly-pluggin generated and uncompressed jar with dependencies. It works.
Then I tried to include the generated spring.schemas into my resources folder, but it's allways overrided by the spring.schemas from spring-context.
I also tried this other solution to exclude spring.schemas from dependency jar using a descriptor XML in assembly, but it doesn't work:
<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>distribution</id>
<formats>
<format>jar</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<include>org.springframework:spring-context</include>
</includes>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>**/spring.schemas</exclude>
</excludes>
</unpackOptions>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>org.springframework:spring-context</exclude>
</excludes>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
</manifest>
<compress>false</compress>
</archive>
<descriptors>
<descriptor>src/main/assembly/distribution.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This are my dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
<!-- MySql 5.5 Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
</dependencies>
Any ideas? Thanks in advance
You can use maven-assembly-plugin.
Add the plugin to your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>fat-jar.xml</descriptor>
</descriptors>
</configuration>
</plugin>
Add a fat-jar.xml descriptor next to your pom.xml:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>fat</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-spring</handlerName>
</containerDescriptorHandler>
</containerDescriptorHandlers>
</assembly>
Notice the metaInf-spring handler. It will merge any META-INF duplicate files having names starting with the spring word.
The reference documentation: https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/using-container-descriptor-handlers.html
Instead of using maven-assembly-plugin and maven-shade-pluggin, I would try to use Spring Boot's own spring-boot-maven-plugin that can be used to create an executable ‘fat’ JAR. See this reference documentation page, section "73.2 Create an executable JAR with Maven".
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
Or, if you don't use the spring-boot-starter-parent POM:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.2.5.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

maven copy-dependencies includeGroupIds and all transitive elements to those

I try to figure out how to copy all dependencies to an explicit version and all the required dependencies.
For Example: My project requires version 3 of a third party lib, called foobar.
I want to copy the version 3 libraries to a folder named lib-foobar-${foobar.version}.
In this folder are those jars which are required to use foobar in version 3. That means the jar itself and all dependent jars which are declared in the foobar pom.
I currently use the org.codehaus.mojo:maven-dependency-plugin:2.1 with goal copy-dependencies in phase package.
My configuration is:
<configuration>
<outputDirectory>${project.build.directory}/lib-foobar-${foobar.version}</outputDirectory>
<includeGroupIds>com.foobar</includeGroupIds>
<excludeTransitive>false</excludeTransitive>
<excludeScope>test</excludeScope>
<includeScope>compile</includeScope>
</configuration>
I don't want to list all allowed and not allowed lib's because a step to a newer version takes place every month.
Are there any other tools which can do that or is there any dodge for that?
Big thanks to user944849.
It was very helpful to me for finding out the best solution in that case.
For everyone who is interested in my solution, here it comes:
At first I changed the plugin to maven-assemby-plugin and added a new assembly file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<dependencies>
<dependency>
<groupId>com.foobar</groupId>
<artifactId>foobar-parent</artifactId>
<version>${foobar.version}</version>
<type>pom</type>
</dependency>
</dependencies>
<executions>
<execution>
<id>copy-foobar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assemble/foobar-libs.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
And the assembly file looks like:
<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>standalone</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>org.foobar</include>
</includes>
<outputDirectory>/lib-foobar-${foobar.version}</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
The key was the <useTransitiveFiltering> element, which resolves all transitive libs based on the included libs.

Maven - Using the Assembly plugin to package dependencies from multiple projects into one file

I’m working on a project that is made up of multiple other projects. It has the following structure:
MainProject
--projectA
--projectB
--projectC
--projectD
--parent
--projectE
...
--projectX
As you can see the parent is amongst the others. I’m not sure if this is unusual or not but it is the way it is and I have to just work with it.
I’m looking to use the Assembly plugin to get the dependencies from each of the sub projects and zip them up all within one folder. So for example, I want a zip file containing a folder which contains all of the unpacked dependencies of the children projects.
This is what I have so far:
assembly.xml (this is in the parent project)
[...]
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<includes>
<include>com.example:../one</include>
<include>com.example2:../two</include>
<include>com.exampleX:../three</include>
</includes>
<binaries>
<outputDirectory>/library/jars</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>library/jars</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<excludes>
<exclude>**.xml</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
and this is in the parent pom.xml
[...]
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
[...]
When I run a mvn clean install from cmd the build is successful. It does not however create a zip file containing my library/jars directory which should then contain all of the dependencies from each of the projects. It simply creates zips in each project. These zips do not contain the directory I want or any dependencies, they are simply zips of the each project directory. Can anyone see what I am doing wrong?

maven assembly dir with a jar dependency from project

I am trying to use maven to assemble a directory for a install package. The install package requires several things: configuration files, dependency libraries and an executable jar of my class files. I am having trouble figuring out how to add the executable jar.
Here is what I have so far:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myproject</artifactId>
<version>8.1.1</version>
<name>my project</name>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<configuration>
<descriptor>${basedir}/src/main/assembly/client.xml</descriptor>
</configuration>
<executions>
<execution>
<id>create-client</id>
<configuration>
<descriptor>${basedir}/src/main/assembly/client.xml</descriptor>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
.
.
.
</dependencies>
</project>
Then my assembly descriptor looks like this:
<assembly>
<id>client</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/reports</directory>
<outputDirectory>/reports</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/resources/audio</directory>
<outputDirectory>/audio</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/client</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
If i call mvn assembly:assembly I get a directory with all the libraries and configuration files. Works great.
But now I want to add an executable jar of all my compiled code. I can create this jar alone by adding this to my pom and calling mvn jar:jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>run</finalName>
<archive>
<manifest>
<mainClass>com.myproject.main.StartProcess</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
The question is, how can I get that jar into my assembly automatically?
I have tried a couple things but I have no idea where to start. Can I somehow call the jar execution from my assembly?
Any help appreciated.
I think you should turn your useProjectArtifact to true (this is the default value).
It determines whether the artifact produced during the current project's build should be included in this dependency set.

How can I make the test jar include dependencies in Maven?

I have a project with src/main/java and src/test/java structure, and I managed to use maven-jar-plugin to build a jar of the test branch. However, I want to package the test jar so that all the dependencies are resolved. Is there a way I can tell maven-jar-plugin to include the dependencies??
Thanks!
Frank
I had a similar problem with integration tests I need to run on Hadoop. Our integration tests are located in the test folder of a separate integration test module, so what is required is a test-jar-with-dependencies to make our life easier.
I'm using the assembly plugin as mentioned by Michael-O. My assembly descriptor is located in src/main/assembly/test-jar-with-dependencies.xml and is a modification of the standard jar-with-dependencies descriptor that's part of the plugin:
<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>test-jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<!-- we're creating the test-jar as an attachement -->
<useProjectAttachments>true</useProjectAttachments>
<unpack>true</unpack>
<scope>test</scope>
</dependencySet>
</dependencySets>
</assembly>
This assembly relies on the test-jar being created as part of the module build. So I added the following to the module's pom.xml:
<!-- create a complete jar for testing in other environments -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/test-jar-with-dependencies.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
You can do this: Create a jar assembly with the assembly plugin, have the dependencies unpacked, pack a new test jar and attach it to the reactor. You're done.
The descriptor for the packaging could look like this.
In a similar situation I ended up moving my test code to a separate jar and made it depend on the original one. You can use an aggregator project to ensure that tests are run when you build the main jar.
<dependency>
<groupId>me.wener.xxx</groupId>
<artifactId>xxx-core</artifactId>
<version>${xxx.version}</version>
<type>test-jar</type>
<!-- <scope>test</scope> -->
</dependency>
I use this to include the test jar.the important line is <type>test-jar</type>.I am not sure this is what you need.
3 years ago, but may help others.At least, it helped me. :-)
to include a test-jar dependency in your assembly specify the include filter of the assembly debendencySet as bellow:
...
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<include>*:jar:*</include>
<include>*:test-jar:*</include>
</includes>
</dependencySet>
...
The following worked for Maven 3
POM.XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<phase>test-compile</phase>
</execution>
</executions>
</plugin>
ASSEMBLY FILE
<dependencySet>
<outputDirectory>demo/test-lib</outputDirectory>
<includes>
<!--test only dependencies (like netty)-->
<include>io.netty:netty-all</include>
<!-- the actual test jar-->
<include>${project.groupId}:${project.artifactId}:test-jar</include>
</includes>
<useProjectAttachments>true</useProjectAttachments>
<scope>test</scope>
</dependencySet>

Resources