Alternative to maven assembly:single for packaging - java

What are the alternatives for packaging a standalone java application apart from creating a big fat assembly, which is horrible in some scenarios as
1) Conflicting files (with same names eg. reference.xml) in resource path of two or more jars get overriden by the lucky one.
2) Replacing a single jar is not possible without extracting and merging and compressing again.
Is there a solution more on the lines of an exploded war file, with all the libs in a lib folder and main class file's jar containing manifest entries.
I am sure i had done that in ant and could surely be done in maven too.

Assembly plugin will be the good choice with a custom descriptor:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
And in the assembly.xml:
<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>bin</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>${project.artifactId}-${project.version}.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
All the dependencies will be seperate jars in the lib directory next to the project jar file.

I tend to use Mojo's AppAssembler Maven Plugin to make a distribution and then use the Maven Assembly Plugin to wrap that all up into a tar.gz or zip file that comprises the distribution... sometimes I will go further and use Mojo's Unix Maven Plugin to create .deb and .rpm installers, and I have heard good things from people who need Windows installers using Mojo's NSIS Maven Plugin though NPanday's WiX Maven plugin should be able to do similar and previously when I needed to generate Windows installers I wrote my own WiX Maven plugin, but the source code for that remains with my previous employers.
If you go down the AppAssembler route, I would suspect you might prefer a repositoryLayout of flat

Related

Adding content files to a Maven artifact

I create an artifact with maven and I want to add some content files to the target consumer beside the jar file. (I want to add some Jenkins scripts, but I want these scripts to get updated when the consumer upgrades into newer version of the artifact).
This is similar to .net nuget, where you can add a content library to the consumer project.
According to #tashkhisi suggestion I'm trying Maven assembly plugin.
The project structure:
> pipline (folder)
>>> file1.groovy (file)
>>> file2.groovy (file)
>>> file3.groovy (file)
> src (folder)
>>> ...
> assembly (folder)
>>> distribution.xml (file)
> pom (file)
In the pom file:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptors>
<descriptor>assembly/distribution.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>trigger-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
The assembly/distribution.xml looes like that:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1">
<id>distribution</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}/pipeline</directory>
<includes>
<include>*.groovy</include>
</includes>
<excludes>
<exclude>file2.groovy</exclude>
</excludes>
</fileSet>
</fileSets>
<files>
<file>
<source>pipeline/file2.groovy</source>
<outputDirectory></outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
I can see in the target folder that two jar files are created:
target/myLib-1.1.0-SNAPSHOT.jar
target/myLib-1.1.0-SNAPSHOT-distribution.jar
But when I try to consume it from another project the pipeline folder with the groovy files is not getting created...
In your configuration you said pipeline is beside src and in your pom.xml you define outputDirectory as <outputDirectory>${basedir}/pipeline</outputDirectory> which is exactly beside src(pipeline is already there!) so if you want to put this pipeline directory beside target jar file in target directory you should modify this configuration to something like this:
<outputDirectory>${basedir}/target/pipeline</outputDirectory>
By the way creating a zip file which contains all the thing that you need in your deployment using assembly plugin is better approach, read the following link:
https://maven.apache.org/plugins/maven-assembly-plugin/
This is not possible.
Adding something as dependency will not add or create any folders in your project.
The consumer will need to add logic to the project to extract files from the dependency and copy them to the right place (probably possible with the Maven dependency plugin).

Pack library and all its dependencies to one folder, but other dependencies in separate folder

I've maven project. I use maven-assembly-plugin to create zip files with all module dependencies packed in it.
Need to create zip with following structure:
/my-libs
/other-libs
To my-libs need to pack dependencies from my-lib depenency + all its transitive dependencies.
TO other-libs need to pack all other dependencies from current maven module.
Basically I need conditionally select target folder:
if (dependency in transitive-dependencies(my-lib))
copy to /my-libs
else
copy to /other-libs
Is it possible to do with maven-assembly-plugin ? Are there any alternative maven plugins to do so?
You need to define two dependencySet in the assembly XML file
<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">
...
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveFiltering>false</useTransitiveFiltering>
<includes>
<include>com.example:dependency1:jar</include>
<include>com.example:dependency2:jar</include>
...
</includes>
<outputDirectory>/my-libs</outputDirectory>
</dependencySet>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveFiltering>false</useTransitiveFiltering>
<includes>
<include>com.example:other1:jar</include>
<include>com.example:other2:jar</include>
...
</includes>
<outputDirectory>/other-libs</outputDirectory>
</dependencySet>
</dependencySets>
...
</assembly>
Docs:
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
I got the impression that you started the wrong way. You say that only some programs need other-libs on the classpath, others not. So these seem not to be "real" dependencies of your project, at most optional ones.
I would suggest to use the my-lib project to create an assembly zip of my-lib with all dependencies. This can be used in all contexts where otherlibs are not necessary.
On the other hand, you can create a second zip from your original project, containing all the libs. This way, you have two different zips for two different purposes, but you do not need to create a directory structure inside the zip.
You can split your dependencies into two modules:
- ModuleX
pom.xml (contain my-libs)
- ModuleY
pom.xml (contain my-libs + other-libs)
Then use maven-dependency-plugin to copy transitives dependencies into respective folder.
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.parent.basedir}/build/other-lib</outputDirectory>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now you each module build will contains exactly only the jars you need, no duplicate INSIDE. (I think we can ignore the duplication of my-libs BETWEENboth build because you don't gonna use both in the same platform anyway)

Project depedencies in Eclipse m2e multiproject vs. package with manifest

I am finishing migrating a standalone multi-project java from Ant to Maven. The structure of my projects is similar to the following:
parent-project
|_ Project A
| |
|_Project B
|_Project C
In parent-project there is no code but I define in the DependencyManager the most common used dependencies and their versions. ProjectA POM uses the depedencies defines in parent-project and ProjectB too and calls methods from ProjectA.
I can perfectly execute my desktop application from Eclipse as a Maven project (Goals: exec:java). The Dependency Hierarchy tab provided by m2e shows the right dependency order and it's the same shown as dependency:build-classpath.
dependency:build-classpath tells Maven to output the path of the dependencies from the local repository in a classpath format to be used in java -cp. The classpath file may also be attached and installed/deployed along with the main artifact.
However, despite my project depedencies seems correct when I launch the standalone application as a Maven main project from Eclipse, it is not the same when I package the jar with its jar dependencies in a sub-directory (created with maven-jar-plugin).
MyApp.jar
|_ \lib
|_ MyApp dependencies
|_ Maven projects jars
|_ Maven projects jars dependencies
My manifest file include a classpath that is the equivalente to execute "mvn dependency:resolve" but I still don't manage to figure out where the order of these jars come from (even changing the POM doesn't seem to have any effect on it). My application manage to run but I have many problem with runtime libraries because it doesn't use the one that it is meant to use.
dependency:resolve tells Maven to resolve all dependencies and displays the version
If there is someone out there that could point out the source of this problem, I would be eternally greatful.
I have found myself the solution after all.
The problem appeared only when invoking maven-dependency-plugin for copying the dependencies. I remove the execution of that plugin and replace the copy-resource with maven-resources-plugin.
Before (not working):
<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>jar-with-dependencies</id>
<formats>
<format>dir</format>
</formats>
<!-- Dependencies properties -->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<!-- To avoid include the target name as a root directory -->
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<!-- Copy resources from resources to target -->
<fileSet>
<directory>../AnotherJavaProject/src/main/resources/utils/</directory>
<outputDirectory>utils</outputDirectory>
</fileSet>
<!-- ...more resources here -->
<!-- Copy the .exe to the target folder -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.exe</include>
</includes>
</fileSet>
</fileSets>
</assembly>
After (working):
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<!-- Copy resources from resources to target --> <directory>../AnotherJavaProject/src/main/resources/utils/</directory>
<targetPath>utils</targetPath>
</resource>
<!-- ...more resources here -->
<!-- Copy the .exe to the target folder -->
<resource>
<directory>${project.build.directory}</directory>
<include>*.exe</include>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Hope this help somebody.

maven, deploy a desktop application

I am switching from ant to maven.
To keep things simple, my application is a swing application that connects to a database.
I want it to be packaged in a jar file.
The application requires an external library, that is Microsoft sql server library, contained in a jar file: sqljdbc4-3.0.jar.
So i tryed to include this library:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
</dependency>
but when launching the jar, i get the exception:
java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
This problem is already documented in other stackoverflow questions; i'm talking of this just to get to the point.
My idea (maybe naive) is the following, i would like to
create a jar with only my code.
create a distribution that contains in a directory (lib) all my libraries.
the manifest file to correctly reference all the libraries.
Searching around, i've found that each one of this issues is performed by a different maven plugin: jar plugin, dependency plugin and assembly plugin.
Is this the way to go? I don't feel it right because i've undesrtood that maven would handle easily "regular" configurations and this solutions of the three plugins seems a bit over-complex.
You need to add these plugins to pom
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
</plugin>
src/main/assembly/assembly.xml
<assembly>
<id>assembly</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
</assembly>
run assembly
mvn clean package assembly:single
This is the maven way. By the way: the jar plugin and the dependency plugin are included always in a maven build and there should be no need to configure them.
Additionally a good help could be the appassembler that creates a distributable including all jar files needed.

Insert a dependent jar into an installer jar

I have a multi-module maven project with an installer sub-project. The installer will be distributed as an executable JAR. It will setup the DB and extract the WAR file to the app server. I would like to use maven to assemble this jar like so:
/META-INF/MANIFEST.MF
/com/example/installer/Installer.class
/com/example/installer/...
/server.war
The manifest will have a main-class entry pointing to the installer class. How might I get maven to build the jar in this fashion?
You can build the jar using the Maven Assembly Plugin.
First, you'll need to add some information to your pom.xml plugins section to make the resulting jar executable:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.example.installer.Installer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I recommend using a separate assembly descriptor to build the actual installer jar. Here's an example:
<assembly>
<id>installer</id>
<formats>
<format>jar</format>
</formats>
<baseDirectory></baseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<!-- this references your installer sub-project -->
<include>com.example:installer</include>
</includes>
<!-- must be unpacked inside the installer jar so it can be executed -->
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<includes>
<!-- this references your server.war and any other dependencies -->
<include>com.example:server</include>
</includes>
<unpack>false</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
If you've saved the assembly descriptor as "installer.xml" you can build your jar by running the assembly like this:
mvn clean package assembly:single -Ddescriptor=installer.xml
Hope this helps. Here are some additional links that you might find useful:
Maven Assembly Plugin - Configuration and Usage
Creating Executable JARs using the Maven Assembly Plugin
Creating executable jars with Maven

Categories