How to call a descriptor from inside another descriptor in maven assembly plugin? - maven

I want to create my own descriptor file in maven which will setup a zip file with a current directory structure. I would like for the jar of my project to be generated with dependencies which can be done by the built-in descriptor jar-with-dependencies.
My question is how can I tell my descriptor to call the jar-with-dependencies descriptor and include the generated jar instead of the normal jar?

You can configure sequence of maven assembly plugin executions:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</execution>
<execution>
<id>package-distribution-zip</id>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>bin</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

Related

Error: Could not find or load main class while executing jar

I have used maven assemble plugin to generate executable jar with dependencies.
I am trying to run the jar and getting Error: Could not find or load main class.
I have tried with and also tried option to specify classes folder.
Here is my pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.abc.TestApplication</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Try to use configuration block inside of an execution one:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.abc.TestApplication
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

mvn output jar file is executable only inside target folder

I have a mvn built jar, which is working fine inside the target folder.
But if I copy the jar to different folder/machine.I get a class not found exception. I understand the jar is unable to access the dependencies. how can i fix this?
my pom contains,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.outputDirectory}/dependency/
</outputDirectory>
</configuration>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.Main</mainClass>
<classpathPrefix>dependency/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Thanks In Advance,
Saranya
Make a fat jar with dependencies using maven-assembly-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The target folder would have additional jar-with-dependencies.jar; you should be able to use this anywhere.
I removed the maven jar and dependency plugins from my pom. Added
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.importer.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>xyz</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance
merges -->
<phase>package</phase> <!-- bind to the packaging phase
-->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This created a jar with dependencies which can be executed anywhere. Also with the specific jar name.

How to avoid WARNING message "already attached to project, ignoring duplicate"

I've tried to avoid the maven warning message which comes from assembly plug in (I compile 2 jar files which are compiled from the same module)
Part of my pom.xml :
maven-jar-plugin
2.3.1
default-jar
none
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<executions>
<execution>
<id>MockFX</id>
<configuration>
<finalName>MockFX</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>mock.DSlauncher.FX.FX_DataSourceLauncher</mainClass>
</manifest>
</archive>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>MockFutures</id>
<configuration>
<finalName>MockFutures</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>mock.DSlauncher.Futures.Futures_DataSourceLauncher</mainClass>
</manifest>
</archive>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
During the compilation process I'm getting the next WARNING msg :
[WARNING] Artifact com.my.company:UAT-Mock:jar:jar-with-dependencies:1.0 already attached to project, ignoring duplicate
What should I add to pom.xml in order to avoid the warning ?
Thanks
Just add <attach>false</attach> to the configuration of both executions

Failed to build multiple assemblies in maven. No assemby descriptor found

I'm having some trouble when trying to generate two or more jars from a maven project when using the assembly plugin.
I have the following maven pom.xml file (see below).
However, when I run mvn clean compile assembly:single,
I get the following error:
Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default-cli) on project hyuga: Error reading assemblies: No assembly descriptors found. -> [Help 1]
What am I missing?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>dg2cep</id>
<configuration>
<archive>
<manifest>
<mainClass>br.pucrio.inf.lac.konoha.hyuga.core.Bootstrap</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>dg2cep</finalName>
</configuration>
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>playback</id>
<configuration>
<archive>
<manifest>
<mainClass>br.pucrio.inf.lac.konoha.hyuga.others.csv.CSVPlayback</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>playback</finalName>
</configuration>
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Ah..you are executing it like this: mvn ... assembly:single ? If so don't do this.. use it within the life cycle like mvn clean package ....

Can maven assembly produce a file without the format suffix

I've managed to get maven assembly to produce a custom NAME-assemblyId.tar.gz, but I actually want to name the final result just NAME-assemblyId, without the format suffix.
No combination of finalName and appendAssemblyId can fix this it seems?
Currently I'm using a copy mojo to rename it after packaging, but that is not ideal, I guess I could also use a attach-artifact mojo to attach them though.
Set appendAssemblyId to false in your assembly configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>jar-with-dep</id>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>com.activee.migration.cli</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>cli-zip</id>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/main/assembly/cli_zip.xml</descriptor>
</descriptors>
<finalName>com.activee.migration.cli</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Resources