Maven Create Jar and exclude Files - maven

I am trying to create a runnable Jar file. I have to create a Jar of only one single java class (MyClass which is under the folder structure src/main/java/org/miso/mcs/util/MyClass) and exclude others. I am able to create to JAR but it has everything.
I am using mvn clean package assembly:single command to create Jar.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.miso.mcs.util.MyClass</mainClass>
</manifest>
</archive>
<resources>
<resource>
<classesDirectory>src/main/java/org/miso/mcs/util</classesDirectory>
<includes>
<include>/*MyClass*</include>
</includes>
<excludes>
<exclude>/*</exclude>
</excludes>
</resource>
</resources>
</configuration>
</plugin>

You won't be able to do that by using the predefined jar-with-dependencies descriptor. However, we can make our own assembly descriptor for that:
<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>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>true</unpack>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<useProjectAttachments>true</useProjectAttachments>
<includes>
<include>${project.groupId}:${project.artifactId}:jar:classes:${project.version}</include>
</includes>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>%regex[org\/miso\/(?!mcs\/util\/MyClass).*]</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>
unpackOptions allows to control the content the unpacked dependencies in the JAR. In this case, we are excluding everything under the org.miso package (which I assume are the classes of your project) and we're only including the class you're interested in (org.miso.mcs.util.MyClass). This will result in a runnable JAR with only the specified class of your project.
Since your project is a WAR, it is a little more complicated. The first dependency set declares all the dependencies of the project and unpacks them. The second only uses the projects attachments and includes only the classes attachment built by the maven-war-plugin.
If you save that snippet into a file called assembly.xml located under src/assembly, your POM will then look like:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>org.miso.mcs.util.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
With such a configuration, you just need to invoke Maven with mvn clean install and it will correctly generate the JAR. Then, you can launch that JAR with java -jar name-of-jar.jar.

Related

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>

Annotated class not found in jar file generated by maven-assembly-plugin

I'm using the maven-assembly-plugin to generate a jar file with a single class inside. When I try to deploy my jar to JBoss server I receive an error: No annotated classes found in the jar:. I've decompiled the class file from the jar and it is annotated. My only guess is for some reason the maven-assembly-plugin is causing an issue. If I generate the jar from a command line and inserting the same .class file the deployment will work.
snippet of the plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<inherited>false</inherited>
<executions>
<execution>
<id>make-RC04InquirySvc-v1.jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<runOnlyAtExecutionRoot>true</runOnlyAtExecutionRoot>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<finalName>RC04InquirySvc-v1</finalName>
<descriptors>
<descriptor>assembly-RC04InquirySvc-v1.xml</descriptor>
</descriptors>
<archive>
<manifestEntries>
<Implementation-Vendor>EDS</Implementation-Vendor>
<Implementation-Title>Agile Card Framework</Implementation-Title>
<Implementation-Version>6.0</Implementation-Version>
<Built-Date>${maven.build.timestamp}</Built-Date>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
And the assembly file:
<assembly>
<id>acf-bundle</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/classes/com/eds/agilecard/wsdl/rc04Inquirysvc_v1</directory>
<outputDirectory>/com/eds/agilecard/wsdl/rc04Inquirysvc_v1</outputDirectory>
<includes>
<include>**/ServiceEndpointImpl.class</include>
</includes>
</fileSet>
</fileSets>
</assembly>

Package Dll in Jar using Maven- single goal

I have added a DLL in my maven project as dependency like this :
<dependency>
<groupId>com.test.dll</groupId>
<artifactId>myDll</artifactId>
<version>0.1</version>
<type>dll</type>
</dependency>
When I try to execute maven:install
It is giving me this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-
beta-5:single (jar-with-dependencies) on project testApp: Failed to create
assembly: Error adding file-set for 'com.test.dll:myDll:dll:0.1' to archive: Error
adding archived file-set. PlexusIoResourceCollection not found for: C:\Users\USER\.m2
\repository\com\test\dll\myDll\0.1\myDll-0.1.dll: No such archiver: 'dll'
What Am I doing wrong here??
Update
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<keystore>src/main/keystore/mykey.keystore</keystore>
<alias>aliasname</alias>
<storepass>passw0rd</storepass>
<verify>true</verify>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The problem here is the jar-with-dependencies descriptor. The descriptor unpacks all dependencies into a directory and packages this directory into a new JAR file. However, it cannot unpack a DLL file (that's the "No such archiver" error message). To get this working, you need to define your own assembly descriptor:
<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>assembly-with-dll</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<!-- package the regular dependencies -->
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<!-- exclude the DLL -->
<excludes>
<exclude>com.example:my-dll</exclude>
</excludes>
</dependencySet>
<!-- package the DLLs -->
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<include>com.example:my-dll</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
Provided, that the descriptor above resides in src/main/assembly, the configuration of the maven-assembly-plugin looks as follows:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
There is a piece of information here : Maven Dll Dependency Problem.
To solve this issue, exlude dll from your assembly :
<excludes>
<exclude>*:dll*</exclude>
</excludes>
Last time I had to create a executable jar with dependencies, I put them out of the jar, in a lib directory.
DLL must be :
either in application classpath (like server/lib for a server)
or in the OS classpath (C:\Windows\system32 for instance)
After reading your pom and dependecy file set, I may be able to be more accurate :)
To add to Stefan's answer, I don't think you want to do a jar-with-dependencies packaging for this project of yours. You should look at using one of the bin packaging (like .zip or tar.gz)

