NoClassDefFoundError when using executions in maven - java

Update
Based on the comments of others and my own tests, this appears to only be a problem with Maven 3.1.1 - I've logged a bug with codehaus
I've created the smallest maven POM file I can to highlight the problem (see below).
I'm using an email server plugin (for integration testing). I can launch the plugin no problem:
mvn emailserver:run
[INFO] --- emailserver-maven-plugin:1.1.1:run (default-cli) # project ---
[INFO] Starting Greenmail mail server...
[INFO] Started Greenmail mail server
[INFO] Waiting for command from client
However, I have configured the plugin to run before the integration-test lifecycle. When run this way, I get a NoClassDefFoundError:
mvn integration-test
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: com/icegreen/greenmail/util/ServerSetup
When I execute maven with -X to see what's happening, in both cases it shows the same dependency tree as far as I can see, so I'm at a bit of a loss as to why I'm getting an exception which it's launched through lifecycle phases. Hopefully someone can help!
<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.mytest</groupId>
<artifactId>project</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Test Project</name>
<build>
<plugins>
<plugin>
<groupId>com.btmatthews.maven.plugins</groupId>
<artifactId>emailserver-maven-plugin</artifactId>
<configuration>
<monitorKey>emailserver</monitorKey>
<monitorPort>10025</monitorPort>
<serverName>greenmail</serverName>
<portOffset>13000</portOffset>
<useSSL>false</useSSL>
</configuration>
<executions>
<execution>
<id>run-mail</id>
<goals>
<goal>run</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-mail</id>
<goals>
<goal>stop</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Maven information:
> mvn -v
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 16:22:22+0100)
Maven home: /opt/apache-maven
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /opt/jdk1.7.0_45/jre
Default locale: en_IE, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-13-generic", arch: "amd64", family: "unix"

NoClassDefFoundError means that the class is present in the classpath at Compile time, but it doesn't exist in the classpath at Runtime.
So clean the project through eclipse, .class files are again will regenerate. Run the maven project again with help of Maven

Related

Type org.springframework.boot.maven.RepackageMojo not present

I am trying to perform "mvn clean package" in my java project and facing the following error :
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:3.0.2:repackage failed: Unable to load the mojo 'repackage' in the plugin 'org.springframework.boot:spring-boot-maven-plugin:3.0.2' due to an API incompatibility: org.codehaus.plexus.component.repository.exception.ComponentLookupException: org/springframework/boot/maven/RepackageMojo has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>org.springframework.boot:spring-boot-maven-plugin:3.0.2
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
My maven version is :
Apache Maven 3.8.7 (b89d5959fcde851dcb1c8946a785a163f14e1e29)
Maven home: /opt/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-56-generic", arch: "amd64", family: "unix"
Following pom.xml file mojo package :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>javaclass.java</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
I still faced the error when my maven version was : Apache Maven 3.6.3
Although the problem got resolved when I upgraded my maven java version to 17 but I want to compile the java code with java version 11.
Does anyone know the fix?

Docker image build failed on Mac M1 chip

