How to package an application into JPMS modules using Maven and NetBeans? - java

My goal is to package an application into a modular runtime image bundled with a custom JRE, using jlink. My app is a simple "hello world" Java Standard Edition app, with a dependency to Guava. I use the JDK 11.
Basically I try to reproduce this tutorial by Baeldung, but with NetBeans, Maven to manage the dependencies and the Maven Compiler Plugin version 3.8.1 for the build with the module system.
The directory structure:
The module-info.java file:
module TestwithJLink {
requires guava;
exports net.clementlevallois.testwithjlink;
}
Controller.java:
package net.clementlevallois.testwithjlink;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
public class Controller {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Multiset<String> test = HashMultiset.create();
test.add("hello");
test.add("world");
System.out.println("test: "+ test.toString());
}
}
The 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>net.clementlevallois</groupId>
<artifactId>TestwithJLink</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<id>compile</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
But it creates compiled classes, no jars or modules. So I can't go further (analyze the modules of the jar with jdeps, then using jlink). I must be missing something obvious but what?

If you want to create a JAR file, then go to the root folder containing pom.xml in your terminal and type :
mvn package
This will create a JAR in target folder.
Now change your path in terminal to target folder and Run the JAR file using:
java -jar {file-name-version}.jar

Finally got it. The scenario:
working from NetBeans, with your dependencies handled by Maven
you app has a module-info.java declaration
your dependencies also have a module-info.java declaration.
You want to package your app in a way that respects the modular system. So:
have these 3 Maven plugins listed in your pom (see below). Be careful about the version numbers for the plugins! In particular, the <goal>resolve</goal> in the maven-dependency-plugin makes sure your dependencies are packaged with their module-info.java file, which is not the case otherwise! (see here).
when the compilation is done, move the jar of your app in the lib folder where all the jars of your dependencies are already located.
You can run this app directly with the run icon in NetBeans, or:
from the parent folder of your lib folder, run: java --module-path lib --module NameOfYourModule
The POM:
<?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>net.clementlevallois</groupId>
<artifactId>TestwithJLink</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>net.clementlevallois</groupId>
<artifactId>utils</artifactId>
<version>1.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>resolve</goal>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<mainClass>net.clementlevallois.testwithjlink.Controller</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<id>compile</id>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

Related

maven assembly plugin does not include my classes inside built jar

Any code referenced below is from the project here
For the AWS Lambda, I have been trying to create a jar file based on the maven configuration defined here.
<?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>be.quodlibet</groupId>
<artifactId>HelloLambda</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
When I try and build the fat JAR, upload it to AWS Lambda console, I see the error
The following package be.quodlibet.hellolambda was not added to the jar resulting in a
"errorMessage": "Class not found: be.quodlibet.hellolambda.helloHandler",
"errorType": "java.lang.ClassNotFoundException"
As #khmarbaise said in the comments, make sure you run "mvn clean package" and not "mvn clean assembly:single".

JavaFX + Maven + Ojdbc6 + IntelliJ (deploy oracle jar with Javafx application for windows)

