I need your help. I'm just getting used to Java and have finished my first milestone in my private project. Now I wanted to use this milestone as an opportunity to deal with Jenkins and CI.
However, I run into problems when running the program via Maven in Jenkins. Maven always throws me a ClassNotFound exception when processing the Jenkins pipeline. But when I start the program locally in IntelliJ it runs without problems.
As far as I can see he can't find a POJO which I use for parsing XML using JAXB.
Why doesn't it find a class when I build using Jenkins but finds everything when I work locally, the POM is the same.
This is my POM:
<groupId>groupId</groupId>
<artifactId>rss_backend</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>dev.morphia.morphia</groupId>
<artifactId>core</artifactId>
<version>1.5.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>projects.rss_backend.MainApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
and this is the error i get when using jenkins/maven:
The following command runs and outputs the execution of your Java
application (which Jenkins built using Maven) to the Jenkins UI.
+ java -jar target/rss_backend-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
at projects.rss_backend.MainApp.main(MainApp.java:20)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more```
Do you have a clue what I'm doing wrong or how I can fix the problem?
Normal jars do not contain dependencies.
You need to build an executable jar as described here: How can I create an executable JAR with dependencies using Maven?
Probably you did not start the jar from IntelliJ with java -jar but with some IntelliJ mechanism that did the magic (i.e. the classpath) for you.
I would suggest to upgrade your jaxb deps: The following are working for me in JDK11+:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.4.0-b180830.0438</version>
</dependency>
As far as I can see, you are using Java 11, which means modules exist.
Did you declare your own module (create a module-info.java in your source folder)?
Most cases above Java 8 where I have seen a ClassNotFoundException, there was a module dependency missing. And that means that you cannot interact with whatever you are trying to do.
I am not familiar with Jenkins, however I assume that either
- your project requires something from Jenkins
or
- Jenkins tries to interact with something from your project via Reflection (e.g. building an object from a database).
In the first case, in your module-info.java, you would write
requires (transitive) jenkins-module-you-want-to-add ;
in the second case you would write
opens your-data-package to jenkins-module-that-needs-access ;
That is in my opinion the most likely source of the ClassNotFoundException you are receiving.
Anyhow, there is also the chance that maven is the source of your problem.
Related
I have an Eclipse workspace with several hierarchically dependent maven projects. Two of these projects (let's call them a and c to preserve the original structure) need a different version of the same library (graphhopper). I wrote the following poms:
a.pom:
<groupId>my.company</groupId>
<artifactId>a</artifactId>
<version>0.2.0</version>
<dependencies>
<dependency>
<groupId>com.graphhopper</groupId>
<artifactId>graphhopper-core</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
b.pom:
<groupId>my.company</groupId>
<artifactId>b</artifactId>
<version>0.2.0</version>
<dependencies>
<dependency>
<groupId>my.company</groupId>
<artifactId>a</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
c.pom:
<groupId>my.company</groupId>
<artifactId>c</artifactId>
<version>0.2.0</version>
<dependencies>
<dependency>
<groupId>my.company</groupId>
<artifactId>b</artifactId>
<version>0.2.0</version>
<exclusions>
<exclusion>
<groupId>com.graphhopper</groupId>
<artifactId>graphhopper-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.graphhopper</groupId>
<artifactId>graphhopper-core</artifactId>
<version>0.8.2</version>
</dependency>
</dependencies>
This pom works completely fine when building c directly via maven on the command line. The eclipse dependency hierarchy view of c.pom shows me too that c is dependent on graphhopper 0.8.2 and not on graphhopper 3.0.
However, the code view shows errors in project c whenever methods from 0.8.2 are used that don't exist anymore in 3.0. If I press F3 on one of those methods, it shows me 3.0 version of the file instead of the 0.8.2 version (I can still directly access the 0.8.2 version by searching my workspace for the class name, which yields 2 results: the 3.0 version and the 0.8.2 version). Building the project in Eclipse (via Maven->Update Project) also fails with the same errors the code view shows.
My collegues do not experience this problem although from what I can tell our eclipse workspaces have the same configurations. Any ideas what might be the cause of the trouble?
Basically, I use Liquibase for most of my Java projects (at least where db migrations are needed) and it worked always on Java 8. I have a requirement now to migrate an existing project to Java 13. The project compiles with the Java 8 and also with Java 13 (see the configuration below), however, Liquibase Maven plug-in (mvn liquibase:diff) throws an ClassLoadingException:
org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mypackage.TestEntity]: Could not load requested class : com.mypackage.TestEntity
here is the plugin configuration in my POM file:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.maven.plugin.version}</version>
<configuration>
<propertyFile>src/main/resources/db-migrations/liquibase/liquibase.properties</propertyFile>
<changeLogFile>src/main/resources/db-migrations/liquibase/db-changelog-master.xml</changeLogFile>
<diffChangeLogFile>${changeset.output.dir}/${timestamp} - ${desc}.xml</diffChangeLogFile>
<logging>info</logging>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate5</artifactId>
<version>${liquibase-hibernate5.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation-api.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
</dependencies>
</plugin>
and the liquibase.properties:
url=jdbc:postgresql://localhost:5432/user_registrations
username=postgres
password=somepwd
driver=org.postgresql.Driver
referenceUrl=hibernate:spring:com.mypackage?dialect=org.hibernate.dialect.PostgreSQL9Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
As I mentioned it at the beginning, the Java 13 build doesn't work:
<maven.compiler.release>13</maven.compiler.release>
<java.version>13</java.version>
All dependencies have the latest version.
Any idea what could be an issue here? Thanks for any input...
we are using eclipse+spring tool suite to build java application, we can start the application in eclipse IDE, but if we export as jar with all dependency jars, and run it on linux machine. we always gets error as following:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.ctrip.framework.apollo.demo.api.SimpleApolloConfigDemo.<clinit>(SimpleApolloConfigDemo.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
here is our POM.xml below:
<?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">
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>1.3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-demo</artifactId>
<name>Apollo Demo</name>
<packaging>jar</packaging>
<properties>
<java.version>1.7</java.version>
<github.path>${project.artifactId}</github.path>
</properties>
<dependencies>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${project.version}</version>
</dependency>
<!-- for spring demo -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- for spring boot demo -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- for refresh scope demo -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<!-- take over jcl -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</project>
2.1 we already included slf4j-log4j12 and slf4j-api as depdency.
2.2 the application can run normally in eclipse IDE window.
2.3 if we export executable jar including all dependencies, and ship jar on linux machine, run the jar file will get the above error.
2.4 we also checked generated jar package on windows with winRAR, the slf4j and log4j* jar package were there, see picture below.
we have struggled this for half day, but did not get any progress. Hope each expert can share with us some light or any suggestions. really appreciated!!!
You need to tell maven to compile the dependencys like this, the jars are compiled but you need the src in youre jar and not the jars in there:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and dependencys normaly have a <scope>compile</scope> tag, to tell the compile that they are needed at runtime
I've faced a similar problem few days ago, right I add the jar libraries but the core (scripts/classes) of the jar file are no included due some reason I was able to solve that problem by right click on the jar file go to properties and set the class path for the classes of that jar folder.
if you wanna check that the jar file has no class Def inside it explore the jar file from the explorer and double click on any class you'll see a page showing up and says there's no script for this class and it asks you to set the class path.
Another approach I tried which also worked for me is I've imported the same jar files on netbeans and they worked 100%
I downloaded this project(to convert docs to Pdf as the project name say) https://github.com/yeokm1/docs-to-pdf-converter on github then I tried to import in Eclipse but something is wrong. I got this error.
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
I also have errors(I have a red cross on this file) in my pom.xml but this is my first project with maven, I'm not able to detect the problem. Here the code:
<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>docs-to-pdf-converter</groupId>
<artifactId>docs-to-pdf-converter</artifactId>
<version>1.8</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
<version>2.32</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.12</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.odftoolkit.odfdom.converter.pdf</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.itext.extension</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
</project>
I tried to see many other questions with a lot of solutions, but no one worked for me.
1) I tried to delete the .m2 folder : Not Worked
2) Try to make right click on the project and then Maven-Update Project: same error. Not Worked
3)I tried to go to WINDOW-PREFERENCE-MAVEN-INSTALLATION and then I changed Embedded with the folder that contains Maven, in my case C:\Maven Not Worked
4)I tried to go to WINDOW-PREFERENCE-JAVA-INSTALLED JREs where at the begin I just had C:\Program Files\Java\jre1.8.0_65, then I have read somewhere that I need Jdk 7, so I downloaded and install it but it doesn't work too. So, Now I have these two C:\Program Files\Java\jdk1.7.0_79 and C:\Program Files\Java\jre1.8.0_65
I also post a picture( I have not enough reputation to post two, in ordert to see also the Path Environment Variable) about the Environment variable, maybe I don't see something obvious. enter image description here
I'm sorry in advance because it could be a very easy question, but as I said above this is my first project with maven/eclipse and I'm not expert in this field.
I downloaded this project, and it compiled fine using Eclipse Mars.
It sounds like your Maven installation does not know how to go out to the internet to find artifacts. Could there be a firewall in the way?
Also, in Eclipse go to Window/Preferences/Maven/User settings. Make sure the settings.xml is the same one as used by your external Maven installation (assuming you are using an external Maven engine). For instance, I have set both the global and user config files to C:\development\tools64\apache-maven-3.3.3\conf\settings.xml
I checked my settings.xml, and there is no configuration that points to the maven central repository - Maven 3 must just know where to find it.
On the first compile, you should see lots of info messages saying that Maven is downloading artifacts. The next time you compile, these messages will not appear, as the dependencies are now cached in your local repository.
Currently I am moving from karaf 3.0.5 to the newest version 4.0.2, I do assembly my own karaf with the karaf-maven-plugin. This is how my pom 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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>my.own.group</groupId>
<artifactId>assemble</artifactId>
<version>1.14.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>karaf-customize</artifactId>
<modelVersion>4.0.0</modelVersion>
<packaging>karaf-assembly</packaging>
<dependencies>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>framework</artifactId>
<version>${karaf.version}</version>
<type>kar</type>
</dependency>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>standard</artifactId>
<classifier>features</classifier>
<version>${karaf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf.karaf</groupId>
<artifactId>apache-cxf</artifactId>
<classifier>features</classifier>
<version>${cxf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.karaf.features</groupId>
<artifactId>enterprise</artifactId>
<classifier>features</classifier>
<version>${karaf.version}</version>
<type>xml</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>my.own.group</groupId>
<artifactId>kar-archive</artifactId>
<version>1.14.0-SNAPSHOT</version>
<type>pom</type>
<optional>true</optional>
</dependency>
<dependency>
<groupId>my.own.group</groupId>
<artifactId>karaf-branding</artifactId>
<version>1.14.0-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.alutam</groupId>
<artifactId>ziputils</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<version>${karaf.version}</version>
<extensions>true</extensions>
<configuration>
<javase>1.8</javase>
<bootFeatures>
<feature>jasypt-encryption</feature>
<feature>config</feature>
<feature>standard</feature>
<feature>region</feature>
<feature>management</feature>
<feature>bundle</feature>
<feature>package</feature>
<feature>kar</feature>
<feature>ssh</feature>
<feature>http</feature>
<feature>cxf</feature>
<feature>service-wrapper</feature>
<feature>jdbc</feature>
<feature>system</feature>
</bootFeatures>
</configuration>
</plugin>
</plugins>
</build>
</project>
With this configuration I do get the following error for several dependencies.
Caused by: org.osgi.framework.BundleException: Unsupported 'Bundle-ManifestVersion' value: 1
at org.apache.karaf.features.internal.resolver.ResourceBuilder.doBuild(ResourceBuilder.java:88)
at org.apache.karaf.features.internal.resolver.ResourceBuilder.build(ResourceBuilder.java:78)
I guess it happens within this parser. The reason is some old third party libraries have only Bundle-ManifestVersion: 1 set within their manifest file.
With karaf-maven-plugin 3.x this didn't matter at all. In contrast the karaf-maven-plugin 4.x fails with message above.
The only way I know to fix this is either rebuild from source or repack the hole jar again.
Is there any other way like a configuration for the karaf-maven-plugin to disable this constraint check? Because it would be awful lot of work to get all of this bundles up an running, again.
I faced the same error when updating to Karaf 4 and you have two choices:
Osgify conflictive dependency using bndtools:
Download bnd tools
Open a shell where you have downloaded bnd-2.4.0.jar.
Type:
java -jar bnd-2.4.0.jar wrap -o osgify-dependency.jar dependency.jar
where dependency.jar is your third party and osgify-dependency.jar will be the output.
Deploy to maven repo overriding the previous maven coordinates, or deploy your thirdparty with different coordinates.
mvn deploy:deploy-file -Dfile osgify-dependency.jar ..
Enable the wrap protocol
Add to you maven karaf plugin wrap and wrapper features.
So you can use wrap protocol to fix your corrupted MANIFEST.MF
Inside some karaf features:
<bundle>wrap:mvn:group.id/third.party.artefact.id/version</bundle>
Inside your pom.xml notice feature wrap / wrapper.
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
</executions>
<configuration>
<!-- no startupFeatures -->
<bootFeatures>
<feature>feature</feature>
<feature>jaas</feature>
<feature>shell</feature>
<feature>ssh</feature>
<feature>management</feature>
<feature>bundle</feature>
<feature>config</feature>
<feature>deployer</feature>
<feature>diagnostic</feature>
<feature>instance</feature>
<feature>kar</feature>
<feature>log</feature>
<feature>package</feature>
<feature>service</feature>
<feature>system</feature>
<feature>wrap</feature>
<feature>aries-blueprint</feature>
</bootFeatures>
<installedFeatures>
..
<feature>wrapper</feature>
</installedFeatures>
</configuration>
</plugin>
Here you have the full code where i tested:
https://github.com/antoniomaria/gazpachoquest/blob/master/karaf-assembly/pom.xml