I'm trying to build a simple docker image, inside a maven project, adding the image build as part of the maven build process:
<build>
<finalName>my-api</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Docker -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.6</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<!-- <goal>push</goal> -->
</goals>
</execution>
</executions>
<configuration>
<repository>reponame/${project.name}</repository>
<tag>${project.version}</tag>
<skipDockerInfo>true</skipDockerInfo>
</configuration>
</plugin>
</plugins>
</build>
FROM openjdk:8-jdk-alpine
VOLUME /tmp
EXPOSE 8080
ADD target/*.jar app.jar
ENTRYPOINT [ "sh", "-c", "java -jar /app.jar" ]
But it fails, always get the same error trace, no matter which image I use, the error persists.
Error:
Caused by:
com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException:
java.lang.UnsatisfiedLinkError: could not load FFI provider
jnr.ffi.provider.jffi.Provider
Caused by: java.lang.UnsatisfiedLinkError:
java.lang.UnsatisfiedLinkError:
/private/var/folders/hz/rgppp8250rsdp86kf_tfjvqw0000gp/T/jffi8502916075702391528.dylib:
dlopen(/private/var/folders/hz/rgppp8250rsdp86kf_tfjvqw0000gp/T/jffi8502916075702391528.dylib,
0x0001): tried:
'/private/var/folders/hz/rgppp8250rsdp86kf_tfjvqw0000gp/T/jffi8502916075702391528.dylib'
(fat file, but missing compatible architecture (have 'i386,x86_64',
need 'arm64e')), '/usr/lib/jffi8502916075702391528.dylib' (no such
file)
Other images I tried:
openjdk:13-alpine3.9
openjdk:8-jre-alpine3.9
azul/zulu-openjdk-alpine:17.0.2-17.32.13-arm64
My java version: openjdk version "11.0.13" 2021-10-19 LTS
My Docker version: Docker version 20.10.11, build dea9396
Thanks in advance.
It looks like the dockerfile-maven-plugin uses a runtime based on x86 architecture and won't run on Apple M1 (Arm).
The plugin is now inactive so you should try something else, for example the fabric8-maven-plugin
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.38.1</version>
<executions>
<execution>
<id>build</id>
<phase>pre-integration-test</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
Both of the spotify docker maven plugins are no longer maintained. They need to upgrade their dependency to a version that supports aarch64.
In our case there was significant refactoring needed to move to fabric8's plugin or to use maven exec so we wanted to continue to use spotify plugin.
Fortunately, you can force the plugin to use a particular dependency by adding a <dependencies> section to your plugin section.
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.6</version>
<dependencies>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.38.14</version>
</dependency>
</dependencies>
</plugin>
I met the same problem.According to the error message, it should be a connection error with the docker daemon.It seems that the plugin won't support Arm socket and you can do this:
(1) install socat
brew install socat
(2) set port forwarding
socat TCP-LISTEN:2375,range=127.0.0.1/32,reuseaddr,fork UNIX-CLIENT:/var/run/docker.sock
(3) set environment variable
export DOCKER_HOST=tcp://127.0.0.1:2375

Maven Not Downloading Plugin

I'm trying to set up Maven on a node project I'm working on for build reasons. I've added the following minimal pom.xml file to the project:
<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>project.id</groupId>
<artifactId>project-title</artifactId>
<version>0.0.1</version>
<name>project-name</name>
<description>Project Description</description>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<!--<executable>npm start</executable> -->
<executable>echo 'HELLO'</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
However, now when I run mvn install it starts, begins to download the plugin but never finishes. Here is the command and console:
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project-name 0.0.1
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.2.1/exec-maven-plugin-1.2.1.pom
Now when I curl or visit the address, I can see the xml file perfect. Can anyone give me any direction on how to head from here? I'm using Maven 3.1.1 and Java 7.
Check your settings.xml file, which Maven looks for in the following locations and order:
$HOME/.m2/settings.xml
$M2_HOME/conf/settings.xml
There is a section in the settings.xml file for proxies and mirrors, which may be why you're seeing inconsistent bhavior between curl and mvn
EDIT
See Maven docs on settings.xml for more details:
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.somewhere.com</host>
<port>8080</port>
<username>proxyuser</username>
<password>somepassword</password>
<nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts>
</proxy>
</proxies>
In some cases it takes long time for maven to understand network problems. I saw situations when it "hangs" for 15 minutes on secured network without proper config for proxy.
I didn't use curl, but I wonder if it can use system proxy settings? Maven should be configured separately, he doesn't understand them.

Maven generates duplicate pom.xml and pom.properties files in a jar

I package my Maven-based Spring app with:
mvn install-DskipTests -Peverything.
And something strange arises. In META-INF of generated jar, I find duplicate pom.xml and pom.properties files. Can someone explain it? Thanks.
Here is extracted 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.abc.xyz</groupId>
<artifactId>migrate-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>migrate-app</name>
<url>http://maven.apache.org</url>
<dependencies>
..
</dependencies>
<profiles>
<profile>
<id>everything</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Output for command:
mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 15:44:56+0700)
Maven home: C:\apache-maven-3.0.4\bin..
Java version: 1.6.0_30, vendor: Sun Microsystems Inc.
Java home: C:\Program Files (x86)\Java\jdk1.6.0_30\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
Add
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
</build>
to your pom.xml
It seems like this is a somewhat common problem that arises from a conflict in Eclipse, especially with the m2e-wtp integration.
Some people are indicating that it can be fixed by including clean in the list of goals to run, e.g. mvn clean package.
Also try to update your m2e installation and make sure you don't have multiple versions of m2e or m2eclipse (the pre-Indigo version) installed.
Some references to other people facing this problem:
http://dev.eclipse.org/mhonarc/lists/m2e-users/msg01995.html
https://issues.sonatype.org/browse/MECLIPSEWTP-209
https://issuetracker.springsource.com/browse/STS-2349
https://github.com/sonatype/m2eclipse-extras/issues/9
I know this issue is a bit old, but I thought I'd share an obscure bit of information on this subject. I was running across the same issue, and I found that when I ran maven with Eclipse open, AND my workspace preferences were set to refresh the workspace using native hooks or polling, the extra pom.xml and pom.properties files were added by eclipse! Closing eclipse or removing the refresh workspace option solved the problem for me. This was a hard one to track down, so hopefully someone else will benefit from this.
Follow up to #Dan's work around:
I tried your solution and it appears to work -- at first blush. However, it's just delaying the eventual failure.
It appears than anytime the workspace is refreshed the pom.xml and pom.properties files are created in the /classes directory. After that any later "package" goal will include the duplicate files in the .jar and thus break any later signing.
If the polling is on - you "fail-fast". If it's off you can fail at arbitrary times in future builds.
You need to do "mvn clean" and the do "mvn package" after that or just do "mvn clean package". if "mvn clean" fails due to any reason and if you do "mvn package" after that , you will see this issue again.
Not sure what you mean by duplicate but the jar created by maven does come with pom.xml and pom.properties in META-INF folder. See this section of the maven getting started guide.

Maven: Attaching multiple artifacts

I have a maven project that uses some custom jars [not found in any repository]. To add them along with the Maven build I am using the attach-artifact goal in maven. Following 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>com.maximus</groupId>
<artifactId>adminbuild</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>adminbuild</name>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>target</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar1.jar</file>
<type>jar</type>
</artifact>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar2.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
My projects that use these jars [customjar1.jar, customjar2.jar] have their dependency on the above pom file [adminbuild].
When I execute the mvn clean install command, I get the following error:
[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.7:attach-artifact (attach-artifacts) on project adminbuild: The artifact with same type and classifier: jar:null is used more than once. -> [Help 1]
Following is the output of mvn -version command:
Apache Maven 3.0.3 (r1075438; 2011-02-28 23:01:09+0530)
Maven home: C:\maven
Java version: 1.6.0_26, vendor: Sun Microsystems Inc.
Java home: C:\Java\jdk1.6.0_26\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
It seems that the way I am attaching the artifacts is incorrect. Should I not attach multiple artifacts in the same pom file? If yes then how. Please help.
Attached artifacts are normally used to install additional files created by your build, like the classes jar of a webapp, documentation or sources.
To add files to your maven repository so they are available as dependencies you should use the install-file goal.
Edit: Attached artifacts are identified by the same groupId and artifactId as your main project but with a different classifier. In your configuration, you did not specify the classifier, hence the error message.
You would want to have these custom jars in a repository manager like nexus so that they can be downloaded/used like a normal dependency jar.
Assuming that is not possible and seeing that these jars are in well-known location, perhaps you could specify these custom jars with system scope in the projects that need them?
<project>
...
<dependencies>
<dependency>
<groupId>my-group</groupId>
<artifactId>customjar1</artifactId>
<version>a.b.c</version>
<scope>system</scope>
<systemPath>${basedir}/../../resources/dependencies/java/customjar1.jar</systemPath>
</dependency>
</dependencies>
...
</project>
I used a secondary project to do this along with a multi-module.
https://stackoverflow.com/q/7908872/242042

Categories

Resources