Java.lang.linkageError:Loader' Bootstrap' java.lang的尝试重复的类定义。

发布于 2025-01-30 17:55:54 字数 3736 浏览 4 评论 0 原文

我在jenkins 中运行测试项目时会遇到以下错误

: (java.lang。$ jacoco位于模块Java.loader'bootstrap'的基准中)

jacoco在父pom和具有不同目标的当前测试pom中定义。

parent-pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.5</version>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13</version>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <id>validate-headless</id>
          <phase>validate</phase>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
          <configuration>
            <skip>true</skip>
            <propertyName>failsafe.argLine</propertyName>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

test-project-pom.xml

<properties>
  <maven.repo.local>${user.home}/.m2/repository</maven.repo.local>
  <itCoverageAgent>-javaagent:${maven.repo.local}/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=${basedir}/target/jacoco-it.exec</itCoverageAgent>
</properties>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-failsafe-plugin</artifactId>
      <configuration>
        <argLine>${failsafe.argLine} ${itCoverageAgent}</argLine>
        <testFailureIgnore>true</testFailureIgnore>
        <reuseForks>true</reuseForks>
      </configuration>
    </plugin>
  </plugins>
</build>

<profiles>
  <profile>
    <id>coverage</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.5</version>
          <executions>
            <execution>
              <id>prepare-agent-unit</id>
              <goals>
                <goal>prepare-agent</goal>
              </goals>
            </execution>
            <execution>
              <id>prepare-agent-integration</id>
              <goals>
                <goal>prepare-agent-integration</goal>
              </goals>
              <configuration>
                <propertyName>itCoverageAgent</propertyName>
              </configuration>
            </execution>
            <execution>
              <id>report-unit</id>
              <goals>
                <goal>report</goal>
              </goals>
            </execution>
            <execution>
              <id>report-integration</id>
              <goals>
                <goal>report-integration</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

如果 $ {itcoverageagent} 被从&lt; grt; grt; grline&gt; 在test-project-pom.xml中,它可以正常工作。我们如何为同一版本的jacoco执行来自父pom和测试项目pom的不同目标?

I am getting following error while running a test project in Jenkins

Caused by: java.lang.LinkageError: loader 'bootstrap' attempted duplicate class definition for java.lang.$JaCoCo. (java.lang.$JaCoCo is in module java.base of loader 'bootstrap')

Jacoco is defined in the parent pom and the current test pom with different goals.

parent-pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.8.5</version>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13</version>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <id>validate-headless</id>
          <phase>validate</phase>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
          <configuration>
            <skip>true</skip>
            <propertyName>failsafe.argLine</propertyName>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

test-project-pom.xml

<properties>
  <maven.repo.local>${user.home}/.m2/repository</maven.repo.local>
  <itCoverageAgent>-javaagent:${maven.repo.local}/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=${basedir}/target/jacoco-it.exec</itCoverageAgent>
</properties>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-failsafe-plugin</artifactId>
      <configuration>
        <argLine>${failsafe.argLine} ${itCoverageAgent}</argLine>
        <testFailureIgnore>true</testFailureIgnore>
        <reuseForks>true</reuseForks>
      </configuration>
    </plugin>
  </plugins>
</build>

<profiles>
  <profile>
    <id>coverage</id>
    <build>
      <plugins>
        <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>0.8.5</version>
          <executions>
            <execution>
              <id>prepare-agent-unit</id>
              <goals>
                <goal>prepare-agent</goal>
              </goals>
            </execution>
            <execution>
              <id>prepare-agent-integration</id>
              <goals>
                <goal>prepare-agent-integration</goal>
              </goals>
              <configuration>
                <propertyName>itCoverageAgent</propertyName>
              </configuration>
            </execution>
            <execution>
              <id>report-unit</id>
              <goals>
                <goal>report</goal>
              </goals>
            </execution>
            <execution>
              <id>report-integration</id>
              <goals>
                <goal>report-integration</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

If ${itCoverageAgent} is removed from <argLine> in test-project-pom.xml it works fine. How do we execute different goals from parent-pom and test-project-pom for the same version of Jacoco ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-02-06 17:55:54

当您使用 java.lang.linkageError 在使用Maven构建后的运行时失败时,通常表明同一依赖项的不同版本已加载到JVM中。当您的POM中的其他依赖项之一具有对同一Java库或Java类的不同版本的传递依赖性时,可能会出现这种冲突。

您可以使用 maven depenterency plugin 用于生成详细的概述通过在项目的根源上运行以下命令,您的依赖性以及其所有的及依赖性:

