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
Related
Using this command : mvn clean install assembly:single
With this maven plugin configuration :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/descriptor.xml</descriptor>
</descriptors>
<!-- Configures the created archive -->
<archive>
<!-- Configures the content of the created manifest -->
<manifest>
<!-- Adds the classpath to the created manifest -->
<addClasspath>true</addClasspath>
<!-- Specifies that all dependencies of our application are found from the lib directory.-->
<classpathPrefix>lib/</classpathPrefix>
<!-- Configures the main class of the application -->
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
With this assembly descriptior file config:
<assembly
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/xsd/assembly-2.1.1.xsd https://maven.apache.org/xsd/assembly-component-2.1.1.xsd">
<!-- Specifies the suffix name previusly of .jar -->
<id>uber</id>
<!-- Specifies that our binary distribution is a jar package -->
<formats>
<format>jar</format>
</formats>
<!-- Avoids generating a folder of this project on base directory. Making unable to find mainClass -->
<includeBaseDirectory>false</includeBaseDirectory>
<!-- Adds the dependencies of our application to the lib directory -->
<dependencySets>
<dependencySet>
<!-- Coppying third party libraries into lib folder -->
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<!-- Decides if thirdparties are unpacked or stored in .jars -->
<unpack>true</unpack> <!--I have tried to set as false but same result -->
<!-- <useTransitiveDependencies>false</useTransitiveDependencies>-->
<!-- <scope>runtime</scope>-->
</dependencySet>
</dependencySets>
<fileSets>
<!-- Adds the application classes to the .jar file created. -->
<fileSet>
<directory>target/classes</directory>
<outputDirectory/>
</fileSet>
</fileSets>
</assembly>
Generates the next .jar
Where added package contains my project sources and lib contains third parties libraries as I wish. Also the manifest file contains what I should have according this file structure.
Anyways I am returning this error:
It looks like it does not found the third part libraries for some reason. I am starting to learing a little more about maven but this things creepy me a little bit.
I also tested configurate assembly plugin like this :
Then the .jar file generated have the third party libraries but not project sources and when launching it. It does not found the main class.
What could I be doing wrong?
Thanks in advance
Edit: I'am ensured this error is because at the start of the program is trying to initialize the log library.
Edit2: Trying to debug. I have find that java.classpath may contain the path to the class path. Making a foreach of System.getProperties(). At least java.class.path has this content:
Does the classpath may refer to this folder? I supposed it had to refer like a relative path to the inside lib folder. As MANIFEST.MF file is configured.
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.
I use Maven to build my MyProject. There are several other projects which are used as library projects for 'MyProject'.
In pom.xml of MyProject, I defined those library projects as dependencies of MyProject. One of the Library project is named "OneLibProject":
<dependency>
<groupId>com.xxx.OneLibProject</groupId>
<artifactId>OneLibProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
...
</dependency>
Under MyProject root path, after I run : maven clean install a MyProject.jar is generated. But those classes defined in library projects(dependencies) are not included in this MyProject.jar.
Each library project also have its own pom & can generate its own jar.
Now, what I want to do is I want to have my pom.xml in "MyProject" to be configured so that the generated MyProject.jar file contains the classes of OneLibProject and classes of MyProject. Other library project classes are not included.
How to achieve this?
You can use maven-assembly-plugin and configure which dependencies to include in jar in assembly descriptor's dependencySet section.
Assuming you have project y and z. Add this plugin to y's pom
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
</plugin>
place this assembly.xml in scr/main/assembly
<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>
<includes>
<include>test:y</include>
<include>test:z</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
run mvn install in z
run mvn package assembly:assembly in y
if all correct you will get y-0.0.1-SNAPSHOT-jar-with-dependencies.jar in target folder. It will contain y and x classes only.
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.
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