我的 Maven POM 是 YAML(感谢io.takari.polyglot),并且我很难在 YAML 中编写配置,因为示例具有值,但我相信我应该将 的maven-enforcer-plugin规则配置banTransitiveDependencies为不采用includes或excludes。
到目前为止,这是我的尝试:
modelVersion: 4.0.0
artifactId: my-app
groupId: com.adam.stuff
version: 3.3.1
packaging: jar
properties:
    spring.boot.version: 1.5.10.RELEASE
    spring.core.version: 4.3.14.RELEASE
dependencies:
  - artifactId: spring-boot-dependencies
    groupId: org.springframework.boot
    version: ${spring.boot.version}
    type: pom
  - artifactId: spring-boot-starter-web
    groupId: org.springframework.boot
    version: ${spring.boot.version}
  - artifactId: spring-boot-starter-test
    groupId: org.springframework.boot
    version: ${spring.boot.version}
  - artifactId: spring-context
    groupId: org.springframework
    version: ${spring.core.version}
  - artifactId: spring-web
    groupId: org.springframework
    version: ${spring.core.version}
build:
    plugins:
      - artifactId: maven-enforcer-plugin
        groupId: org.apache.maven.plugins
        version: …我在 Dropwizard 项目中使用 io.github.dropwizard-jobs ( https://github.com/dropwizard-jobs/dropwizard-jobs ) 作为调度程序。
添加了 POM 依赖项
<dependency>
    <groupId>io.github.dropwizard-jobs</groupId>
    <artifactId>dropwizard-jobs-guice</artifactId>
    <version>4.0.0-RELEASE</version>
</dependency>
集成通过 eclipse 在本地运行良好,但是在尝试使用创建包时mvn clean package,出现以下错误。
[WARNING]
Dependency convergence error for com.mchange:c3p0:0.9.5.4 paths to dependency are:
+-project_name:Scheduler:1.0.0.0
  +-io.github.dropwizard-jobs:dropwizard-jobs-guice:4.0.0-RELEASE
    +-io.github.dropwizard-jobs:dropwizard-jobs-core:4.0.0-RELEASE
      +-com.mchange:c3p0:0.9.5.4
and
+-project_name:Scheduler:1.0.0.0
  +-io.github.dropwizard-jobs:dropwizard-jobs-guice:4.0.0-RELEASE
    +-org.quartz-scheduler:quartz:2.3.1
      +-com.mchange:c3p0:0.9.5.3
and
+-project_name:Scheduler:1.0.0.0
  +-io.github.dropwizard-jobs:dropwizard-jobs-guice:4.0.0-RELEASE
    +-com.mchange:c3p0:0.9.5.4
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.DependencyConvergence failed with message:
Failed while enforcing releasability the error(s) are [
Dependency convergence error for com.mchange:c3p0:0.9.5.4 paths to dependency are:
+-project_name:Scheduler:1.0.0.0
  +-io.github.dropwizard-jobs:dropwizard-jobs-guice:4.0.0-RELEASE
    +-io.github.dropwizard-jobs:dropwizard-jobs-core:4.0.0-RELEASE
      +-com.mchange:c3p0:0.9.5.4
and
+-project_name:Scheduler:1.0.0.0
  +-io.github.dropwizard-jobs:dropwizard-jobs-guice:4.0.0-RELEASE
    +-org.quartz-scheduler:quartz:2.3.1
      +-com.mchange:c3p0:0.9.5.3
and …<?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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo</groupId>
  <artifactId>foo</artifactId>
  <version>0.0.1</version>
  <properties>
    <java.version>1.8</java.version>
    <mvn.version>3.6.3</mvn.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
          <execution>
            <id>enforce-versions</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <failFast>true</failFast>
              <rules>
                <requireMavenVersion>
                  <version>${mvn.version}</version>
                </requireMavenVersion>
                <requireJavaVersion>
                  <version>${java.version}</version>
                </requireJavaVersion>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
然后我验证了配置是否正确评估:
$ mvn validate -Djava.version=1.7 -X
...
DEBUG] Configuring mojo org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3, parent: sun.misc.Launcher$AppClassLoader@7852e922]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce' with basic configurator --> …当尝试运行 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
如何配置 maven-enforcer-plugin 以排除测试范围中的某些规则?
我有这样的配置:
<executions>
  <execution>
    <id>enforce-bytecode-version</id>
    <goals>
      <goal>enforce</goal>
    </goals>
    <configuration>
      <rules>
        <enforceBytecodeVersion>
          <maxJdkVersion>1.7</maxJdkVersion>
        </enforceBytecodeVersion>
      </rules>
      <fail>true</fail>
    </configuration>
  </execution>
</executions>
但我只想检查常规代码的 JDK 版本,而不是测试范围。