I have Java Maven project in Eclipse. When I do maven build and set goal to package maven builds jar file with name my-project-0.0.1-SNAPSHOT.jar. I need package to be always constant name since I put it system that starts it with script. my-project.jar would be fine. How to achieve that?
How to ask maven to put all jar libraries my project is using into my-project.jar?
How to ask maven to place my-project.jar into particular folder target/ready_release. Currently maven puts jar into target folder. How to ask maven copy all libraries and configuration files project is using into this folder too.
Maybe I'm mistaking and all these jobs should be done under some other maven goal or any other operation?
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>com.aaa.rfid.scaleandlabel</groupId>
<artifactId>my-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<organization>
<name>aaa</name>
<url>www.aaa.lt</url>
</organization>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.scream3r</groupId>
<artifactId>jssc</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</project>
You can specify the name of the package by configuring the jar plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<finalName>ready_release/my-project</finalName>
</configuration>
</plugin>
This will create the jar file at <project-root>/target/ready_release/jar-name.jar and every subsequent build will overwrite it.
Related
My problem
I have written some java-code. The code runs flawlessly in intellij.But when I run it as a .jar file (i.e., the command java -jar app.jar), I get the error message:
Unable to initialize main class RSocketClient.Client
Caused by: java.lang.NoClassDefFoundError: io/rsocket/transport/ClientTransport
My Research
I have been searching about the NoClassDefFoundError on google and stackoverflow and have found that the error arises when dependencies are missing. The solution seems to be that I need to add dependencies to the classpath or maven repository. But I don't know how to do this (my pom.xml is presented below).
My Question
All I want to do is to turn my program into a .jar file and run it through my terminal (Windows10 OS). I appreciate all the help that I can get.
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>org.example</groupId>
<artifactId>RSocketSample</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>RSocketClient.Client</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.rsocket</groupId>
<artifactId>rsocket-core</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.rsocket</groupId>
<artifactId>rsocket-transport-netty</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
</project>
It looks like your jar doesn't contain classes of RSocket itself, and its fine.
Jar created by maven contains only your classes (in your module).
The jar of rsocket (as well as other jars that you're using,like SLF4J stuff for example) reside in the corresponding jars prepared by the maintainers of these third-parties.
IntelliJ "sees" the whole classpath (that includes all the dependencies) and runs your application with all this classpath.
Probably what you should do is create a jar including dependencies:
See This SO thread
Another option is keeping your jar "as is" but requiring that it will be run with all the dependencies in the classpath, but it looks like its not what you're trying to do, this way is used when you're developing a library and not a standalone application.
I am using the Eclipse IDE and in the past, I have been developing a maven project depending on local sources (from github (sap-olingo)) as well as maven dependencies loaded as jar. I have now updated the github source and also all the dependencies which seems to break standard eclipse export functionalities (right click porject -> export -> war)
The webproject is working fine when using the maven install functionality and is loading all sources into the META-INF/lib classes, but when using the export as war from eclipse (which I did previously) it is missing to compile a proper dependency. Missing in the sense of that the jar is still present in the META-INF/lib directory of the war, but the content is empty and the size is only 1kb
When comparing the mvn clean install to the exported, there are rarely any differences. Except for the projects that are to be compiled during the runtime. Sizes of the libs in the mvn created war and the exported war roughly matches except for that one jar.
Is there a way to root cause that issue? I am using solely the maven integrated into the IDE which is why I doubt differences in the maven versions and or jdk.
Unfortunately I have no idea in where to start and what info you require
Thanks for your input
EDIT:
My projects 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>odata</artifactId>
<packaging>war</packaging>
<version>4.0.0</version>
<name>odataMaven Webapp</name>
<url></url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sap.olingo</groupId>
<artifactId>odata-jpa-processor</artifactId>
<version>0.3.7-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies>
<build>
<finalName>odata</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${project.build.source}</source>
<target>${project.build.source}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.source>1.8</project.build.source>
</properties>
</project>
The odata-jpa-processor source/project is pulled from here. The problem exists solely with the olingo-jpa-metadata jar which is empty. All other maven dependencies are exported correctly.
I am migrating my project from Java 8 to Java 9. My project is mavenised. Now for migrating to Java 9, I am planning on creating a separate module directory where all the required dependency of a module will go.
For doing this through maven the only way I know is to use the copy plugin of maven to copy all the required dependency in the module directory. So after running maven installs for a module, the dependent jars will be copied in repository folder(which is by default) and also copied to this module directory folder.
So there will be a copy of the jars and also hard coding in pom.xml for copying specific dependency in the module directory.
This approach doesn't seem to be clean, is there any way out were automatically maven can read my module-info.java file and copy the required dependency not in the classpath but in the specified directory
Here is my 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>
<parent>
<groupId>com.aa.bb</groupId>
<artifactId>cc</artifactId>
<version>0.0.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>dd</artifactId>
<name>dd</name>
<groupId>com.aa.cc</groupId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
<compilerArgs>
<arg>--module-path</arg>
<arg>./moduledir</arg>
</compilerArgs>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>6.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
module-info.java
module com.some_module_name {
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
requires org.apache.commons.codec;
requires spring.beans;
requires spring.context;
}
After adding module-info.java to an existing java 10 module and running maven i am getting issue like "package exist is used in module a and module b".
What i believe is while running maven it is looking for module dependency in the .m2/repository and there are loads of jar there as the m2./repository is common for my multiple modules.
So what i was planning to do is create a separate module directory for each module and place the required jar for that module in it which way it even works.
I assume you're making this effort to make sure Maven "does the right thing" regarding the module and class path, i.e. placing direct dependencies of your module on the former and all other dependencies on the latter. If that's so, there's nothing you need to do - from version 3.7 on, the Maven Compiler Plugin does it for you as soon as you add a module-info.java to your src/main/java directory.
You can verify that by running Maven in debug mode with mvn clean compile -X. When you carefully analyze the output, you will see which JARs end up on which path.
This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 6 years ago.
i might have completely missundestood how maven works, but i have the following issue.
i created an application with maven in netbeans. it runs in netbeans fine, but when i make Clean and Build it just compiles my source files, and the maven dependendencies are not in the jar file, so cannot run in command window.
this is my pom file. it has build section, the dependencies are in the classpath of mainfest.mf
<?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.ibh</groupId>
<artifactId>SafePassword</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.ibh.safepassword.TestMain</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>4.2.0.Final</version>
<type>jar</type>
</dependency>
</dependencies>
<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>
i came from c# world, thought maven is similar than NuGet which is playing the game only in DEV phase, but at compile time everything is there ...
what is missing? how to execute the jar file to have the other dependencies in place and so the app can run?
thanks for your hints!
EDIT
i don't agree that this question is fully duplicated to the one you're referring to.
as i can read these many solutions maven is quite complicated, there is no ONE solution for every issue
By default Maven builds a jar that is basically a library - it doesn't contain its dependencies. If you want to include dependencies, making a runnable application, you have two main options:
Bundle everything in that one jar using some plugin that allows it, such as maven-shade-plugin. You can then run this jar.
Create a distribution bundle with your jar and all dependencies:
Use packaging that is designed to be a distribution, such as war or ear - if one of them matches your intent
Otherwise use generic bundling plugin, such as standard maven-assembly-plugin or even more advanced appassembler-maven-plugin
First off: I'm not a Java coder. I'm new to the Java/Maven tool chain. We're using a Java library for a project which we want to launch as a Heroku background worker.
This project relies on two external libraries, the mongodb Java driver which is available through Maven's central repo, and another third party library. I've seen the Heroku article on "unmanaged dependencies", but something else appears missing, as I get an error like: Could not find the main class: com.company.myproject.MyApp Program will exit. when I try to run the app locally according to Heroku's instructions on "Getting Started with Java".
I noticed that their pom.xml file contains a Maven plugin maven-dependency-plugin to copy dependencies, and when I check my target/classes folder, I don't see any of the dependencies.
Heroku also publishes a guide on building background workers in Java. That pom.xml contains a build assembly plugin, which seems more complex.
I'm a bit lost in all this ceremony (especially coming from Rails), and I'd like to stat with the simplest possible pom.xml to get this running. Is there a Maven archetype file for Java workers on Heroku? I'm also using NetBeans as IDE, and it would be great to use the IDE tools for this, if available, but it's a secondary priority.
Below my pom.xml so far:
<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.myproject</groupId>
<artifactId>myproject</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>myproject</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>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.thirdparty</groupId>
<artifactId>thirdparty</artifactId>
<version>0.2.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>project-local</id>
<name>Project-local Repo</name>
<url>file:${project.basedir}/repo</url>
</repository>
</repositories>
</project>
You definitely need to use the maven-dependency-plugin to copy all of the dependencies into the target/dependency directory:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then your Procfile needs to include those dependencies in the classpath:
foo: java -cp target/classes:target/dependency/* com.myproject.Main
Where com.myproject.Main is the class name of the Java class you want to run (which must contain a public static void main method. Note that this also adds the Java classes which are compiled from src/main/java into the target/classes dir.