如何使用插件依赖项和 java 模块系统修复 exec-maven-plugin?

问题描述

我正在使用模块系统将一些 Java 8 代码移植到 Java 11。

一个 jar 包含以下模块:

module de.powerstat.fb.generator
 {
  exports de.powerstat.fb.generator;
  // ....
 }

这是完美的工作。 现在在另一个 Maven 项目中,我在 pom 中引用了这个 jar/模块:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <includeProjectDependencies>false</includeProjectDependencies>
    <includePluginDependencies>true</includePluginDependencies>
    <executableDependency>
      <groupId>de.powerstat.fb</groupId>
      <artifactId>generator</artifactId>
    </executableDependency>
      <mainClass>de.powerstat.fb.generator/de.powerstat.fb.generator.CodeGenerator</mainClass>
    <arguments>
      <argument>${fb.hostname}</argument>
      <argument>${fb.port}</argument>
      <argument>${fb.username}</argument>
      <argument>${fb.password}</argument>
      <argument>${project.build.directory}</argument>
    </arguments>
    <cleanupDaemonThreads>false</cleanupDaemonThreads>
  </configuration>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>de.powerstat.fb</groupId>
      <artifactId>generator</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    
  </dependencies>
</plugin>

现在什么时候做

mvn clean install

这会导致 NullPointerException:

...
Caused by: java.lang.NullPointerException
  at org.codehaus.mojo.exec.AbstractExecMojo.findExecutableArtifact (AbstractExecMojo.java:277)
  at org.codehaus.mojo.exec.ExecJavaMojo.determineRelevantPluginDependencies (ExecJavaMojo.java:606)
  at org.codehaus.mojo.exec.ExecJavaMojo.addRelevantPluginDependenciesToClasspath (ExecJavaMojo.java:539)
  at org.codehaus.mojo.exec.ExecJavaMojo.getClassLoader (ExecJavaMojo.java:492)
  at org.codehaus.mojo.exec.ExecJavaMojo.execute (ExecJavaMojo.java:273)
  ...

现在注释掉以下部分时:

<!--
<executableDependency>
  <groupId>de.powerstat.fb</groupId>
  <artifactId>generator</artifactId>
</executableDependency>
-->

然后错误变为:

Caused by: java.lang.module.ResolutionException: Modules xml.apis and xercesImpl export package org.w3c.dom.html to module maven.model
  at java.lang.module.Resolver.resolveFail (Resolver.java:885)
  at java.lang.module.Resolver.failTwosuppliers (Resolver.java:797)
  at java.lang.module.Resolver.checkExportsuppliers (Resolver.java:718)
  at java.lang.module.Resolver.finish (Resolver.java:362)
  at java.lang.module.Configuration.<init> (Configuration.java:141)
  at java.lang.module.Configuration.resolve (Configuration.java:424)
  at java.lang.module.Configuration.resolve (Configuration.java:256)
  at org.codehaus.mojo.exec.LoaderFinder.find (LoaderFinder.java:54)
  at org.codehaus.mojo.exec.ExecJavaMojo.getClassLoader (ExecJavaMojo.java:498)
  at org.codehaus.mojo.exec.ExecJavaMojo.execute (ExecJavaMojo.java:273)

所以我的问题是,我能做些什么来解决这种情况?也许我做错了什么?或者是插件中的错误?如果它是插件中的错误 - 是否有某种解决方法(因为插件看起来没有维护)?

只是让你知道 - 我在 MacOS 上使用 maven 3.6.3。

@Extension1:

今天在使用上面注释掉的代码进行编译时,我收到一条不同的错误消息:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default) on project gentr64: Execution default of goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java Failed: Module sisu.inject.bean contains package org.sonatype.guice.asm,module sisu.inject.plexus exports package org.sonatype.guice.asm to sisu.inject.bean -> [Help 1]

所以它看起来更像是插件的问题,或者可能是 maven 本身的问题。

我想问题可能在于我试图在模块中执行一个 java 类并且因为 maven 没有模块化所以失败了? 也许有人可以证实或证伪这个理论?

@扩展 2:

从我的 pom 中删除 jacoco 插件后,我得到了另一个不同的错误

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java (default) on project gentr64: Execution default of goal org.codehaus.mojo:exec-maven-plugin:3.0.0:java Failed: Module maven.core contains package org.apache.maven.plugin,module maven.plugin.api exports package org.apache.maven.plugin to maven.core -> [Help 1]

解决方法

最后,当尝试在模块化 jar 上使用 exec:java 时,maven 3.6.3 和 exec-maven-plugin 无法正常工作。

作为解决方法/解决方案,我使用了以下内容:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>3.1.2</version>
  <executions>
    <execution>
      <id>copy</id>
      <phase>initialize</phase>
      <goals>
        <goal>copy</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>de.powerstat.fb</groupId>
            <artifactId>generator</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>jar</type>
            <overWrite>false</overWrite>
            <outputDirectory>target</outputDirectory>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.0.0</version>

  <configuration>        
    <executable>java</executable>            
    <arguments>
      <argument>-jar</argument>
      <argument>target/generator-1.0-SNAPSHOT.jar</argument>

      <argument>${fb.hostname}</argument>
      <argument>${fb.port}</argument>
      <argument>${fb.username}</argument>
      <argument>${fb.password}</argument>
      <argument>${project.build.directory}</argument>
    </arguments>
  </configuration>

  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>exec</goal>
      </goals>
    </execution>
  </executions>
</plugin>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...