How to define the order of executions using maven-assembly-plugin - maven

I am working on the migration of a project from Ant to Maven. The final distribution I need to deliver is a zip containing an executable jar with all its depencencies. Here is part of my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<finalName>ProjectDistribution</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fullQualifiedNameToMainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>dist</id>
<phase>assembly</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
And here is the assembly file:
<assembly>
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<!-- 1st approach-->
<!--files>
<file>
<source>
/target/ProjectDistribution.jar
</source>
<outputDirectory>/</outputDirectory>
</file>
</files-->
<fileSets>
<!-- 2nd approach-->
<!--fileSet>
<directory>/target</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet-->
<fileSet>
<directory>/HelpFiles</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
</fileSets>
I run 1.- mvn compile, 2.- mvn package, and 3.- mvn assembly:single
The problem I am dealing with is that
It does generate the jar with all the dependencies and it does generate the zip but it does not includ the jar in the zip. I pretty much need to figure out a way of making the assembly first generate the jar and wait until it is created (because its size is 5 MB) and then create the zip. Right now the 1st and 2nd approaches -from the assembly file- are commented out, however, I have used both and none of them seem to work.
Any help will be greatly appreciated!
Eric

To get this working you need to split the <configuration> and put it into the two plugin executions:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>ProjectDistribution</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fullQualifiedNameToMainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</execution>
<execution>
<id>dist</id>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The first execution will create the jar file. The second execution will take that JAR file and
put it into the ZIP file together with the other files. With this configuration, you can just execute mvn verify or mvn install to create the assembly.
There are two other things to consider:
You should use the verify phase to build your assembly because the jar-with-dependencies descriptor includes the project artifact itself. During the package phase the project artifact will not be ready for packaging
The jar-with-dependencies descriptor has very limited capabilities to create a JAR file with all dependencies. You should use the maven-shade-plugin instead.

You are mixing the pre-defined jar-with-dependencies with a custom zip descriptor. You would normally want one of them - not both.
It looks like you want a zip which contains your project artifact along with its dependencies. For this you would not need to create a jar-with-dependencies. If, however, you do want a single executable jar with all the dependencies in it, then it is not clear why you need to zip it again.

Related

Include only certain packages in a Maven generated non-runnable jar

