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

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>**

Related

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>

How to format the name of a JAR File, which will be created with Maven3?

I have a question concerning Maven3. I want to create a JAR File with the name myJarFile_1.0.0-SNAPSHOT_200140723.jar.
But Maven only create the follwoing name (I used the command "mvn clean install"):
myJarFile_1.0.0-SNAPSHOT_200140723-jar-with-dependencies.jar
Here is my finalName declaration:
<finalName>${project.artifactId}_${project.version}_${timestamp}</finalName>
Here is the configuration of my Maven-asembly-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifestEntries>
<source-jdk>1.6</source-jdk>
<target-jdk>1.6</target-jdk>
</manifestEntries>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>myMainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assembly-jar-Id</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
How can i solve this problem?
I can only work with the JAR File, which includes the text "jar-with dependencies". But i must rename this file, because of my bash sell script, which execute this File. The second JAR file, which was created by maven, can not be used, since i get this error message: no main manifest attribute, in myJarFile.jar
Many thanks !

Maven: Not copying files from src/main/resources to the classpath root - Executable Jar

I am building an executable jar with Maven using the Assembly plugin. I have a resource file(.xml) which is placed at src/main/resources. When I build the executable jar, the file is not getting copied into the jar - Checked by unpacking the jar.
Here is my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
I am trying to call the following resource which is kept under src/main/resources:
reader = Resources.getResourceAsReader("mybatis-configuration.xml");
Getting the following exception while executing java -jar InterfaceRunner.jar
Exception caught while reading or parsing the mybatis config xml :java.io.IOException: Could not find resource mybatis-configuration.xml
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:108)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:95)
at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:153)
Has anyone faced a similar issue before? Looking for your help, Maven gurus..
You can try replacing your Configuration as follows;
<configuration>
<archive>
<manifest>
<mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>InterfaceRunner</finalName>
</configuration>
And then
mvn package
By default, maven-assembly-plugin makes TWO jars, not one (with phase package). Make sure you are running the one called <artifactid>-<version>-jar-with-dependencies.jar
If this is not the problem, are you doing anything weird in your pom here:
<build>
<resources>
<resource>
<targetPath><!-- here?? --></targetPath>
</resource>
</resources>
<!-- etc. -->
</build>

Creating Two Executable Jars Using maven-assembly-plugin

I have a Maven project and I want to create two executable jar files from it. One will be used interactively by users and a second will be run as a scheduled job that reads the log files produced by the former. In the end, I would expect the two jar files to be identical except for the Main-Class attribute in the MANIFEST.MF file.
I am using maven-antrun-plugin to create an executable jar and this seemed to be working fine until I tried to create a second jar file by introducing Maven profiles. The relevant section of my POM file looks like this:
<profiles>
<profile>
<id>publisher</id>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}</finalName>
<archive>
<manifest>
<mainClass>fully.qualified.path.Publisher</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>logReader</id>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}-logReader</finalName>
<archive>
<manifest>
<mainClass>fully.qualified.path.LogReader</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Really, the only difference between the two is the "finalName" and "mainClass" elements as defined within the plugin.
When I try to execute mvn:package on both profiles (I'm using IntelliJ IDEA, by the way), I get two .jar files, but one is correct and the other is incorrect. The "correct" one contains all the dependencies and a valid MANIFEST.MF file. The "incorrect" one contains no dependencies and the MANIFEST.MF file lacks the "Main-Class" property that I need in order for it to be executable.
I have found that if I ever run just one profile or the other, that it works fine but, if I try to execute both profiles at once, it fails. I also get the following notes in my log, but I must admit that I don't completely understand what they are saying:
[INFO] Building jar: .../target/publisher.jar
...
[INFO] Building jar: .../target/publisher-logReader.jar
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: .../target/publisher-logReader.jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: .../target/publisher.jar with assembly file: .../target/publisher-logReader.jar
Any thoughts about this? Is it possible to produce two jar files with a single mvn:package in this way, or am I barking up the wrong tree?
Thanks!
So as soon as I posted this, I found this thread:
Create multiple runnable Jars (with depencies included) from a single Maven project
This uses a different approach in that it doesn't use two profiles, it uses two executions, as such:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-publisher</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>fully.qualified.path.Publisher</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>build-logReader</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>fully.qualified.path.LogReader</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-logReader</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This seems to be working. The moral of the story seems to be that I don't completely understand profiles or when they should be used.

Categories