maven assembly plugin does not include my classes inside built jar - java

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".

Related

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

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>

How to run my maven project (angular+springboot) on tomcat?

I have created a web application based on angular 8 and spring boot. I implemented the codebase locally and it is working fine.
My Angular code(client) is running on localhost:4200 and spring boot(server) is running on localhost:8080.
Till here everything is working as expected.
Now I want to deploy this whole web application as a single bundle so that I can deploy it as a single war on tomcat.
I am creating the war file using maven.
But when I deploy this war file on tomcat and start the tomcat I am not able to see the expected login page on the browser.
Basically, I don't have much understanding of maven and was following resource available in below link on the internet to generate the war file.
https://dzone.com/articles/building-a-web-app-using-spring-boot-angular-6-and
So I am not able to figure out whether the issue is with my build or the URL through which I am trying to access the resources.
if I deploy only the UI build, then if I hit localhost:8080, I am able to see the login page.
I am having three pom files as mentioned in the tutorial.
1. parent-pom
2. server-pom
3. ui-pom
Below are my pom files
parent-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.dzs.licenseGenerator</groupId>
<artifactId>lg-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath />
</parent>
<modules>
<module>LicenseGenerator_Backend</module>
<module>LicenseGenerator_UI</module>
</modules>
</project>
server-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>
<parent>
<groupId>com.dzs.licenseGenerator</groupId>
<artifactId>lg-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- lookup parent from repository -->
</parent>
<artifactId>LicenseGenerator_Backend</artifactId>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dzs.licenseGenerator</groupId>
<artifactId>LicenseGenerator_UI</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<tasks>
<echo>Displaying value of pom.xml element</echo>
<echo>[project.build.directory] ${project.build.directory}</echo>
<echo>[project.parent.basedir] ${project.parent.basedir}</echo>
</tasks>
<outputDirectory>${project.build.directory}/classes/resources/</outputDirectory >
<resources>
<resource>
<directory>${project.parent.basedir}/LicenseGenerator_UI/dist/lg-app/</directory >
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/lib/tomcat-*.jar</packagingExcludes>
<warName>lg-app</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
UI-pom.xml
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.dzs.licenseGenerator</groupId>
<artifactId>lg-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>LicenseGenerator_UI</artifactId>
<name>LicenseGenerator_UI</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<nodeVersion>v10.16.0</nodeVersion>
<npmVersion>6.9.0</npmVersion>
<workingDirectory>src/main/web/</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
<execution>
<id>prod</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run-script build</arguments>
</configuration>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Just to ensure whether my code structure is correct or not I am posting a screenshot of my Project Explorer in Eclipse.
Use ng build –prod command for generating production build artifacts.
Run this command in your UI project folder.
Post generation of production build you should see new folder named ‘dist’.
You have to use Maven resource plugin to package as single jar. As i can see you already have the plugin in you pom, just verify the directory folder to pint to dist folder.
After this just run maven clean install . After running this you should see jar with both Angular 6 & Spring Boot application on the target folder.
Execute with Java –jar command to launch the application, you should see the Angular application served from static folder.
You can use frontend-maven-plugin for kicking off your frontend build. Once the frontend build is completed, it will generate the resource files in dist directory.
After that you can use the maven-resources-plugin to copy the files from dist to the required location in the target directory.

Building multiple component zips for a multi-module Maven project

I am trying to find a good example on how you would build multiple zip files for a multi-module project with several runnable components using the maven-assembly-plugin.
My end result would hopefully have a single "dist" component that is capable of building all the necessary zip files.
<?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>
<parent>
<groupId>xyz.exampleproject</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<name>Project Distribution</name>
<artifactId>dist</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>component1-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>component2-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-bundles</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I would add one extra module per zip file that you want to create and configure the assembly plugin in there.

java.lang.NoClassDefFoundError while finding class in package

I have project (TeamCity plugin), written in Scala, packaged by maven and running on TeamCity(which uses Spring).
There is the dependency com.ullink.slack.simpleslackapi in my project. Now I try to update it (from 0.5.2 to 1.2.0) and become an error at runtime.
Error java.lang.NoClassDefFoundError: Could not initialize class
com.ullink.slack.simpleslackapi.impl.SlackWebSocketSessionImpl
The code that caused this exception is pretty simple:
val session = SlackSessionFactory.createWebSocketSlackSession(config.oauthKey)
My build/pom.xml file looks like
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>***</artifactId>
<groupId>***</groupId>
<version>1.0.0</version>
</parent>
<artifactId>build</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>***</groupId>
<artifactId>***-server</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>***</finalName>
<outputDirectory>${project.parent.build.directory}</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>plugin-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I've tried to append this dependency to this file and tried maven-shade-plugin — it does not help.
Could you advice what else can I try?

Java MAVEN: Could not find or load main class stigastatistics.stigastatistics.StatisticsGUI

I know, it was written here many times, but I tried solutions, but with no success. I have netbeans maven project with dependencies. I want to make executable jar file with dependencies. This is my pom file
<?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>sitgaStatistics</groupId>
<artifactId>StigaStatistics</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>
<mainClass>stigastatistics.stigastatistics.StatisticsGUI</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>gov.nih.imagej</groupId>
<artifactId>imagej</artifactId>
<version>1.42</version>
</dependency>
</dependencies>
<name>StigaStatisticsGUI</name>
</project>
when I clean and build projext it will create 2 jar files in target direcotry (StigaStatistics-1.0-SNAPSHOT.jar, StigaStatistics-1.0-SNAPSHOT-jar-with-dependencies.jar) when I start them from terminal like:
java -jar StigaStatistics-1.0-SNAPSHOT-jar-with-dependencies.jar
it will respond me error:
Error: Could not find or load main class stigastatistics.stigastatistics.StatisticsGUI
I really don't know what is wrong with this.
Thanks for answers
The problem is the misspelled fully-qualified name of the main class. There is a typo in the first part of the package name.
Either rename the package sitgastatistics.stigastatistics or correct the <mainClass>stigastatistics.stigastatistics.StatisticsGUI</mainClass> property definition so that the package name reads the same.

Categories

Resources