I've been trying to deploy my JavaFX application with the Oracle client (ojdbc6) embedded into the same jar.
The application runs fine and connects to the database if I'm running it through IntelliJ, but once I run the "package" task and try and run the application from double clicking the jar or running:
"java -jar .\maven-Newton-1.0-SNAPSHOT.jar"
The application starts, but it won't connect to the DB:
`
In the code I've tried both:
Class.forName("oracle.jdbc.driver.OracleDriver");
and
Class.forName("oracle.jdbc.OracleDriver");
I'm just starting with maven and I'm not too sure if my configurations are correct:
• I've tried adding the ojdbc6.jar to the global libraries:
• I've tried adding the ojdbc6.jar file to the SDK classpath:
• And I've messed around with the module dependencies:
But my problem may be lying on the POM file because the other jar that I want to embed within my application is not deploying as well (org.reflections)
• 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>Newton</groupId>
<artifactId>maven-Newton</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Controller.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
</dependencies>
</project>
Not too sure where to go from here, any help would be very much appreciated.
Thanks
The problem is that the single jar file, you produce with the 'package' goal does not contain the ojdbc.jar in itself.
You will have to run the jar file with a classpath, e.g.
java -cp ojdbc.jar;othernecessary.jar maven-newton-project-1.0.jar
Btw, there is a distinction in maven between dependencies (which are needed for the code to work) and plugins (which are needed for maven to build). You dependency on maven-compiler-plugin and maven-resource-plugin suggests to me you are confusing these two concepts.
I would move these two into the section called <build><plugins> instead alongside the maven-jar-plugin.
If you want an easy to comprehend start, try this: http://www.darrencoxall.com/java/understanding-maven/

why do I get a jni error being caused by FirebaeValueEventListener on my netbeans project

I created a program that writes to firebase and the program runs fine when I run it in the Netbeans IDE but when I build it to a .JAR file and run said jar file I get this message:
Heres my pom XML if that helps:
<?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.FUJCode</groupId>
<artifactId>climate2.0</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.2.0</version>
</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>
<name>UsageMoniter</name>
<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>Climate</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Im fairly sure I got all the dependencies so i don't know what could be wrong.
If you need anymore information let me know and any help would be greatly appreciated.
Many thanks,
Tom
EDIT
EDIT 2
here in the project folder it shows all the firebase stuff in the dependencies folder:
But when you look at the files there is no firebase business to be seen, so im not sure what to say...
You need to copy the necessary third party artifacts into your project, e.g. by extending your pom.xml:
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Then you need to add your third party dependencies to the classpath of your application when starting it, e.g:
java.exe -cp "climate2.0-1.0-SNAPSHOT.jar;lib/*" -jar climate2.0-1.0-SNAPSHOT.jar
or create a uberjar with the help of maven hade plugin, see here
This will create another jar which contains all your dependencies when you build your project.

Maven: Generating an executable jar file with all the dependencies

I recently converted one of my Java project to a Maven project. My pom.xml looks something 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<groupId>com.myproject.groupid</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyProject</name>
<description>The first version of my maven project</description>
<dependencies>
<dependency>
<groupId>com.dependent.jar</groupId>
<artifactId>dependentjar</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<type>jar</type>
<systemPath>${project.basedir}/jars/dependent.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myproject.main.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</build>
</project>
When i execute the mvn compile and mvn install, the project works fine and it also generates the jar. But when i try to run the jar [using java -jar MyProject.jar], i get an error which says: Exception in thread "main" java.lang.NoClassDefFoundError and this is because maven is not able to add the dependent jar specified in the section. [it is not available during run time]
Could anybody let me know the best possible way for me to copy the systemPath jars to the jar that is being generated by maven?
I looked at maven-shade-plugin and maven-assembly-plugin and could not find much luck with both of them. Any help would be appreciated!
Try to add this to your maven-jar-plugin configuration
<addClasspath>true</addClasspath>
<classpathPrefix>*path to dependencies*</classpathPrefix>
Also to manage your dependencies you can use Maven dependency plugin
In the build section of your pom, you can use:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com:library:**</include>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then, on the command line, you can run mvn package, it will show in the output which jars it is excluding. Then, you can include selected jars accordingly. If you then get NoClassDefFoundError, you may need to point Java to your class, by for example using com.company.Main as an argument to the java command.

What is the difference between the jars produced by Maven's Shade plugin?

In my Maven project, I have tried the maven-shade-plugin to produce an uber jar when I run mvn package. As a result, I get three jars in my target directory:
original-hello-world-0.1.0-SNAPSHOT.jar
hello-world-0.1.0-SNAPSHOT.jar
hello-world-0.1.0-SNAPSHOT-shaded.jar
I understand that first jar is the output of packaging without including dependencies. But the both the second and the third jars include dependencies and seem to be completely identical (they both have the same file size).
Is there supposed to be a difference between the second and third jars? If so, what is it? Otherwise, why are two identical jars being produced?
Here 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>mygroup</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>hello-world</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<main.class>org.example.HelloWorld</main.class>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>0.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- To build executable jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${main.class}</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- To use Maven to run main class -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
I copied your pom into a project of mine and only got two jars created when I ran mvn package.
Are you sure the "hello-world-0.1.0-SNAPSHOT-shaded.jar-shaded" jar is not the output of a previous maven run? Please try a mvn clean package and check what lands in the target folder.
If you would like an additional jar with the "-shaded" tag you can add the following to your pom in the shade plugin configuration:
<shadedClassifiedName>shaded</shadedClassifiedName>

Categories

Resources