Maven assembly - separate jar and dependecies - maven

I have a parent project with multiple jar project and I'm using a specific child project to assembly zip package with all jar and dependencies, indeed I added all my project as dependencies of this specific project.
Is there any way to separate jar and dependencies in two different zip?

The easiest way i can think about, is to use two assembly descriptors.
pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly-myjars.xml</descriptor>
<descriptor>src/assembly/assembly-dependencies.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
assembly-myjars.xml:
<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>myjars</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>your.groupId:*</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
assembly-dependencies.xml:
<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>dependencies</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<excludes>
<exclude>your.groupId:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>

Related

creating a tar archive from maven deploy

I am trying to use maven v3 to create a tar.gz output for a non-Java project in order to deploy to nexus. I cannot specify tar in the packaging option in my pom file. Unless mistaken, I can specify tar as an option for packaging when running mvn deploy. Is there anyway I can specify that in the pom file itself?
You can create the TAR.GZ file and "attach" it to the primary artifact. I have an example to repackage a WAR into Linux service on GitHub. The key steps are to package the assembly:
<project ...>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<attach>true</attach>
<formats>
<format>tar.gz</format>
</formats>
<descriptorSourceDirectory>src/main/assemblies</descriptorSourceDirectory>
</configuration>
</plugin>
...
</plugins>
</pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
...
</plugins>
</build>
</project>
<attach>true</attach> installs the artifact. The assembly in this case is:
<?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>bin</id>
<baseDirectory>${artifactId}-${version}</baseDirectory>
<files>
<file>
<outputDirectory>bin</outputDirectory>
<source>
${project.build.directory}/${project.build.finalName}.jar
</source>
<filtered>false</filtered>
<destName>${artifactId}.jar</destName>
</file>
</files>
<fileSets>
<fileSet>
<outputDirectory>bin</outputDirectory>
<directory>${project.basedir}/src/bin</directory>
<includes>
<include>${artifactId}.conf</include>
</includes>
<filtered>true</filtered>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>*:*-x86_64:*</include>
</includes>
<scope>runtime</scope>
<outputFileNameMapping>
lib${artifact.artifactId}.${artifact.type}
</outputFileNameMapping>
</dependencySet>
</dependencySets>
</assembly>
but you'll need to find/create one appropriate for your use-case.

How do I exclude certain target output sub-directories with maven-assembly-plugin?

