简体   繁体   English

使用 jenkins 和 maven 时出现 ClassNotFound 异常

[英]ClassNotFound Exception when using jenkins and maven

I need your help.我需要你的帮助。 I'm just getting used to Java and have finished my first milestone in my private project.我刚刚习惯了 Java 并完成了我在私人项目中的第一个里程碑。 Now I wanted to use this milestone as an opportunity to deal with Jenkins and CI.现在我想利用这个里程碑作为一个机会来处理 Jenkins 和 CI。

However, I run into problems when running the program via Maven in Jenkins.但是,我在通过 Jenkins 中的 Maven 运行程序时遇到了问题。 Maven always throws me a ClassNotFound exception when processing the Jenkins pipeline.在处理 Jenkins 管道时,Maven 总是向我抛出 ClassNotFound 异常。 But when I start the program locally in IntelliJ it runs without problems.但是当我在 IntelliJ 中本地启动程序时,它运行没有问题。

As far as I can see he can't find a POJO which I use for parsing XML using JAXB.据我所见,他找不到我用于使用 JAXB 解析 XML 的 POJO。

Why doesn't it find a class when I build using Jenkins but finds everything when I work locally, the POM is the same.为什么当我使用 Jenkins 构建时它没有找到 class 但是当我在本地工作时找到所有东西,POM 是一样的。

This is my POM:这是我的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:这是我在使用 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.普通 jars 不包含依赖项。

You need to build an executable jar as described here: How can I create an executable JAR with dependencies using Maven?您需要按照此处所述构建可执行 jar:如何使用 Maven 创建具有依赖关系的可执行 JAR?

Probably you did not start the jar from IntelliJ with java -jar but with some IntelliJ mechanism that did the magic (ie the classpath) for you.可能您没有使用 java -jar 从 IntelliJ 启动java -jar ,而是使用了一些 IntelliJ 机制为您创造了魔法(即类路径)。

I would suggest to upgrade your jaxb deps: The following are working for me in JDK11+:我建议升级您的 jaxb 部门:以下在 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.据我所知,您使用的是 Java 11,这意味着模块存在。
Did you declare your own module (create a module-info.java in your source folder)?您是否声明了自己的模块(在源文件夹中创建 module-info.java)?
Most cases above Java 8 where I have seen a ClassNotFoundException, there was a module dependency missing.在 Java 8 以上的大多数情况下,我看到了 ClassNotFoundException,缺少模块依赖项。 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我不熟悉 Jenkins,但我认为要么
- your project requires something from Jenkins - 你的项目需要来自 Jenkins 的东西
or或者
- Jenkins tries to interact with something from your project via Reflection (eg building an object from a database). - Jenkins 尝试通过反射与项目中的某些内容进行交互(例如,从数据库构建 object)。

In the first case, in your module-info.java, you would write在第一种情况下,在你的 module-info.java 中,你会写
requires (transitive) jenkins-module-you-want-to-add ;需要(传递) jenkins-module-you-want-to-add
in the second case you would write在第二种情况下,你会写
opens your-data-package to jenkins-module-that-needs-access ;打开你的数据包jenkins-module-that-needs-access

That is in my opinion the most likely source of the ClassNotFoundException you are receiving.在我看来,这是您收到的 ClassNotFoundException 的最可能来源。
Anyhow, there is also the chance that maven is the source of your problem.无论如何,maven 也有可能是您问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM