如何确保 Maven 依赖项在给定范围内?

Rid*_*del 3 java dependencies maven

在我正在从事的一个项目中,我们发现我们的 EAR 使用 Oracle Coherence 产品作为compile依赖项。这引发了已检测到的奇怪的类路径问题,并且一致性现在是一个provided依赖项。

然而,我想确保没有人再犯直接或不直接使用连贯性作为compile. 那么,是否有任何 Maven 插件/解决方案,在给定一组依赖约束的情况下,将确保所有 Maven 模块强制执行这些约束?

khm*_*ise 5

您应该深入了解maven-enforcer-plugin,它正是支持此类功能。

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <id>enforce-banned-dependencies</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <bannedDependencies>
                  <excludes>
                    <exclude>com.xyz:abc:*:jar:compile</exclude>
                    <exclude>com.xyz:abc:*:jar:runtime</exclude>
                    <exclude>com.xyz:abc:*:jar:test</exclude>
                  </excludes>
                </bannedDependencies>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Run Code Online (Sandbox Code Playgroud)