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

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>

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.

Maven assembly - separate jar and dependecies

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>

copy the content of a directory of maven module project into another module

I want to copy the content of a directorymodule maven project.
Here is the hierarchy
I want to copy the content of dist directory into Tourism/Tourism-Services/src/main/webapp directory
For this purpose, I tried to use maven-assembly-plugin
here is the extract of the relative pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>./</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>cfg-main-resources</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/angular5/tourism/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Here is the extract of assembly.xml file
<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>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>./</outputDirectory>
<directory>src/main/angular5/tourism/dist</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
The problem is that the content of dist directory is copied in Tourism/Tourism-Web/target directory. I would like to copy this content in another module, in Tourism/Tourism-Services/src/main/weapp directory. Thank you for your help
I succeded in configuring the pom.xml's
Here is an extract of pom.xml within Tourism-Web sub-module
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>cfg-main-resources</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>${basedir}/src/main/angular5/tourism/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Here is an extract of assembly descriptor file
<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>assembly</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>./</outputDirectory>
<directory>src/main/angular5/tourism/dist</directory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
Here is an extract of pom.xml of Tourism-Services sub-module
<dependencies>
<dependency>
<groupId>tourism-guide</groupId>
<artifactId>tourism-web</artifactId>
<version>${project.version}</version>
<classifier>assembly</classifier>
<type>zip</type>
<scope>compile</scope>
</dependency>
..................
's..................
</dependencies>
<build>
<!-- Set the name of the war, used as the context root when the app is
deployed -->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-angular-dist-resources</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>compile</phase>
<configuration>
<outputDirectory>${basedir}/src/main/webapp</outputDirectory>
<includeArtifactIds>tourism-web</includeArtifactIds>
<includeGroupIds>${project.groupId}</includeGroupIds>
<excludeTransitive>true</excludeTransitive>
<excludeTypes>pom</excludeTypes>
<scope>compile</scope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Thank you for reading

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.

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.

Resources