This is my pom 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.sia.g7</groupId>
<artifactId>sia</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>sia</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>commons-math</groupId>
<artifactId>commons-math</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jmathplot</groupId>
<artifactId>jmathplot</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jmathplot.jar</systemPath>
</dependency>
<dependency>
<groupId>jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jgraphx.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.sia.g7.AbstractSimulation</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
And when I run the jar I get:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mxgraph/swing/mxGraphComponent
which is part of the jgraphx dependency.
What am I missing?
you can do it by adding this dependencySet to your assembly file descriptor
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
this assembly descriptor add all dependencies including system scoped
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-all-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
Yes, and this is one of the reasons you shouldn't abuse system scope dependencies (which is globally a bad practice) and this problem has already been mentioned several times here on SO (here, here). I'm proposing a solution to deal with project relative dependencies in a "clean" way in this answer.
You should remove the <scope>system</scope> clauses from those dependencies.
When the scope is set to system that means the artifact is ALWAYS available, so jar-with-dependencies does not include it.
It must not be system scope in the first place. This is the source of your problem.
Install/deploy your dependency into the repository and make it a normal compile (or runtime) scope dependency.
There are 2 solutions:
Recommended: create a repository and then provide the dependency in the same way as you provide dependency from maven repository.
Though the above solution is the recommended one, but if you don't want to create the repository, then here is the workaround:
First in your pom.xml, remove the system scope and the systemPath. Please note that the commented lines were removed to resolve this issue.
<dependency>
<groupId>com.paymentus.timgroup.statsd</groupId>
<artifactId>paymentus-timgroup-statsd-client</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<scope>system</scope>-->
<!--<systemPath>${basedir}/external_lib/xyz.jar</systemPath>-->
</dependency>
Use maven-install-plugin in the same pom.xml. You need to modify the details in bold, no need to change any other thing in this plugin:
<project> ....
<profiles>...<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
**<file>${basedir}/external_lib/xyz.jar</file>**
<repositoryLayout>default</repositoryLayout>
**<groupId>com.my.project</groupId>**
**<artifactId>my-project</artifactId>**
**<version>1.0-SNAPSHOT</version>**
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins></profiles>
</project>
Unpack your jar and add it to src/main/resources.
Related
I updated my project to Java 17/Wildfly 27/Keycloak 19 and I get the error:
java.lang.NoClassDefFoundError: jakarta/ws/rs/core/MultivaluedMap
The library Maven: jakarta.ws.rs-api is added in the library of the project via Maven: jakarta.ws.rs:jakarta.ws.rs-api:3.1.0
Does somebody know why this happens?
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mainProject</artifactId>
<groupId>ch.company.mainProject</groupId>
<version>4.5.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>auth-post</artifactId>
<name>Project Auth Post (Keycloak Setup)</name>
<description>This project is responsible to setup Keycloak.</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>ch.company.project.AuthPostMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ch.company.project.AuthPostMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>ch.company.project</groupId>
<artifactId>common</artifactId>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client-jakarta</artifactId>
<version>19.0.0</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>
</dependencies>
</project>
Stacktrace:
"C:\Program Files\ojdkbuild\jdk-17.0.2\bin\java.exe" -
Dtenant.config=../e2e-test/src/main/resources/e2e-config "-
javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=55918:C:\Program Files\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath C:\work\project\auth-post\target\classes;C:\work\project\common\target\classes;C:\Users\maku\.m2\repository\org\keycloak\keycloak-admin-client-jakarta\19.0.0\keycloak-admin-client-jakarta-19.0.0.jar;C:\Users\maku\.m2\repository\org\keycloak\keycloak-core\19.0.0\keycloak-core-19.0.0.jar;C:\Users\maku\.m2\repository\org\keycloak\keycloak-common\19.0.0\keycloak-common-19.0.0.jar;C:\Users\maku\.m2\repository\org\jboss\resteasy\resteasy-client\6.2.2.Final\resteasy-client-6.2.2.Final.jar;C:\Users\maku\.m2\repository\org\jboss\resteasy\resteasy-client-api\6.2.2.Final\resteasy-client-api-6.2.2.Final.jar;C:\Users\maku\.m2\repository\org\jboss\resteasy\resteasy-core-spi\6.2.2.Final\resteasy-core-spi-6.2.2.Final.jar;C:\Users\maku\.m2\repository\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;C:\Users\maku\.m2\repository\org\jboss\resteasy\resteasy-core\6.2.2.Final\resteasy-core-6.2.2.Final.jar;C:\Users\maku\.m2\repository\org\jboss\jandex\2.4.3.Final\jandex-2.4.3.Final.jar;C:\Users\maku\.m2\repository\jakarta\activation\jakarta.activation-api\2.1.0\jakarta.activation-api-2.1.0.jar;C:\Users\maku\.m2\repository\org\eclipse\angus\angus-activation\1.0.0\angus-activation-1.0.0.jar;C:\Users\maku\.m2\repository\com\ibm\async\asyncutil\0.1.0\asyncutil-0.1.0.jar;C:\Users\maku\.m2\repository\org\jboss\logging\jboss-logging\3.5.0.Final\jboss-logging-3.5.0.Final.jar;C:\Users\maku\.m2\repository\org\apache\httpcomponents\httpclient\4.5.14\httpclient-4.5.14.jar;C:\Users\maku\.m2\repository\org\apache\httpcomponents\httpcore\4.4.16\httpcore-4.4.16.jar;C:\Users\maku\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\maku\.m2\repository\commons-codec\commons-codec\1.15\commons-codec-1.15.jar;C:\Users\maku\.m2\repository\org\reactivestreams\reactive-streams\1.0.4\reactive-streams-1.0.4.jar;C:\Users\maku\.m2\repository\org\jboss\resteasy\resteasy-jaxrs\3.15.3.Final\resteasy-jaxrs-3.15.3.Final.jar;C:\Users\maku\.m2\repository\org\jboss\spec\javax\ws\rs\jboss-jaxrs-api_2.1_spec\2.0.1.Final\jboss-jaxrs-api_2.1_spec-2.0.1.Final.jar;C:\Users\maku\.m2\repository\org\jboss\spec\javax\xml\bind\jboss-jaxb-api_2.3_spec\2.0.1.Final\jboss-jaxb-api_2.3_spec-2.0.1.Final.jar;C:\Users\maku\.m2\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;C:\Users\maku\.m2\repository\org\jboss\spec\javax\annotation\jboss-annotations-api_1.3_spec\2.0.1.Final\jboss-annotations-api_1.3_spec-2.0.1.Final.jar;C:\Users\maku\.m2\repository\com\sun\activation\jakarta.activation\1.2.2\jakarta.activation-1.2.2.jar;C:\Users\maku\.m2\repository\commons-io\commons-io\2.6\commons-io-2.6.jar;C:\Users\maku\.m2\repository\com\github\stephenc\jcip\jcip-annotations\1.0-1\jcip-annotations-1.0-1.jar;C:\Users\maku\.m2\repository\org\jboss\resteasy\resteasy-jaxb-provider\6.2.2.Final\resteasy-jaxb-provider-6.2.2.Final.jar;C:\Users\maku\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\4.0.0\jakarta.xml.bind-api-4.0.0.jar;C:\Users\maku\.m2\repository\org\glassfish\jaxb\codemodel\4.0.1\codemodel-4.0.1.jar;C:\Users\maku\.m2\repository\org\glassfish\jaxb\jaxb-core\4.0.1\jaxb-core-4.0.1.jar;C:\Users\maku\.m2\repository\org\glassfish\jaxb\jaxb-jxc\4.0.1\jaxb-jxc-4.0.1.jar;C:\Users\maku\.m2\repository\org\glassfish\jaxb\jaxb-runtime\4.0.1\jaxb-runtime-4.0.1.jar;C:\Users\maku\.m2\repository\org\glassfish\jaxb\txw2\4.0.1\txw2-4.0.1.jar;C:\Users\maku\.m2\repository\org\glassfish\jaxb\jaxb-xjc\4.0.1\jaxb-xjc-4.0.1.jar;C:\Users\maku\.m2\repository\org\glassfish\jaxb\xsom\4.0.1\xsom-4.0.1.jar;C:\Users\maku\.m2\repository\com\sun\istack\istack-commons-runtime\4.1.1\istack-commons-runtime-4.1.1.jar;C:\Users\maku\.m2\repository\com\sun\istack\istack-commons-tools\4.1.1\istack-commons-tools-4.1.1.jar;C:\Users\maku\.m2\repository\com\sun\xml\bind\external\relaxng-datatype\4.0.1\relaxng-datatype-4.0.1.jar;C:\Users\maku\.m2\repository\com\sun\xml\bind\external\rngom\4.0.1\rngom-4.0.1.jar;C:\Users\maku\.m2\repository\org\jboss\resteasy\resteasy-jackson2-provider\6.2.2.Final\resteasy-jackson2-provider-6.2.2.Final.jar;C:\Users\maku\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.14.1\jackson-core-2.14.1.jar;C:\Users\maku\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.4.2\jackson-databind-2.13.4.2.jar;C:\Users\maku\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;C:\Users\maku\.m2\repository\com\fasterxml\jackson\jakarta\rs\jackson-jakarta-rs-base\2.13.4\jackson-jakarta-rs-base-2.13.4.jar;C:\Users\maku\.m2\repository\com\fasterxml\jackson\jakarta\rs\jackson-jakarta-rs-json-provider\2.13.4\jackson-jakarta-rs-json-provider-2.13.4.jar;C:\Users\maku\.m2\repository\com\fasterxml\jackson\module\jackson-module-jakarta-xmlbind-annotations\2.13.4\jackson-module-jakarta-xmlbind-annotations-2.13.4.jar;C:\Users\maku\.m2\repository\com\github\java-json-tools\json-patch\1.13\json-patch-1.13.jar;C:\Users\maku\.m2\repository\com\github\java-json-tools\msg-simple\1.2\msg-simple-1.2.jar;C:\Users\maku\.m2\repository\com\github\java-json-tools\btf\1.3\btf-1.3.jar;C:\Users\maku\.m2\repository\com\github\java-json-tools\jackson-coreutils\2.0\jackson-coreutils-2.0.jar ch.company.project.AuthPostMain
Fehler: Hauptklasse ch.company.project.AuthPostMain kann nicht initialisiert werden
Ursache: java.lang.NoClassDefFoundError: jakarta/ws/rs/core/MultivaluedMap
WildFly (and other JEE / Jakarta EE containers) provide their own Java EE / Jakarta EE classes (servlet API, JAX-RS, JPA, etc.). If you bundle them in your application, these classes will be not be used by WildFly. Even if the class files are identical, the classes in your application are not the same as the provided classes - they have different class loaders. Class equality does not just use the fully qualified class name, it also uses the class loader. As a result, you will most likely get a class mismatch. You didn't show the full stack trace, but this is a likely cause of it.
Mark these dependencies as provided, that should usually solve the issue. You will also have to do something in your shade plugin if they still get included.
The same goes for the RESTEasy dependencies, although that's specific to WildFly / JBoss.
Deleted .idea and reimported everything and now it works
I'm trying to debug my application that's running on a remote JVM, but I can't manage to get my dependencies there too.
Here are a few things I tried:
Remote debugger:
I've set up a remote debugger, and it corrects correctly to the JVM but when I then use my application I get a bunch of NoClassDefFoundError errors coming from external libraries that are in my maven dependencies (which makes sense because I building the app with the Intellij building tools and not Maven itself. I have some relocation rules set up so maybe that's the issue?
The JVM itself is running on my local machine.
Maven debugger
There is a maven debugger integrated inside Intellij but it seems like that is just using the remote JVM during the jar packaging but nothing else. Maybe I'm going something wrong there?
I also have a few yaml files in my resources folder that should be correctly included in the debugger.
Here is my project structure:
Here is my POM file for reference:
<?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>me.playbosswar.com</groupId>
<artifactId>commandtimer</artifactId>
<name>CommandTimer</name>
<version>6.0</version>
<description>Plugin</description>
<build>
<finalName>${project.name}_v${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>/Users/tristan/Documents/minecraft/servers/1.16.5/plugins</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<relocations>
<relocation>
<pattern>com.cryptomorin.xseries</pattern>
<shadedPattern>me.playbosswar.com</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>codemc-snapshots</id>
<url>https://repo.codemc.io/repository/maven-snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.github.cryptomorin</groupId>
<artifactId>XSeries</artifactId>
<version>7.2.1.2</version>
</dependency>
<dependency>
<groupId>fr.minuskube.inv</groupId>
<artifactId>smart-invs</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>net.wesjd</groupId>
<artifactId>anvilgui</artifactId>
<version>1.5.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
Here is the actual error:
Hello everyone and thank you in advance for you help. I'm a student and learning java. For this semester we have a group project were we need to complete a code with already some functions implemented. My main task was to implement an algorithm in the class assign from the directory assignment mainly using functions from class in the directory data model. Every class though in different directorie is in the same package. When calling static fields from data model classes in assign I encounter no problem. However when calling a method here is what I get error ClassDefNotFound. I shall add that I run the program with a .jar file. I tried a LOT of things to solve the problem but unfortunately I haven't solved it yet and I'm starting to run out for time so here I am looking for your help.
I tried to change the pom.xml file from assignment directory by adding in the configuration however it's still not working. Besides I'm confused by which path should I exactly write down. I know it as to be an absolute path but I can't writ my user directory because the project is supposed to work on any computer.
Thanks again for you future help. I've been pulling my hairs out on this one thing for ever so any help solving this would be a life saver !
Think you are missing the dependencies when generating the jar. thats why it says that the class isn't defined, try this in your pom.xml
source:
Including dependencies in a jar with Maven
<build>
<plugins>
<!-- any other plugins -->
<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>
</configuration>
</plugin>
</plugins>
</build>
Personal pom.xml example (notice at the begining the use of the above plugin and also I defined a manifest, try both ways:
<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>NuevaUtilidadExcel</groupId>
<artifactId>NuevaUtilidadExcel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.nuevaUtilidad.vista.InterfazPrincipal</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>unknown-jars-temp-repo</id>
<name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
<url>file:${project.basedir}/lib</url>
</repository>
<repository>
<id>netbeans</id>
<name>Netbeans rep</name>
<url>http://bits.netbeans.org/maven2/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>com.toedter</groupId>
<artifactId>jcalendar</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<name>NuevaUtilidadExcelSVG</name>
</project>
My Project structure has resources folder inside the src/main/ folder. The resources folder contains the file server.properties. My pom is as follows:
<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.fde</groupId>
<artifactId>Listener</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Listener</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>3.6.10.Final</hibernate.version>
<java.version>1.6</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Hibernate dependencies START -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- Hibernate dependencies END -->
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.fde.ListenerServer</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>resources</resource>
<file>server.properties</file>
</transformer>
</transformers>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The Jar is created properly and the main class is mentioned in the manifest. I have the following questions :
The target folder contains the classes folder whch has the class files. The jar also contains them so why are they needed. My goal is to have a executable jar with all the dependancies only.
The resources are not getting added in the jar at all. I have added the transformer according to instructions seen on the net but no use!!!
What are the other dir getting created in the target folder (maven-archiver, surefire, surefire-reports etc) ??
Another jar gets created every time i do a maven clean install (original-Listener....jar)
I have absolutely no clue about how to include resources. Any help is appreciated!!
EDIT:::
This is the tag i used for the maven-assembly-plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.fde.ListenerServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
This created the Listener-1.0-SNAPSHOT-jar-with-dependencies.jar with all the classes from referred jars in the folders. The manifest also contains the main class.
The problem still is that I cant include the resource bundle in the folder \src\main\resources.
Also I cant understand why jar files referenced from my code are included in the jar as well as inside the META-INF folder.
The problem wasn't in your maven-shade-plugin's configuration, rather that you have explicitly set resource directory to a wrong path:
<!-- wrong -->
<resource>
<directory>resources</directory>
</resource>
As <directory> element's doc says: "Describe the directory where the resources are stored. The path is
relative to the POM."
So if you follow default maven project structure you have to set like this:
<!-- correct -->
<directory>src/main/resources</directory>
or don't set it, then it falls back to same as maven defaults.
I removed the following from the pom.xml and the property files were included at the root dir.
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
Still havent figured out why there is a repetition of the referred classes in the jar.
I am using Eclipse Maven (m2e) inside Eclipse and I am running my project like this:
My pom.xml looks like this:
<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>ro.project</groupId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ro.project</name>
<properties>
<org.springframework.version>3.1.1.RELEASE</org.springframework.version>
<org.hibernate.version>4.1.0.Final</org.hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>ro.project.ProjectServer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.7.0_02</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<id>ant-magic</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<property name="compile_classpath" refid="maven.compile.classpath" />
<property name="runtime_classpath" refid="maven.runtime.classpath" />
<property name="test_classpath" refid="maven.test.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<ant antfile="${basedir}/clientExport.xml" target="export-all" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<artifactId>project-core</artifactId>
<url>http://www.project.ro</url>
</project>
After I run the maven install it is working...
Maven run configurations:
The problem is that my generated .jar it doesn't have the dependencies included....
How can I configure pom.xml to include all my dependencies in .jar format and not unpacked.. because it seems that unpacked are not working correct...
To be sure that including all jars is ok.. I downloaded and added each library into jar's /lib folder and the jar is running... so.. my only question is: How can I configure pom.xml in order to add all my dependencies in jar format?
I tried all methods:
assembly:assembly
assembly:single
assembly:single with my descriptor (an assemble.xml file) but it wasn't working
maven copy dependencies plugin but still not working with Eclipse Maven - m2e
I am out of solutions... can anyone tell me a proper way to add my dependencies in jar? I can't believe that maven is so complex and I can't find an answer to my question everywhere..
Thank you in advance
There are a couple of ways of doing this.
1) If you want an uber-jar (repacked with all dependencies), look into using and configuring the maven-shade-plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.group.id.Launcher1</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This will unpack all dependencies and merge them into one JAR file.
2) If you want to deliver a bundle (zip, tar.gz, etc) with the unpacked JAR files in the bundle (perhaps under lib/) then you need to look into the maven-assembly-plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>create-distro</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/dist.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note that this requires an assembly descriptor src/main/assembly/dist.xml and example looks like this:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>false</useTransitiveDependencies>
<unpack>false</unpack>
<scope>runtime</scope>
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
<outputDirectory>bin</outputDirectory>
<includes>
<include>com.group.id:project-launch1</include>
<include>com.group.id:project-launch2</include>
</includes>
</dependencySet>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>runtime</scope>
<fileMode>0644</fileMode>
<directoryMode>0755</directoryMode>
<outputDirectory>lib</outputDirectory>
<includes>
<include>com.group.id:project-lib1</include>
<include>com.group.id:project-lib2</include>
<include>com.group.id:project-lib3</include>
<include>com.group.id:project-lib4</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
And since you are now assembling dependencies, you have better define the dependency in the pom.xml, like so:
<dependencies>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-launch1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-launch2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
... and so on ...
</dependencies>
3) If you are delivering a bundle with an executable JAR file launcher, you will likely need to add a maven-jar-plugin configuration in addition to the maven-assembly-plugin:
<dependencies>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
... and so on ...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<compress>true</compress>
<manifest>
<mainClass>com.group.id.Launcher1</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Note that the "addClasspath" directive adds the project dependencies to the class path. This is needed for JAR launchers, as they explicitly ignore all CLASSPATH environmental variables.