How to package multi module in a zip file? - maven

I have a multi module project.
Each module is packaged in a jar/war file in his own /target folder.
I want to take every module jar/war files and some config files, and put everything in a zip.
How to do that? I try with assembly plugin, but all i've managed to do so far is a zip of each module in their respective /target folder, so pretty useless ^^

First is to create a separate module let us call it dist add to the parent as new module and add all modules which you like to see in the resulting zip file as dependencies including the type war, jaretc.
The <packaging>pom</packaging> should be given cause this module does not contain any java code which needed to be compiled etc. We only want to create zip file which contains the given dependencies.
<project ..>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>module-1</groupId>
<artifactId>module-1-artifact</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>module-2</groupId>
<artifactId>module-2-artifact</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-bundles</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>proj1-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Add the following 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>dist-assembly</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
The dependencies are needed to be sure the order of execution is correctly calculated by Maven.
If you need supplemental configuration in that zip file you can add those files into src/main/config for example and add fileSets parts in the assembly descriptor.

Related

How to upload ZIP file to Nexus using Maven and avoid creating pom artifact in Nexus?

In my project I have couple of files that I pack into a ZIP file and upload it to a Nexus repository.
I implemented both actions using Maven assembly plugin:
POM.XML
<groupId>com.ddd.tools</groupId>
<artifactId>mytool</artifactId>
<version>2.1.0</version>
<packaging>pom</packaging>
<name>Tool</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- File bin.xml saves information about which files are to be included to the ZIP archive. -->
<descriptor>bin.xml</descriptor>
<finalName>${pom.artifactId}-${pom.version}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
BIN.XML
<assembly ...>
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/release</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>Tool.exe</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Now, when I look in Nexus I see TWO artifacts:
<dependency>
<groupId>com.ddd.tools</groupId>
<artifactId>mytool</artifactId>
<version>2.1.0</version>
<type>pom</type>
</dependency>
and
<dependency>
<groupId>com.ddd.tools</groupId>
<artifactId>mytool</artifactId>
<version>2.1.0</version>
<classifier>bin</classifier>
<type>zip</type>
</dependency>
I'm only interested in the latter ZIP artifact because it is the ZIP file I uploaded.
How can I get rid of the first POM artifact from Nexus? Or is there any scenario where I could need it?
I believe what you want to do is possible when using raw repos in Nexus 3. AFAIK, the pom is required for an actual artifact upload, per khmarbaise

How to get the content of a directory inside of WAR that is inside of an EAR that is inside of a ZIP?

