Maven dependencies doesn't get exported into the jar - java

I have a project which I want to export as jar (for some reasons it's impossible to export it as a runnable jar). I have 3 maven dependencies, gson, io and junit, but when I execute the maven built jar in console it says this:
Check my build path:
I export it this way (Eclipse):
Run as -> Maven build...
(mvn) package
And here is my pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Carlos</groupId>
<artifactId>Buscaminas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Buscaminas</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<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>res.application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
Also the maven build result:

My project didn't seem to have the correct project structure so I crated a new (maven) project and migrated my packages to the src/main/java folder and then used this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>res.application.Main</mainClass> <!-- Or wherever is your main method-->
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
And executed mvn package
That created a "jar-with-dependencies" jar also with all the recources (images, interface fxml files...).

From what I understand what you need might be this one
mvn install dependency:copy-dependencies

The built jar does not contain the dependencies, you must either provide them in classpath when executing the jar or have build process copy them into your jar, creating a so-called uber-jar. Good way to achieve the latter is using maven-shade-plugin.

Related

How can I execute a simple jar?

I'm trying to make a simple batch file with maven commands to execute the installation and execute the jar main class.
But the compile JAR does not have the dependencies and I get error
Exception in thread "main" java.lang.NoClassDefFoundError:
akka/actor/ActorSystem
This is my simple script
call mvn clean dependency:copy-dependencies
call mvn package
call cd target
call java -jar distributed-1.0.0.jar
pause
POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>distributed</groupId>
<artifactId>distributed</artifactId>
<version>1.0.0</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.scala-lang/scala-library -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.5.16</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>src/main/java/resources/lib
</outputDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source/>
<target/>
</configuration>
</plugin>
<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.ipca.distributed.Implementations</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
What do I need here??
I think the easiest solution to create a runnable jar is to use Maven Assembly Plugin, or Maven Shade Plugin, or One Jar Plugin or even Spring Boot Plugin ( it's not weird if it works ). You can find some more details on how to use each one here.
These plugins are doing exactly you are trying to do with the 2 combined plugins that you have ( maven-dependency-plugin and maven-jar-plugin ).

Execution of Scala and Maven project

I have created a simple maven project of Scala in eclipse's scala ide using below details -
Artifact Id - scala-archetype-simple
Group Id - net.alchim31.maven
After created the project I have modified the pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>scalapoc</artifactId>
<version>0.0.1</version>
<name>${project.artifactId}</name>
<description>Test scala app</description>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.5</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.compat.version}</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<!-- <arg>-make:transitive</arg> -->
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
<archive>
<manifest>
<mainClass>App.scala</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,... -->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have also added manifest entry inside plugin tag.
<archive>
<manifest>
<mainClass>App.scala</mainClass>
</manifest>
</archive>
I have only one App.scala file and one test file available in project. Build was successful. But when trying to execute the jar using java -jar <jar_name> getting below error -
no main manifest attribute, in scalapoc-0.0.1.jar
When trying to execute the command java -cp scalapoc-0.0.1.jar com.test.scalapoc.App.scala getting below error -
Error: Could not find or load main class com.test.scalapoc.App.scala
Please suggest what to be needed to execute the jar.
try
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.test.scalapoc.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
.scala is for source only, source is not executable
archive tag is part of the configuration of a plugin
see https://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make
UPDATE 2018-06-05:
To create a standalone jar (without need to defined a classpath with scala-library and other dependencies on command line), you should create a jar that included other classes/jar.
see:
Apache Maven Shade Plugin – Executable JAR and the rest of the doc of the plugin for tuning & configuration
or the old one-jar tool Deliver Your Java Application in One-JAR™ !

Java Maven Clean and Build With Dependencies

im still new in java maven and dependencies. can i ask?
I create some project about QR code Generator with using Java Maven.
When I run my project into Netbeans, using qrgen-1.2.jar, core-2.0.jar and javase-2.0.jar. IT CAN GENERATE ANY QR CODE THAT I WANT.
But when i try to Build and Clean, it CANNOT generate my QR code in my Document/NetbeansProjects/QRcode/target/QRcode-1.0-SNAPSHOT.JR
Here my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>QRcode</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.qrcode.QRcode</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.glxn</groupId>
<artifactId>qrgen</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
Based on your POM, you aren't packaging your dependencies in the executable jar being generated. This is causing your program to fail when you run it outside of the IDE.
Here's an example of how to use the Maven Assembly plugin to create an executable jar that includes your dependencies:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.qrcode.QRcode</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
The output of mvn package will now include target/QRcode-1.0-SNAPSHOT-jar-with-dependencies.jar, which you can see includes the classes specified by your build dependencies.
Here's a link to the documentation for the plugin.

Maven build does not compile

I am trying to compile a maven project but I get the following error:
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project storm-example: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1]
I am new to maven projects actually. The thing is I am not using the codehous.mojo plugin at all. From what I saw from different existing issues, the people who got this were using the mojo plugin in the build, but in my case i just need the maven-assembly-plugin. Do I need mojo in every maven object despite not explicitly needing it?
The following is my maven pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.learningstorm</groupId>
<artifactId>storm-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>storm-example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass />
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The error is a complaint about your following tag in the maven-assembly-plugin's configuration :
<archive>
<manifest>
<mainClass />
</manifest>
</archive>
Unless you want to create an executable jar (in which case you should specify the qualified name of the "main" class), you should just remove the whole <archive> tag.
It looks like you've based your work around the "Creating an Executable JAR"
example of the maven-assembly-plugin's usage page while you might only need what is defined in the first basic example.

maven how to include dependencies correctly

I was trying to figuring out how to use maven but I didn't get it.
First I tried sqlite 3.8.7 from :
mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.8.7
I can compile well but when I try to execute maven doesn't find sqlite jar file, so I try to use :
mvn install:install-file
but It didn't work too, so I just used -cp and I have fixed.
Second I try to use jfreechart from :
http://mvnrepository.com/artifact/jfree/jfreechart/1.0.13
I did same steps below for jfreechart but this time it gives me NoClassDefFoundError.
Both of them works when I compile manually but with maven it's not. What I am missing about it ? If I always add manually like sqlite why should use maven anyway ?
Notes : I compile as :
mvn compile
and I'm packaging as :
mvn package
and finally I try to execute as :
java -cp target/porject.jar org.path.App
Edit :
This is for jfreechart app(pom.xml)
I add dependency tags from mvnrepository.com
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.project</groupId>
<artifactId>ChartTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ChartTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>
</project>
Thanks in Advance
All that you need to create your jar executable with your dependencies and launch it as a standalone app:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.mypackage.main.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
and then run goal :
clean package assembly:single
Your problem is you don't include your dependencies in your target jar.
Add this to your pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>attached</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then you can start your app using the command line:
java -cp target/ChartTest-1.0-SNAPSHOT-jar-with-dependencies.jar org.path.App
Don't use hyphen in commands, use mvn package and mvn compile

Categories