mvn dependency:tree

输出非常有用,但这是详尽的依赖项列表,有时可能最终会出现是一件好事。如果您的项目是正确的,则有一个 Include 选项,该选项可用于过滤输出,以便将其集中在特定的依赖项或依赖项上。例如,您可能会使用它,并使用Asterisk ('*')通配符专注于 jacoco 依赖项:

mvn dependency:tree -Dincludes=org.jacoco.*

选项允许多个过滤子句,用comma (',')隔开,并且使用以下格式结构支持多个级别的Maven依赖性过滤:

[groupId]:[artifactId]:[type]:[version]

您可以在MAVEN上找到更多的参考材料和文档依赖项插件的

如果您确实发现自己具有引起冲突的传递依赖性,则通常可以通过在引起问题的依赖关系的定义中添加&lt; exclusions&gt; 条目来解决问题:

<dependency>
  <groupId>org.open.source.library</groupId>
  <artifactId>problematic-artifact</artifactId>
  <scope>provided</scope>
  <exclusions>
    <exclusion>
      <groupId>org.jacoco</groupId>
      <artifactId>conflicting-transitive-artifact</artifactId>
    </exclusion>
  </exclusions>
</dependency>

这维护您的 问题:所需的依赖性,但不包括造成冲突的及传递依赖关系的分辨率,并将解决运行时链接错误。


实际上,我不认为您的根部问题是依赖性版本不匹配。我认为您的问题是构建插件配置碰撞。

MAVEN中的默认构建插件配置处理将自动传播父pom中定义的所有插件配置,直到儿童POMS(通常是您想要的)。

但是就您而言,它会破坏您的构建。但是,有一种简单的方法可以使您声明本质上是排除的方法 - 这只是您要排除构建插件配置继承而不是构建依赖关系。您可以使用:&lt; enstarited&gt; 标签关闭习惯插件配置传播,如下所示:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.5</version>
            <inherited>false</inherited>
            <!-- Additional build lifecycle goals,
                   plugin configuration entries, and
                      plugin execution definitions: ...
                                              -->
        </plugin>
     </plugins
 </build>

When you fail at runtime with a java.lang.LinkageError after building with Maven, it's usually an indication that different versions of the same dependency are getting loaded into the JVM. This type of conflict can arise when one of the other dependency entries in your POM has a transitive dependency on a different version of the same Java library or Java class.

You can use the Maven Dependency Plugin to generate a detailed outline of your dependencies, along with all of their transitive dependencies, by running the following command at the root of your project:

mvn dependency:tree

The output is extremely helpful, but it is an exhaustive dependency list, and can sometimes end up being too much of a good thing. If that's true for your project, there is an includes option that may be used to filter the output so that it's focused on a specific dependency or group of dependencies. As an example, you might use it is follows, with an asterisk ('*') wildcard to focus on just Jacoco dependencies:

mvn dependency:tree -Dincludes=org.jacoco.*

The includes option allows multiple filter clauses, separated by a comma (','), and also supports multiple levels of Maven dependency filtering, using the following format structure:

[groupId]:[artifactId]:[type]:[version]

You can find more reference material and documentation on the Maven Dependency Plugin's Filtering the dependency tree web page.

If you do find that you have a transitive dependency that's causing a conflict, the issue can usually be resolved by adding an <exclusions> entry within the definition of the dependency that's causing the problem:

<dependency>
  <groupId>org.open.source.library</groupId>
  <artifactId>problematic-artifact</artifactId>
  <scope>provided</scope>
  <exclusions>
    <exclusion>
      <groupId>org.jacoco</groupId>
      <artifactId>conflicting-transitive-artifact</artifactId>
    </exclusion>
  </exclusions>
</dependency>

This maintains your desired dependency but excludes resolution of the transitive dependency that is creating the conflict, and will resolve the runtime linkage error.


Actually, I don't believe your root problem is a dependency version mismatch; I think your issue is a build plugin configuration collision.

The default build plugin configuration handling in Maven will automatically propagate all plugin configuration defined in a parent pom down to the child poms - which is usually exactly what you want.

But in your case, it's ruining your build. There is however, a simple way for you to declare what is essentially an exclusion -- it's just that you want to exclude build plugin config inheritance rather than build dependencies. You can turn off the customary plugin config propagation using the: <inherited> tag as shown here:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.5</version>
            <inherited>false</inherited>
            <!-- Additional build lifecycle goals,
                   plugin configuration entries, and
                      plugin execution definitions: ...
                                              -->
        </plugin>
     </plugins
 </build>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文