How to build a jar from multimudule project? - java

I have two Maven modules in on root module.
root
- module-core
- module-run
module-run has module-core as a dependency.
How can I build a jar file based on module-run with dependencies and manifest?
UPD
I use
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.test.app.main.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Use the maven-assembly-plugin. It will by default bundle all of the modules dependencies into the JAR. If you need to restrict it to just module 2, that's trickier but doable... To bundle all dependencies, just add this plugin configuration:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

Related

Maven - error cannot find or load main class

I tried to build this example : https://github.com/oltzen/JavaCppExample with Maven (mvn clean install) on Linux.
After the successful build, when I run : java de.oltzen.javacppexample.Abc : it says 'could not load or find the main class'
The video tutorial (https://www.youtube.com/watch?v=LZrrqZLhtmw) uses Eclipse and it runs the program with Run as .. Java Application
Is the POM file missing something ?
I tried to add this plugin in POM but it did not work:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>de.oltzen.javacppexample.Abc</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I am executing from JavaCppExample/target/classes/ :-
The /classes folder contains package folders : de/oltzen/javacppexample/
The last folder contains the class file Abc.class
So I run the command :
java de.oltzen.javacppexample.Abc
The /target folder contains :
1) classes [folder containing the package]
2) JavaCppExample.jar
3) maven-archiver
4) maven-status
Please help
I added the following plugins for maven copy dependencies and executed java -jar javaCppExample.jar [from /target folder]and it worked. Thanks everyone !
[Simply build using mvn clean install]
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<target>1.8</target>
<source>1.8</source>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>de.oltzen.javacppexample.Abc</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
If you are only looking to run your prgramm via maven then use exec-maven-plugin.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>de.oltzen.javacppexample.Abc</mainClass>
</configuration>
</plugin>
Explore more here => https://www.mojohaus.org/exec-maven-plugin/index.html
If you want to build & run as jar then use assembly plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>${project.basedir}/assembly/assembly.xml</descriptor>
</descriptors>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>de.oltzen.javacppexample.Abc</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Explore more about Maven assembly plugin to customize to your needs.

Creating an uberjar with nested resources

I have created an uberjar for a project that has nested resources. So in the src/main/resources folder there are subfolders. When I try to access any of those resources in the program via a getResourceAsStream("filename") it is unable to find any of them unless I either provide a full path to them inside the call or place them all at the root of the jar. Is there a way to add these to the classpath?
Here is my pom.xml entry for the assembly-plugin i am using to build the uberjar:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-first</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.joy.hmi.core.HMIApplication</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>HMIApp</finalName>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.joy.hmi.core.HMIApplication</mainClass>
</manifest>
<manifestEntries>
<Class-Path>images/ config/ properties/ controlTidxConfig/ HMITidxConfig/</Class-Path>
<Trusted-Library>true</Trusted-Library>
<Permissions>all-permissions</Permissions>
<Codebase>*</Codebase>
<Trusted-Only>true</Trusted-Only>
</manifestEntries>
</archive>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>make-the-zip</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<finalName>HMIApp</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>

assembly-maven-plug not generate jar-with-dependecies ad no main manifest attribute

Hi when i run mvn package the assembly plugin generate the jar name AnonymousChat-1.0-SNAPSHOT.jar then i thought this shuld be good then i try to run this jar but i had the error "no main manifest attribute" this is the snippet of maven plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.shell.Terminal</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
I find this code that will solve the problem, if for someone can be useful I write i here.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.shell.Terminal</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

Eclipse built-in jar export option vs Maven assembly plugin

I use IntelliJ IDEA to generate a jar file with maven-assembly-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>test.LeanFTest</mainClass>
</manifest>
</archive>
<finalName>${project.artifactId}-fatjar-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
It didn't work, it asks continuously for dependencies during execution. I was unable to use jar file, so I imported the project to Eclipse and used the built-in jar export option. This jar is working fine. I couldn't find what the difference is between these jar files.
The reason is probably that Eclipse is generating a "Class-Path:" entry in the manifest file.
Try to update the assembly conf in your .pom file by adding something like (by heart, not checked):
<addClasspath>true</addClasspath>
By default the maven-assembly-plugin will not bundle all the dependencies required for your own jar to run standalone.
So you will need to add this below under the plugin configuration:
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
As a result, the plugin tag becomes:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>test.LeanFTest</mainClass>
</manifest>
</archive>
<finalName>${project.artifactId}-fatjar-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef> <!-- the secret -->
</descriptorRefs>
</configuration>
</plugin>
You can run:
java -jar path/to/LeanFTest.jar
To directly run the program.

Maven ,error when build with maven-assembly-plugin

i have a maven project and i want to create a .jar contain all the dependency
for that i am using maven-assembly-plugin
Maven build... with goal clean package assembly:single i get errors like
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] C:\integ\src\main\java\com\woo\bfi\la\ss\batchIntegrator\BatchRunner.java: [11,54] error: package com.ss.ff.ll.dd.bigDataAccessManager does not exist
it say that the packeges i imported in the class BatchRunner does not exist
but i can run the application successfully
the plugin in the pom.xml :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.test.IntegrationTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
i know that maybe there are not enough information to solve this but i hope someone had the same problem and solved it
try to use this. only mvn clean package
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>testXXX</finalName>
<archive>
<manifest>
<mainClass>com.test.IntegrationTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Categories