I'm trying to build a non-executable jar file that only includes certain packages from my project to use as a library for common code. I'm planning on deploying that to Nexus and including it in other projects.
I can make it manually in eclipse by right clicking on the project, selecting Export, selecting JAR file (NOT Runnable Jar File) then checking the packages that I want to include. I'm trying to mimic this behavior in Maven. I have been able to replicate the non-runnable part in maven by not including a main in the manifest, but still have to find out how to include only certain packages.
This project actually builds 2 jar files already (from 2 different main's in the program - a test GUI and the regular application). I'm having trouble finding information on how to do the non-runnable jar and selecting certain packages only.
Here is the existing build portion of my pom file is below. Build-third is the non-runnable jar:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-first</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.joy.fb20.da.exec.jDaExecutive</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>DaExecutive</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>build-second</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.joy.fb20.gui.jExampleAppGui</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ExampleAppGui</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<execution>
<id>build-third</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ddslib</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</plugin>
</plugins>
</build>
Tried this myself and it is possible.
Create a new maven profile
Add pluginManagement section to that new profile for maven-jar-plugin and if needed for maven-resources-plugin also
Use this pluginManagement to configure inclusions/exclusions of sources/resources and override artifacts final name here to have different name for that separate jar
Example
<profiles>
<profile>
<id>subset</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<finalName>subset</finalName>
<includes>
<include>org/example/stuff/**</include>
</includes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
Using this profile maven creates subset.jar that has everything under org/example/stuff/**
See more about excluding: How to exclude a set of packages from maven build jar?
Of course it might be possible to do all this also without pluginManagement but in my opinion pluginManagement provides clean & flexible way to configure this kind of stuff so i recommend that.
You should be able to do that by using a custom assembly descriptor and specifying your fileset and / or file includes / excludes. See https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_fileSet

Create a tar with maven generated jar with dependenceis and few other files

I'm new to maven and its interesting subject to learn.
Succeed to create a jar with dependencies, using:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
Now, i have to include few shell scripts and generated jar to a tar.
To achieve this, i ve tried the following way:
Added
<descriptors>
<descriptor>hadoop-job.xml</descriptor>
</descriptors>
to the above script.
In hadoop-job.xml i'm including required files into tar.
The problem is tar is generated first and says no *.jar found in target.
Is there a way to schedule jar creation first and tar next, since both the configurations reside in assembly plugin.
OR
Is there a command to execute and generate a jar first and then a command to generate a tar ?
By the way i'm executing mvn clean assembly:assembly -Dbinary=true.
Help me to resolve. Thanks.
Here is an example of assembly building the jar-with-dependencies and after that a tar that includes this jar-with-dependencies (built by the maven-assembly-plugin just before). In the tar I also includes the shell script files in .sh from the folder src/main/bin.
First, the plugin configuration :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorId>jar-with-dependencies</descriptorId>
</descriptorRefs>
</configuration>
</execution>
<execution>
<id>all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/all.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/assembly/</outputDirectory>
</configuration>
</plugin>
The order of the executions is important because if I want to include the jar-with-dependencies into my tar I need to have it built before.
I put all the assemblies in the same folder, that's why I have a global configuration tag additionally to the configuration tag of the executions
And then the content of my assembly file all.xml :
<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>all</id>
<formats>
<format>tar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**/*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>755</fileMode>
</fileSet>
</fileSets>
</assembly>
The includeBaseDirectory is here to avoid my tar containing the root folder with the name of the project with the version.
If you don't specify the outputDirectory tags, the folder structure into your archive will begin from the root of the project, for example it will include your jar with the path target/your-project-1.0-SNAPSHOT.jar.
EDIT : The plugin configuration that I did before could be simplified with just the following :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorId>jar-with-dependencies</descriptorId>
<descriptors>
<descriptor>src/assembly/all.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/assembly/</outputDirectory>
</configuration>
</plugin>
The reason is because in the maven-assembly-plugin the descriptorId is read before the descriptors so that's good for our case but if not, we would need two executions as done higher with care about the order.
The same attention have to be done for the order of the descriptors, they are performed in the reading order (it can seems logic but I think it might be useful to indicate it).

Packing one tar created using maven assembly into another tar using maven assembly

I have created a tar(say project.tar) using maven assembly descriptor.
I need to create one more tar(say final.tar) file which would contain the previously created tar (project.tar) along with one script file.
To do this 'm trying using two descriptors specified in pom.xml. One descriptor is for project.tar and second for final.tar .
While doing so I'm facing the following error.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.4:single (default-cli) on project ede: Failed to create assembly: Error creating assembly archive bin: A tar file cannot include itself. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.4:single (default-cli) on project ede: Failed to create assembly: Error creating assembly archive bin: A tar file cannot include itself. "
My pom.xml file is
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/src.xml</descriptor>
<descriptor>assembly/final.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
...
I've also tried using pom.xml file as
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/src.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<id>bin</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/final.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
<finalName>ede2</finalName>
</configuration>
</execution>
</executions>
</plugin>
In this way, it's not able to locate the descriptors files.
Could anyone please guide me if 'm doing anything wrong?
I know that this post is very old but I think it might need some explanations.
In my opinion, the best way for what you want to do is to use two executions of the maven-assembly-plugin. The first will generate your project tar and the second you final tar. (please read all the answer before saying that there is more simple)
Here is what I would do (and it works of course).
As I like to have the assemblies in different folders (because I think it is easier like that), I declare properties in my pom with the directories paths :
<properties>
<myproperties.assembly.project-tar.dir>${project.build.directory}/assembly-project/</myproperties.assembly.project-tar.dir>
<myproperties.assembly.final-tar.dir>${project.build.directory}/assembly-final/</myproperties.assembly.final-tar.dir>
</properties>
Then in the maven-assembly-plugin I configure the executions (never forget that the order of the executions is very important because they will be executed in the declaration order if they are binded on the same phase) :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>project-tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>${myproperties.assembly.project-tar.dir}</outputDirectory>
<descriptors>
<descriptor>src/assembly/project-tar.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>final-tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<outputDirectory>${myproperties.assembly.final-tar.dir}</outputDirectory>
<descriptors>
<descriptor>src/assembly/final-tar.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
(you can see that the tars will be in different folders and that the project tar will not have the assembly id appended to its name so it will look like myproject-1.0.tar)
Now concerning the assemblies definitions.
For the project-tar.xml, I just include the jars in it, you can do whatever you want :
<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>project</id>
<formats>
<format>tar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
And for the final-tar.xml, I include the project tar and the scripts I have in my folder src/main/scripts :
<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>final</id>
<formats>
<format>tar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${myproperties.assembly.project-tar.dir}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.tar</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/scripts</directory>
<outputDirectory></outputDirectory>
<fileMode>755</fileMode>
</fileSet>
</fileSets>
</assembly>
And there we are, you have your two tars.
There is a solution more simple which is as you did at the beginning :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<executions>
<execution>
<id>project-tar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/project-tar.xml</descriptor>
<descriptor>src/assembly/final-tar.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
The descriptors order is very important of course.
The only difference with your initial solution is that I keep the property appendAssemblyId set to true because if not, the two assemblies will have the same name and when the maven-assembly-plugin will try to build the final assembly it will detect that there is a file which is already having this name and will fail saying that it cannot include itself...
With my solution in two executions, you can set the appendAssemblyId to false.
Part of the answer is to use the descriptorRef as outlined in the assembly plugin examples.
Hopefully that will give you something to get started with.

can maven assembly plugin picking the specify java package?

Suppose we have following packages in our project
org.xxx.dao, org.xxx.service, org.xxx.service.impl
all the package are in ther source folder src/main/java
can i assembly packages into seprated jars,(xxx-dao.jar,xxx-service.jar,xxx-service-impl.jar)?
You can use the standard maven-jar-plugin for this:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>dao</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>dao</classifier>
<includes>
<include>org/xxx/dao/**/*</include>
</includes>
</configuration>
</execution>
<execution>
<id>service</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>service</classifier>
<includes>
<include>org/xxx/service/**/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
This gives you one JAR with everything and then several JARs with only the selected content which you can select with a classifier in your Maven POMs.
If you want to omit the default JAR (which contains everything), then you replace one <id> with <id>default-jar</id> and add an <excludes> element because the default JAR includes everything, so you have to get rid of anything that you don't want.
maven-assembly plugin is not intended for this kind of tasks, see its features. If you have the source codes, then I would suggest copying them into a module of your project.
But if you want to do it with maven-assembly plugin, then I think you can do it by creating separate descriptor components and defining which files to include in which assembly. You can define source files to be included in includes tags under sources definition.

maven create zip with jar and some more files

I do not understand maven. Better use ant, but... I've managed to create jar (with, or without dependencies), I've managed to copy bat runner script close to jar but now i want to create zip with this jar and this bat. So i use assembly plugin and get BUUUM!!!! CADAAAM! In my configuration it happens so, that it executes parallel to jar packaging. I wrote assembly file:
<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>jg</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/123</outputDirectory>
<excludes>
<exclude>assembly/**</exclude>
<exclude>runners/**</exclude>
</excludes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
Then, I bound maven-assembly-plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<inherited>false</inherited>
<configuration>
<archive>
<manifest>
<mainClass>by.dev.madhead.lzwj.Main</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/assembly/assembly.xml</descriptor>
<!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
Now I get this in ./target:
runner.bat
jar_without_dependencies.jar (it is from maven-jar-plugin, right?)
jar_without_dependencies.jar
And the third angers me. It contains:
And the 123 directory contains:
As you see, I get jar with unpacked dependencies, EXCLUDED DIRS!!!!, and with dir 123, which is actually what I want (Oh! assembly plugin did that!!!).
I want to get jar with dependencies and correct manifest with classpath. As an option i want jar with unpacked dependencies (I know about <unpack>false</unpack> in assembly, but cannot get it work). I want to change /123 to / and get NORMAL JAR WITHOUT EXCLUDED FILES!!! I want two separate tasks to build jar and zip (is it done with profiles in maven??) As in ant, i would wrote something like this:
<target name="jar_with_deps" depends-on="compile">
<jar>
here i copy classes (excluding some dirs with runner script), and build manifest
</jar>
<copy>
copy bat file from src/main/resources/runner/runner.bat
</copy>
</target>
<target name="zip" depends-on="jar_with_deps">
<zip>
Get jar from previous target, get runner.bat. Put them in zip
</zip>
</target>
Excuse me, if I am too expressive, but I am really angry with this implicit behavior. I am really stuck with this.
Just in case it helps anyone else, I found that this was pretty easy to do, at least for my basic needs. I was already using the Maven Shade plugin to build a jar with all dependencies included:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<configuration></configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
So when I ran mvn package, it would produce target/MyApp-version.jar, whereas I wanted a MyApp-version.zip containing MyApp-version.jar along with some other files (a README, etc.). So, I add the Assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The above block refers to assembly.xml, which configures the way the plugin works:
<?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>release</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target</directory>
<includes>
<include>MyApp-${app.version}.jar</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<files>
<file>
<source>CHANGES.md</source>
<fileMode>0644</fileMode>
</file>
<file>
<source>LICENSE</source>
<fileMode>0644</fileMode>
</file>
<file>
<source>README</source>
<fileMode>0644</fileMode>
</file>
</files>
</assembly>
(${app.version} is defined in the pom.xml <properties> element.)
That's it, now mvn package produces both the jar and the zip.
You have to options to achieve your goal:
option: Create two assembly descriptors, one for jar w/ deps and one for zip. Zip takes the the newly created jar.
option: Create two more modules in your project: first modules shades all deps into one jar (or attach a shaded jar along with your main jar, save another module), have the second module depend on it and suck in that jar in your assembly. Your done.
Depending on the size and structure of your project, I would go for the safe way: option 2. This one is guaranteed to have the correct build and dep order. Option 1 violates the maven way somewhat.
I've made two profiles in pom.xml:
<profiles>
<profile>
<id>jar-with-dependencies</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<archive>
<manifest>
<mainClass>by.dev.madhead.lzwj.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>distro</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/distro.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Now i am able to create simple jar (mvn clean package), jar with dependencies (mvn clean package -Pjar-with-dependencies). I also can call mvn package -Pdistro to create zip. But i need to call maven with -Pjar-with-dependencies before it manually. Except this, everything is ok.

Resources