我正在开发一个需要并禁止特定依赖项的项目。我目前有一个独立的 Gradle 插件,它具有所需的 deps 并尝试全局排除一些代表。由于某种原因,在我应用该插件的项目中,我无法访问 Gradle 插件中实现的依赖项。我一直在测试通过 gradle dependency 实现插件的项目,并检查树中是否列出了所需的 deps。
在此示例中,我的插件正在拉入 group: 'org.slf4j', name:'slf4j-api', transitive: true
我需要能够看到/知道它是作为一个过渡部门被拉入实现项目。
Gradle 插件build.gradle:
buildscript {
repositories {
maven { url mainUrl }
}
}
apply plugin: 'java'
apply plugin: 'maven'
repositories {
maven {
url mainUrl
}
}
dependencies {
implementation gradleApi()
// ...
compile(group: 'org.slf4j', name:'slf4j-api', transitive: true)
}
实现Gradle插件build.gradle的项目:
buildscript {
repositories {
maven { url mainUrl
metadataSources {
mavenPom()
artifact()
}
}
}
configurations.all { resolutionStrategy.cacheDynamicVersionsFor 0, 'minutes' }
dependencies {
classpath 'com.gradlepluginpath:gradle-plugin:1.0.+'
}
}
plugins {
id 'java'
}
apply plugin: "gradle-plugin"
sourceCompatibility = 1.8
repositories {
maven{
url mainUrl
}
}
dependencies {
//...
}
运行 gradle dependency
后,org.slf4j
不会显示在实现项目的 dep 树中。
感谢任何帮助。
还有一个问题是全局排除不起作用。通过 gradle 插件 build.gradle 中的此 block ,log4j
模块仍显示在实现项目的 dep 树中。
configurations {
classpath {
all*.exclude group: 'org.apache.logging.log4j', module: '*'
}
}
最佳答案
您不会在应用程序的依赖项中看到 Gradle 插件依赖项,它们在设计上是完全独立的。
如果您的 Gradle 插件需要/使用 org.slf4j:slf4j-api
,那么您可以在 Gradle 插件的 Gradle 文件中定义它,您已完成此操作。
如果您的应用程序还需要 org.slf4j:slf4j-api
,那么您需要在应用程序的 Gradle 文件 (Kotlin DSL) 中定义依赖项:
dependencies {
implementation("org.slf4j:slf4j-api:1.7.30") {
isTransitive = true
}
}
There is also a problem with the global exclude not working. With this block in the gradle plugin build.gradle, log4j modules are still showing in the dep tree for the implementing project.
插件和项目的构建配置是两个不同的东西。您必须增强 Gradle 插件以使用 Gradle API 排除 log4j
:
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import java.util.HashMap;
import java.util.Map;
public class MyCustomGradlePlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
Map<String, String> log4j = new HashMap<>();
log4j.put("group", "org.apache.logging.log4j");
log4j.put("module", "*");
project.getConfigurations().all((configuration) -> configuration.exclude(log4j));
}
}
关于java - 访问另一个项目中的 Gradle 插件依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62476320/