How to prevent 'Error creating assembly archive distribution: A zip file cannot include itself' - maven

I have a multi module project and I want to make a zip with all sources (and other files). I use maven-assemble-plugin.
I found a way with dependencySet, but I do not want add all my module sources (and later javadoc, too) as dependencies. I tried to use moduleSet, but I get an error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.6:single (make-zip) on project test-distribution: Failed to create assembly: Error creating assembly archive distribution: A zip file cannot include itself -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.6:single (make-zip) on project test-distribution: Failed to create assembly: Error creating assembly archive distribution: A zip file cannot include itself
My pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assemble/distribution.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
<finalName>test</finalName>
</configuration>
</execution>
</executions>
</plugin>
My assembly descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<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>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<sources>
<outputDirectory>/Sources</outputDirectory>
</sources>
</moduleSet>
</moduleSets>
</assembly>
With dependencySet I could fix that problem with option useProjectArtifact, but I found no option for moduleSet. Is there any way to exclude the current project's build?

I find a way to exclude current project's build:
<?xml version="1.0" encoding="UTF-8"?>
<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>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<excludes>
<exclude>*:test-distribution</exclude>
</excludes>
<sources>
<outputDirectory>/Sources</outputDirectory>
</sources>
</moduleSet>
</moduleSets>
</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 to create password-protected zip in maven

currently I am using maven-assembly-plugin to create a zip file via "mvn clean package". The whole process works fine, the zip contains everything I need but a new requirement asks to create this zip file password-protected. I googled but not found any useful; is there anyone who needed to perform this task ?
Current plugin is defined in this way:
<build>
...
<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/zip.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</build>
and zip.xml is defined in this way:
<?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>zip</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<outputDirectory>directory1/</outputDirectory>
<directory>directory1</directory>
</fileSet>
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>config.properties</include>
</includes>
</fileSet>
</fileSets>
<files>
<file>
<source>target/data-jar-with-dependencies.jar</source>
<destName>data-jar-with-dependencies.jar</destName>
</file>
</files>
</assembly>
You must use an external program to unzip and zip the archive again. For example 7z:
7z e Unprotected.zip
7z a Protected.zip -pPASSWORD UNPROTECTEDFILENAME
If you want to integrate it in the build lifecycle then you need the maven exec-plugin to execute those two commands after the package phase.

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>

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>

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