简体   繁体   English

Gradle'提供'对Java插件的依赖

[英]Gradle 'Provided' dependency for Java plugin

I am attempting to compile several WAR files, all that depend on a common JAR module. 我正在尝试编译几个WAR文件,所有这些都取决于一个常见的JAR模块。 In my Gradle build however, I cannot seem to get a 'Provided' like dependency to work with the Java plugin. 然而,在我的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 newRuntime with 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 Question Of 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. 此行不会像其他尝试一样给出错误,但似乎提供的Compile(我提供的版本)依赖项实际上并未放在编译类路径上,因为尝试编译时会出现类路径错误。

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。

You have added a providedCompile configuration, but you aren't doing anything with it. 您已添加了providedCompile配置,但您没有对其执行任何操作。 Hence it won't make it on any class path. 因此它不会在任何类路径上。 To put the configuration on the main compile class path, you can do: 要将配置放在主编译类路径上,您可以执行以下操作:

sourceSets.main.compileClasspath += configurations.providedCompile

Likewise, to put it on the test compile class path: 同样,将它放在测试编译类路径上:

sourceSets.test.compileClasspath += configurations.providedCompile

You can use compile scope inside 'jar' modules and providedCompile inside 'war' module. 您可以在'war'模块中使用编译范围,在'war'模块中使用providedCompile

War's providedCompile scope will override jar's compile scope. War 提供的 Compile范围将覆盖jar的编译范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM