如何解决maven EnforcedBytecodeVersion失败?

Vis*_*ena 2 java maven-3 maven maven-enforcer-plugin

当尝试运行 Maven Enforcer 时,由于某些类符合 1.9,而整个项目仅限于 1.8,因此失败。以下是日志的堆栈跟踪。该特定依赖项是由不同的 jar 拉取的,由于它具有编译时依赖项,因此无法排除该依赖项。

[INFO] Checking unresolved references to org.codehaus.mojo.signature:java18:1.0
[INFO] Restricted to JDK 1.8 yet javax.json.bind:javax.json.bind-api:jar:1.0:compile contains module-info.class targeted to JDK 1.9
[WARNING] Rule 14: org.apache.maven.plugins.enforcer.EnforceBytecodeVersion failed with message:
Found Banned Dependency: javax.json.bind:javax.json.bind-api:jar:1.0
Run Code Online (Sandbox Code Playgroud)

khm*_*ise 5

看来您误解了enforceBytecodeversion的意图...它将检查所有依赖项,如果它们使用字节码作为更新版本,这意味着高于JDK 8,只是提升 maxJdkVersion 并不能解决问题。该问题与您正在使用的依赖项有关......

The dependency: javax.json.bin:javax.json.bind-api 
contains a `module-info.class` file which is related to JDK 9 ...
Run Code Online (Sandbox Code Playgroud)

如果您确定该依赖项中的所有代码都不使用 JDK 9 特定的内容,则您必须module-info.class从检查执行器规则中排除...

更新:这可以通过使用以下方法来实现:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M1</version>
        <executions>
          <execution>
            <id>enforce-bytecode-version</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <enforceBytecodeVersion>
                  <maxJdkVersion>1.8</maxJdkVersion>
                  <ignoreClasses>
                    <ignoreClass>module-info</ignoreClass>
                  </ignoreClasses>
                </enforceBytecodeVersion>
              </rules>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>extra-enforcer-rules</artifactId>
            <version>1.0-beta-9</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Run Code Online (Sandbox Code Playgroud)