Maven ,error when build with maven-assembly-plugin - java

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>

Related

Jar created using maven-assembly-plugin not running with mvn install

I am creating a jar file using maven assembly plugin.
The minimal pom file looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.org.taptest.tests.TestsRunningAsTAP</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn clean install, it creates a jar file but when I run the jar file using java -jar <jarfileName>, it says Error: Could not find or load main class.
While if I edit the above pom, to have
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.org.taptest.tests.TestsRunningAsTAP</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
and after running mvn clean install, I run mvn compile test-compile assembly-plugin, it generates the same jar file, but this jar file runs as expected.
So, my question is why the jar file with the first approach does not runs and what exactly does mvn compile test-compile assembly-plugin command do?
I was facing Same issue,
please check if you have added jar at top you pom.xml inside tag.
like this:
<project>
<groupId>com.rohan</groupId>
<artifactId>jmetertester</artifactId>
**<packaging>jar</packaging>**

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.

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.

How to build a jar from multimudule project?

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>

Categories