maven assembly dir with a jar dependency from project

I am trying to use maven to assemble a directory for a install package. The install package requires several things: configuration files, dependency libraries and an executable jar of my class files. I am having trouble figuring out how to add the executable jar.
Here is what I have so far:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myproject</artifactId>
<version>8.1.1</version>
<name>my project</name>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<configuration>
<descriptor>${basedir}/src/main/assembly/client.xml</descriptor>
</configuration>
<executions>
<execution>
<id>create-client</id>
<configuration>
<descriptor>${basedir}/src/main/assembly/client.xml</descriptor>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
.
.
.
</dependencies>
</project>
Then my assembly descriptor looks like this:
<assembly>
<id>client</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/reports</directory>
<outputDirectory>/reports</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/resources/audio</directory>
<outputDirectory>/audio</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/client</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
If i call mvn assembly:assembly I get a directory with all the libraries and configuration files. Works great.
But now I want to add an executable jar of all my compiled code. I can create this jar alone by adding this to my pom and calling mvn jar:jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>run</finalName>
<archive>
<manifest>
<mainClass>com.myproject.main.StartProcess</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
The question is, how can I get that jar into my assembly automatically?
I have tried a couple things but I have no idea where to start. Can I somehow call the jar execution from my assembly?
Any help appreciated.
I think you should turn your useProjectArtifact to true (this is the default value).
It determines whether the artifact produced during the current project's build should be included in this dependency set.

How can I use Maven to build a jar with required libraries in a sub-folder (like Eclipse)

In Eclipse, you can create a project jar with its required dependencies in an adjacent sub-folder by doing ...
Export->Java->Runnable JAR file
Select Library handling option: Copy required libraries into a sub-folder next to the generated JAR
Is there a way to do this with the Maven assembly plugin? Or is there another Maven plugin that would be more appropriate for this task?
Thanks!
yes you can use assembly plugin.
pom.xml:
<build>
<!-- final name set the jar name, if left it
will give defualt "${artifactId}-${version}" -->
<finalName>jar final name</finalName>
<sourceDirectory>src</sourceDirectory>
<!-- compiler plug in -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- assembly plugin -->
<!-- the assembly plugin is used to define your
final deploy output (jar, zip, dir, war, etc..)-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assembly:package</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<!-- The filename of the assembled distribution
file defualt ${project.build.finalName}-->
<finalName>${project.build.finalName}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<!-- A list of descriptor files path to generate from-->
<descriptors>
<descriptor>assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<!-- jar plug in -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
<!-- to create a class path to your
dependecies you have to fill true in this field-->
<addClasspath>true</addClasspath>
<!-- if you put your dependencySet/outputDirectory
in the assembly file is in a specific folder (lib for example),
you need to add classpathPrefix-->
<classpathPrefix>lib/</classpathPrefix>
<!-- if you defined your dependencySet/outputFileNameMapping
in the assembly, instead of using the classpathPrefix,
you should use customClasspathLayout,
add the classpathPrefix at the begining of the
customClasspathLayout, and then add the pattern of the outputFileNameMapping,
NOTICE YOU NEED TO ADD '$' BEFOR OF EACH '$'.
supported only from version 2.3>-->
<!--<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>
lib/$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension}
</customClasspathLayout>-->
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
assembly.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!--the id will be add to the end of the distribution file -->
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>icons</directory>
<outputDirectory>icons</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
<fileSet>
<directory>conf</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
<files>
<!-- you need to create the bat file yourself -->
<file>
<source>batFileName.bat</source>
<filtered>true</filtered>
</file>
</files>
<dependencySets>
<dependencySet>
<!--define the outputDirectory of the dependencies,
NOTICE: if it's diffrent from '/' make sure to
change the classPath configuration for
the maven-jar-plugin in the pom-->
<outputDirectory>lib</outputDirectory>
<!-- maping the dependencies jar names.
NOTICE : if you used this definition, you need to use
customClasspathLayout classPath configuration
for the maven-jar-plugin in the pomg-->
<outputFileNameMapping>
${artifact.groupId}.${artifact.artifactId}.${artifact.extension}
</outputFileNameMapping>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
It's not exactly what you want but if you use the war:war goal (with -DfailOnMissingWebXml=false) it will put the dependencies in the WEB-INF/lib directory in your target folder.
Alternatively check out the dependency plugin.

Resources