Include spring xml in jar by maven-assembly-plugin packaging - maven

I am trying to create an executable jar with all of its dependency along with the spring's configuration xml file.
Here is the definition of the maven-assembly-plugin in the pom.xml:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>service.coucou.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
This is the assembly.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<includes>
<include>/src/main/resources/cxf.xml</include>
</includes>
</fileSet>
</fileSets>
</assembly>
I am trying to include /src/main/resources/cxf.xml file in the created jar. But it is not including the xml. How can I include it in the jar?

Replace the <fileSets/> with:
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>cxf.xml</include>
</includes>
</fileSet>

Related

maven-assembly-plugin creates zip with no files

maven-assembly-plugin generate empty zip archive, but source folder is not empty.
Plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.my.group</groupId>
<artifactId>maven-assemblies</artifactId>
<version>${bigdata-assemblies.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>airflow-dags</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>airflow-dags-assembly</descriptorRef>
</descriptorRefs>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
My airflow-dags-assembly.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>airflow-dags</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/airflow-dags</directory>
<outputDirectory>/</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>target/main/airflow-dags</directory>
<outputDirectory>/</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
</assembly>
I have files in src/main/airflow-dags and in target/main/airflow-dags, but I keep getting empty zip file:
Try adding includes to the fileSets as described in the assembly documentation.
<fileSet>
<directory>src/main/airflow-dags</directory>
<outputDirectory>/</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>**/*</include>
</includes>
</fileSet>
Adjust the file pattern as needed.

maven include testOutputDirectory in the jar

Want to add the testoutputdirectory files in the project jar. Tried using below but, the test classes were not included in the jar
What need to be changed to include the same in the project jar?
Using below in the pom.xml
<build>
<finalName>${project.name}</finalName>
<testOutputDirectory>test/classes</testOutputDirectory>
<resources>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${final.name}</finalName>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptor>./src/main/resources/assembly/assembly.xml</descriptor>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
.........
</build>
Below I have in the assembly.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">
<moduleSets>
<moduleSet>
<sources>
<fileSets>
<fileSet>
<directory>.</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>test/classes/**/*.class</include>
</includes>
</fileSet>
</fileSets>
</sources>
</moduleSet>
</moduleSets>
</assembly>
I dont think it is a good idea to do that at all. Maybe think about to create a separate test-jar. You can do it with the maven-jar-plugin test-jar goal
Nonetheless, if you really want to do that, you can achieve it by setting the
testOutputDirectory like:
<build>
<testOutputDirectory>${project.build.directory}/classes</testOutputDirectory>
</build>
The Tests.java are then compiled to target/classes and from there the maven-jar-plugin packages them to the jar.
Thank you all for your help.
I was able to include the testoutputdirectory to the jar using the below.
<build>
<finalName>${project.name}</finalName>
<testOutputDirectory>test/classes</testOutputDirectory>
<resources>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>${final.name}</finalName>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/assembly/assembly.xml</descriptor>
</descriptors>
<!-- descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs-->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
.......
</plugins>
..........
</build>
<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>jar-with-dependencies</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>./target/classes/</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
</fileSet>
<fileSet>
<directory>.</directory>
<outputDirectory></outputDirectory>
<includes>
<include>test/classes/**/*.class</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>

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>

Maven package zip file structure

I package zip using maven-assembly-plugin. After finished package, the zip file structure as follow:
test.zip
|--test
|--test.jar
|--test.dalp
My assemble.xml and pom.xml configured as follow:
assemble.xml
<assembly>
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>/</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>test.dalp</include>
</includes>
</fileSet>
<fileSet>
<directory>/target</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>test.jar</include>
</includes>
</fileSet>
</fileSets>
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
I want to know how to configure assemble.xml and pom.xml, and after I run "mvn clean package" in command line the zip file structure will be as follow:
test.zip
|__test.jar
|__test.dalp
You could change the configuration into the following:
<assembly>
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>/</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>test.dalp</include>
</includes>
</fileSet>
<fileSet>
<directory>/target</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>test.jar</include>
</includes>
</fileSet>
</fileSets>

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