maven include testOutputDirectory in the jar - maven

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>

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.

Spring-Boot External lib folder with Maven

I need externalize my application's dependencies. I want to achieve the following layout for my Spring Boot Application
.
├── *.properties
├── static
├── main.jar
└── lib
└── *.jar
Where lib folder contains my dependencies.
I try with the maven-jar-plugin and spring-boot-maven-plugin but I get the following error:
Could not find or load main class com.Application
My pom.xml
<profile>
<id>dist</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<finalName>alfred</finalName>
<excludes>
<exclude>src/main/resources/*</exclude>
<exclude>*.properties</exclude>
<exclude>*.html</exclude>
<exclude>**/assembly/*</exclude>
<exclude>**/lib/*</exclude>
<exclude>**/static/*</exclude>
<exclude>**/templates/*</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<Main-Class>com.Application</Main-Class>
</manifest>
<manifestEntries>
<Class-Path>./alfred_lib/**</Class-Path>
<Class-Path>./**</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.2.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-1</version>
<configuration>
<descriptor>src/main/resources/assembly/dist.xml</descriptor>
<finalName>alfred</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
My src/main/resources/assembly/dist.xml:
<assembly>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources/assembly</directory>
<outputDirectory></outputDirectory>
<includes>
<include>**.sh</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
<!-- Estáticos -->
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory></outputDirectory>
<excludes>
<exclude>lib/**</exclude>
<exclude>assembly/**</exclude>
</excludes>
</fileSet>
<!-- Aplicación -->
<fileSet>
<directory>target</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<!-- Dependencias -->
<dependencySets>
<dependencySet>
<outputDirectory>alfred_lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
I've been reading for several days and I have discarded:
Using spring-boot-thin-layout
Create far jar: It's a project requirement.

How can a jar with specific deps be created, after using maven-dependency-plugin to select the wanted deps?

My goal is to create a jar with specific dependencies from my dependency list in the pom. I'm using maven-dependency-plugin like so:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<includeScope>runtime</includeScope>
<excludes>META-INF/*.SF,META-INF/*.DSA,META-INF/*.RSA</excludes>
<outputDirectory>${project.build.directory}/uber-deps/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.some.blaClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
and an assembly.xml file holding:
<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>plugin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<include>
${project.build.directory}/uber-deps/
</include>
</includes>
<excludes>
<exclude>*:sources</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
After mvn clean install all relevant dependencies appear in target/uber-deps as I would expect. My problem is with the next plugin under <plugins> - maven-assembly-plugin. Seems to me as if it doesn't take uber-deps in.
I know this only by trying to unpack the jar using jar xf to see if the deps in uber-deps were packed in the jar created after mvn clean install.
What should be changed?
1)
The jar you are building as part of the assembly-plugin will be called (by default) ./target/<artifactId>-plugin.jar
Note that the plugin part is what you've put under id in your assembly xml file.
2)
Since you already unpack the dependencies to a folder, you should use fileSets rather then dependencySets:
<fileSets>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>
${project.build.directory}/uber-deps/
</include>
</includes>
</fileSet>
</fileSets>
</fileSets>
3)
BTW if you want the outputs of your own project in that jar you should add another fileSet:
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>
${project.build.outputDirectory}
</include>
</includes>
</fileSet>
4) Also just noted that your assembly plugin definition is not stating the location of your assembly xml file and that you try to define mainClass using shade-plugin configuration. This is how it should look in assembly plugin (assuming that your assembly file is located under src/assembly/plugin.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>src/assembly/plugin.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.some.blaClass</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>

do not create meta-inf/maven in jar package when using maven assembly plugin

I have one project that using maven assembly plugin for merging two jar,without using maven assembly ,create jar package have meta-inf/maven that contain pom.properties and pom.xml . but after merge by Assembly this folder not exists.
i search it and found that this can make that OK but dose not work .
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>src/assembly/frm-assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
<archive>
**<addMavenDescriptor>true</addMavenDescriptor>**
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
and descriptor is this
<assembly>
<id>uberjar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/webapp</directory>
<outputDirectory>/META-INF/resources</outputDirectory>
</fileSet>
</fileSets>
</assembly>

Assembly ID is getting appended when zip file is created by maven

xml and pom.xml which creates jar file and then zip file with some artifacts including jar file. But when i run maven install zip file is getting created as GenerateMissingUsersReport-bin.zip instead I want it to create as GenerateMissingUsersReport.zip. I have set as false. But no difference.
Any pointers?
Here is dep.xml
<id>bin</id>
<baseDirectory>../</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<includes>
<include>plugin.xml</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
Here is pom.xml
<finalName>GenerateMissingUsersReport</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>GenerateMissingUsersReport</finalName>
<appendAssemblyID>false</appendAssemblyID>
<descriptor>src/assembly/dep.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>plugin.xml</exclude>
</excludes></configuration>
</plugin>
Add the following line under configurations element of the pom
<appendAssemblyId>false</appendAssemblyId>
Also make sure you use the latest version of the assembly plugin
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>

Resources