I am using Maven 3.x with the maven-assembly-plugin. When I build my project I get the message below.
Caused by: org.apache.maven.lifecycle.LifecycleExecutionException:
Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2.1:single (tarball)
on project [ProjectName]: Failed to create assembly:
Error adding file 'io.something:artifactid:jar:1.5.0' to archive:
/path/to/project/target/scoverage-classes isn't a file.
I am using scoverage for Scala compilation and testing, and I don't know why the maven-assembly-plugin would want to include that sub-directory.
The project is quite complicated ("highly nested" maven projects), but I will post the relevant parts. In the build plugins section.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<tarLongFileMode>gnu</tarLongFileMode>
<descriptors>
<descriptor>src/main/assembly/job.xml</descriptor>
<descriptor>src/main/assembly/src.xml</descriptor>
</descriptors>
<archive>
<index>true</index>
</archive>
</configuration>
<executions>
<execution>
<id>tarball</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>1.3.0</version>
</plugin>
In the reporting-plugins section.
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<scalaVersion>2.10.4</scalaVersion>
<highlighting>true</highlighting>
<aggregate>true</aggregate>
</configuration>
<reportSets>
<reportSet>
<reports>
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
The job.xml assembly descriptor is as follows. I'm suspecting this descriptor is the primary culprit (with the target referenced).
<assembly
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
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>job</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>target</outputDirectory>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<scope>system</scope>
<outputDirectory>target</outputDirectory>
<excludes>
<exclude>${artifact.groupId}:${artifact.artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
The src.xml assembly descriptor is as follows.
<assembly
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
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>dependencies</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
</assembly>

Package provided dependencies in a single zip file

I have put dependency jars in separate folder but expecting folder should be zipped instead of direct folder. I tried using assembly descriptor but it is creating separate folder with dependencies but not zipping it (folder should not be there, only zip file is expected). Please suggest me better approach to accommodate my dependency jars in a single zip file
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>provided</includeScope>
<outputDirectory>/Users/venugopal/Documents/providedDependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/archive_format.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
archive_format.xml (Assembly descriptor
<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>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>/Users/vp/Documents/providedDependencies</directory>
</fileSet>
</fileSets>
</assembly>
If I understand your requirement right, you want to package all provided dependencies into a zip file.
You are headed the wrong way using maven-dependency-plugin. You should use maven-assembly-plugin and its dependencySet:
<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>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<scope>provided</scope>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>
Setting the scope to provided will make sure only the provided dependencies are considered in this set. We need to exclude the project artifact, otherwise it will be included by default.

Include spring xml in jar by maven-assembly-plugin packaging

I am trying to create an executable jar with all of its dependency along with the spring's configuration xml file.
Here is the definition of the maven-assembly-plugin in the pom.xml:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>service.coucou.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
This is the assembly.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<includes>
<include>/src/main/resources/cxf.xml</include>
</includes>
</fileSet>
</fileSets>
</assembly>
I am trying to include /src/main/resources/cxf.xml file in the created jar. But it is not including the xml. How can I include it in the jar?
Replace the <fileSets/> with:
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>cxf.xml</include>
</includes>
</fileSet>

Maven best practice for creating ad hoc zip artifact

Assume that I need to manage an artifact that consists of an aribtrary folder / file structure rolled up as a zip archive. It's not clear to me how to accomplish this in Maven in a way that best fits "the Maven way."
I know that there is no "zip" packaging type. Does this mean there is no generic lifecycle in Maven to simply take what I have in the resources folder, zip it up, and install/deploy it to my repositories?
I'm looking for options, with evaluations of how each option satisifies my requirement to follow the maven way, as I do not wish to incur the obvious penalities of straying from the golden path . . .
Decide what classifier you will use for your zip file, for sake of argument let's say it would be sample.
In your project create file assembly/sample.xml
Fill in assembly/sample.xml with something like this:
<?xml version="1.0" encoding="UTF-8"?>
<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>sample</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>some/directory/in/your/project</directory>
</fileSet>
</fileSets>
<!-- use this section if you want to package dependencies -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<excludes>
<exclude>*:pom</exclude>
</excludes>
<useStrictFiltering>true</useStrictFiltering>
<useProjectArtifact>false</useProjectArtifact>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
Add this to your pom's build section
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/sample.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
As a result it should create and install you-project-name-VERSION-sample.zip.
I suggest you read chapter on assemblies from Sonatype's maven book:
https://books.sonatype.com/mvnref-book/reference/assemblies.html
Also, read assembly format specification: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
Fill in assembly/sample.xml with something like this:
<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>install</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<includes>
<include>README*</include>
<include>*.sh</include>
</includes>
</fileSet>
<fileSet>
<directory>target/classes/</directory>
<outputDirectory>resources</outputDirectory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
<lineEnding>dos</lineEnding>
</fileSet>
<!--
<fileSet>
<directory>target/</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
<lineEnding>dos</lineEnding>
</fileSet>
-->
</fileSets>
<!-- use this section if you want to package dependencies -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<excludes>
<exclude>*:pom</exclude>
</excludes>
<useStrictFiltering>true</useStrictFiltering>
<useProjectArtifact>true</useProjectArtifact>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
add below codes in pom.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>assembly/sample.xml</descriptor>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
run commands as mvn package -P dev -Dmaven.test.skip
will create the zip files that include :
xxxx.zip
-lib/.jar
-resources/.xml/properties
-readme
-start.sh
In your maven project create a folder assembly.
Add zip.xml file in the assembly folder.
Add below code in zip.xml to package the content of your project in to zip.
<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>zip</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>directoryName of your project</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>

Resources