mvn jar file : library include failed - java

I did mvn package and ran my file by doing java -jar target\output.jar
All the jar libraries specified in the dependency of pom.xml are not included. Do advice what's wrong below. Thanks!
Below is my pom.xml to allow jar file generation.
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.workspez.psg.letrikEtara.PlanetGroupLetrikEtara</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Try and compare/complete your declaration with the ones in "Including dependencies in a jar with Maven", like adding the executions section.
Or, if this is not working, consider the maven shade plugin, whic can achieve a simlar goal.

Got it working. This was missing:
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
To the following should work:
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.workspez.psg.letrikEtara.PlanetGroupLetrikEtara</mainClass>
</manifest>
</archive>
</configuration>
</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>**

Making absolute reference to a classpath in Maven

I have a customized jar that the application uses. Instead of installing jar in maven I want to make reference to it by defining absolute location for that jar in the manifest file. I am using the code shown below to update information in manifest file. However, I am not sure how can I reference to the jar location that uses absolute path like : D:/Shared_library/Test.jar . This might not be a good practice but I want to see if there is anything like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
You can add it using <manifestEntries> tag.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>file:///D:/Shared_library/Test.jar</Class-Path>
</manifestEntries>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Putting version information in manifest file into JAR using Maven

I am trying to find a way of putting version information in manifest file into JAR using Maven?
For WAR files I use the following in my POM.XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
and then I run "mv clean package"
How would you do this for a JAR.. Can you please give me details information on the changes and the commands... thanks
You can use maven-jar-plugin (the configuration is nearly the same as maven-war-plugin)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
<key>value</key>
</manifestEntries>
</archive>
</configuration>
...
</plugin>
Here the documentation of the plugin: https://maven.apache.org/plugins/maven-jar-plugin/examples/manifest-customization.html
And all options: http://maven.apache.org/shared/maven-archiver/index.html

Can not set the final jar name with maven-assembly-plugin

This is how I configured maven-assembly-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>myapp</finalName>
<archive>
<manifest>
<mainClass>com.myapp.Main</mainClass>
</manifest>
</archive>
<!--
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
-->
</configuration>
</plugin>
and I expect the final jar file should be myapp.jar but it ends up with myapp-jar-with-dependencies.jar
Can you tell me how to configure to exclude "jar-with-dependencies" out of the final name?
You can specify the finalName property to give the jar the name you want, and specify that appendAssemblyId should be false to avoid the jar-with-dependencies suffix.
The configuration below will output a jar called test.jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>test</finalName>
<archive>
<manifest>
<mainClass>com.myapp.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>

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>

Categories