I have the following :
WAR1 that has /images directory that contains : photo1.png, photo2.png
WAR2 that has /images directory that contains : photo2.png, photo3.png
WAR3 that has /images directory that contains : photo4.png, photo5.png
EAR1 that has WAR1 and WAR2
EAR2 that has WAR3
ZIP file that created by the Maven Assembly plugin that contains EAR1 and EAR2.
Now I need the following :
While creating the ZIP file by the Maven Assembly plugin, I need to create the directory /images inside of the ZIP file that contains the content of /images directory of every WAR file that is in the EAR that is in the ZIP.
Please, if not clear let me know guys, so I would give more details.
Thank you so much.
The first step is to create a separate module like mod-zip which contains the following pom which has dependencies to all the artifacts and the configuration for maven-assembly-plugin.
You have to change the parent to your parent approriately:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.soebes.maven.multiple.artifacts</groupId>
<artifactId>parent</artifactId>
<version>1.0.4-SNAPSHOT</version>
</parent>
<artifactId>mod-zip</artifactId>
<packaging>pom</packaging>
<name>Mod: ZIP</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mod-war1</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mod-war2</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mod-war3</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mod-ear1</artifactId>
<version>${project.version}</version>
<type>ear</type>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mod-ear2</artifactId>
<version>${project.version}</version>
<type>ear</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>test.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Second you need an assembly descriptor which describes what you like to have which is a little bit tricky:
<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</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<useProjectArtifact>false</useProjectArtifact>
<includes>
<include>${project.groupId}:*:war:${project.version}</include>
</includes>
<unpackOptions>
<includes>
<include>images/**</include>
</includes>
</unpackOptions>
</dependencySet>
</dependencySets>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>${project.groupId}:*:ear:${project.version}</include>
</includes>
<binaries>
<outputDirectory>result</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
</assembly>
The tricky things is to go via two steps. First filter the artifacts having only the wars with the dependencySets and the unpackOptions which will filter out the images from them. And second you need to use the EAR modules by using the moduleSets.

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

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>

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.

Managing multi-module dependencies with Maven assembly plugin

I use Maven assembly plugin to create an assembly for my multi-module project. There are two separate applications built from this multi-module project, each having a separate set of dependencies. I made a custom assembly descriptor which assembles two directories (for each application) with module builds and their respective dependencies. It does everything fine but one thing - it puts dependencies for both modules to each other's assembly.
The following is a simplified version of my project, that has exactly the same behavior.
Consider a project consisting of two modules and an assembly module:
APP
module1
module2
assembly
I have added dependencies purely for demonstration:
com.test.app:module1:jar:1.0
\- commons-cli:commons-cli:jar:1.2:compile
com.test.app:module2:jar:1.0
\- commons-daemon:commons-daemon:jar:1.0.8:compile
Here's the parent POM:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
<module>assembly</module>
</modules>
</project>
module1 POM:
<project>
<parent>
<groupId>com.test</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.app</groupId>
<artifactId>module1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
module2 POM:
<project>
<parent>
<groupId>com.test</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.app</groupId>
<artifactId>module2</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>commons-daemon</groupId>
<artifactId>commons-daemon</artifactId>
<version>1.0.8</version>
</dependency>
</dependencies>
</project>
assembly POM:
<project>
<parent>
<groupId>com.test</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.app</groupId>
<artifactId>assembly</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/descriptor.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
And finally, the assembly descriptor:
<assembly>
<id>distribution</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>dir</format>
</formats>
<moduleSets>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>com.test.app:module1:jar</include>
</includes>
<binaries>
<outputDirectory>module1</outputDirectory>
<unpack>false</unpack>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</binaries>
</moduleSet>
<moduleSet>
<useAllReactorProjects>true</useAllReactorProjects>
<includes>
<include>com.test.app:module2:jar</include>
</includes>
<binaries>
<outputDirectory>module2</outputDirectory>
<unpack>false</unpack>
<dependencySets>
<dependencySet>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</binaries>
</moduleSet>
</moduleSets>
</assembly>
As you can see, assembly is bind to package phase. So, when I execute
mvn package
from parent directory, I have the following assembly
module1/
commons-cli-1.2.jar
commons-daemon-1.0.8.jar
module1-1.0.jar
module2/
commons-cli-1.2.jar
commons-daemon-1.0.8.jar
module2-1.0.jar
Basically, the problem here is that module1 does not depend on commons-daemon, but the assembly plugin has included the dependence. Similarly, with module2 and commons-cli.
Can someone explain why the assembly plugin behaves this way?
An what would be a solution?
I've always had similar experiences using the assembly plugin with multi-module projects where the end result wasn't what I expected. I hope someone else can provide a more accurate answer as to why that's happening and how best to use those two concepts in tandem.
That said, a possible work-around would be to have module1 and module2 generate their own assembly artifacts which contain their respective jars and dependencies. Then you can modify the assembly sub-module pom file to have dependencies on the generated distribution artifacts from its sibling modules and then unpack those into a new assembly.
In both Module1 and Module2's pom files you can add an assembly plugin configuration to your package phase much like you did with the assembly sub-module.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/main/assembly/descriptor.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
Module1 would have a src/main/assembly/descriptor.xml like this
<assembly>
<id>distribution</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>module1</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
And Module2 will have a similar src/main/assembly/descriptor.xml
<assembly>
<id>distribution</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>module2</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
Then in the assembly/pom.xml you would add the Module 1 and 2 zip artifacts as dependencies
<dependencies>
<dependency>
<groupId>com.test.app</groupId>
<artifactId>module1</artifactId>
<version>1.0</version>
<type>zip</type>
<classifier>distribution</classifier>
</dependency>
<dependency>
<groupId>com.test.app</groupId>
<artifactId>module2</artifactId>
<version>1.0</version>
<type>zip</type>
<classifier>distribution</classifier>
</dependency>
</dependencies>
...and trim up the assembly/src/main/assembly/descriptor.xml file to look like this
<assembly>
<id>distribution</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>dir</format>
</formats>
<dependencySets>
<dependencySet>
<useTransitiveDependencies>false</useTransitiveDependencies>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
Like I said this would be one possible work around and unfortunately adds a significant amount of additional XML configuration to your build process. But it works.

Resources