java Java插件的Gradle“提供”依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12301513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:22:27  来源:igfitidea点击:

Gradle 'Provided' dependency for Java plugin

javagradle

提问by Chris Lefevre

I am attempting to compile several WAR files, all that depend on a common JAR module. In my Gradle build however, I cannot seem to get a 'Provided' like dependency to work with the Java plugin.

我正在尝试编译几个 WAR 文件,所有这些文件都依赖于一个通用的 JAR 模块。然而,在我的 Gradle 构建中,我似乎无法获得“提供”之类的依赖项来使用 Java 插件。

My compile looks like this:

我的编译看起来像这样:

apply plugin: 'java'
configurations{
    providedCompile
}

dependencies {
    compile module("org.springframework.amqp:spring-amqp:${springAmqpVersion}")
    compile module("org.slf4j:slf4j-api:${slf4jVersion}")
    compile module("org.slf4j:slf4j-ext:${slf4jVersion}")

    providedCompile "javax.servlet:servlet-api:${servletApiVersion}"

    runtime module("org.slf4j:jcl-over-slf4j:${slf4jVersion}")
    runtime module("org.slf4j:jul-to-slf4j:${slf4jVersion}")
    runtime module("org.slf4j:log4j-over-slf4j:${slf4jVersion}")

    sourceArchives module("org.springframework.amqp:spring-amqp:${springAmqpVersion}:sources")
    sourceArchives module("javax.servlet:servlet-api:${servletApiVersion}:sources")
}


sourceSets {
    main { compileClasspath += configurations.providedCompile }
}

However, that last bit is where it gets an exception. I have tried adding the servlet-api (Provided by Tomcat) to the configuration after the runtime dependencies would extend it, or simply putting it in as a compile module, then removing it from runtime dependencies later.

但是,最后一点是它出现异常的地方。我尝试在运行时依赖项扩展它之后将 servlet-api(由 Tomcat 提供)添加到配置中,或者只是将它作为编译模块放入,然后稍后从运行时依赖项中删除它。

I've attempted several different ways of modifying the dependencies, with my closest results being:

我尝试了几种不同的修改依赖项的方法,我最接近的结果是:

newRuntime = configurations.runtime.minus(configurations.providedCompile)
configurations.runtime = newRuntime

This last bit however, will generate the variable newRuntimewith the proper dependencies, however when I tried to reassign the variable back to the runtime configuration, it throws a "Cannot find property exception"

然而,最后一点将生成具有适当依赖项的变量newRuntime,但是当我尝试将变量重新分配回运行时配置时,它会抛出“找不到属性异常”

I found a lot of discussion of this exact problem on Gradle's bug tracking:Gradle-784

我在 Gradle 的错误跟踪中发现了很多关于这个确切问题的讨论:Gradle-784

However the main lead from that is from Spring who uses Maven with their gradle builds, which I am unfamiliar with.

然而,其中的主要领导来自 Spring,他使用 Maven 和他们的 gradle 构建,这是我不熟悉的。

The most promising link I found here on SO, but unfortunately I could not get the examples to work as well: SO Provided QuestionOf note for the Stack Overflow question the line that showed most promise:

我在 SO 上找到的最有希望的链接,但不幸的是,我无法让示例也能正常工作:SO 提供了 Stack Overflow 问题的注意事项,该行显示了最有希望的:

//Include provided for compilation
sourceSets.main.compileClasspath += configurations.provided

This line does not give an error like other attempts, however it appears that the providedCompile (My version of provided) dependency is not actually put on the compile classpath, as there is a classpath error when compilation is attempted.

此行不会像其他尝试一样给出错误,但是似乎providedCompile(我的provided 版本)依赖项实际上并未放在编译类路径上,因为在尝试编译时会出现类路径错误。

采纳答案by Natan Cox

I'm not 100% following your message but providedCompile is only allowed for 'war' plugin.

我不是 100% 关注您的消息,但仅允许为“War”插件提供编译。

apply plugin: 'war'

dependencies {
  // others go here
  providedCompile "javax.servlet:javax.servlet-api:${servletVersion}"
}

During 'war' step the servlet jar is not included.

在“War”步骤中,不包括 servlet jar。

回答by Peter Niederwieser

You have added a providedCompileconfiguration, but you aren't doing anything with it. Hence it won't make it on any class path. To put the configuration on the main compile class path, you can do:

您已经添加了一个providedCompile配置,但您没有对它做任何事情。因此它不会出现在任何类路径上。要将配置放在主编译类路径上,您可以执行以下操作:

sourceSets.main.compileClasspath += configurations.providedCompile

Likewise, to put it on the test compile class path:

同样,将它放在测试编译类路径上:

sourceSets.test.compileClasspath += configurations.providedCompile

回答by Max Shramko

You can use compilescope inside 'jar' modules and providedCompileinside 'war' module.

您可以在“jar”模块内使用编译范围,在“war”模块内使用providedCompile

War's providedCompilescope will override jar's compilescope.

War 的providedCompile范围将覆盖 jar 的compile范围。