【问题标题】:Gradle - Changing "compile" to "implementation" get errorGradle - 将“编译”更改为“实现”会出错
【发布时间】:2018-09-07 08:45:11
【问题描述】:

build.gradle - 部分:

    dependencies {
        compile 'org.codehaus.groovy:groovy-all:2.4.15'
    }

如果将compile 更改为implementation,在构建时会出现以下错误:

任务 ':greeter:compileJava 执行失败

改回来然后构建就会成功。

提示:

  • 项目使用Gradle 4.10,支持implementation
  • 还有其他依赖,在子项目中,改成implementation不会出问题,只有这一行会出问题。

有什么帮助吗?

【问题讨论】:

标签: gradle groovy build.gradle


【解决方案1】:

我想知道为什么您的主要 java 项目需要 groovy-all,因为它用于编译 groovy 库。 implementation配置应该够用了。

感谢您的链接,我确实重现了您的问题并专注于错误:

:greeter:compileJava FAILED
/mnt/star/git_repository/workspace/groovy_workplace/gradle/hello/hello_multi_project/greeter/src/main/java/greeter/Greeter.java:5: error: cannot access GroovyObject
        final String output = GreetingFormatter.greeting(args[0]);
                                               ^
  class file for groovy.lang.GroovyObject not found

为什么编译你的java代码需要GroovyObject?我查看了GroovyObject 源代码,它击中了我:

package groovy.lang;

/**
 * The interface implemented by all Groovy objects.
 * <p>
 * Especially handy for using Groovy objects when in the Java world.
 *
 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 */
public interface GroovyObject {
[...]

由所有 Groovy 对象实现的接口。特别方便 在 Java 世界中使用 Groovy 对象

GreetingFormatter 是一个 Groovy 对象,隐式实现了GroovyObject。这就是为什么在编译类路径中需要 groovy-all 的原因,即它应该在 groovy 库中声明为 compile 依赖项。

【讨论】:

  • 其实我是根据这个官方教程尝试通过gradle构建多项目:guides.gradle.org/creating-multi-project-builds,它在groovy子项目中使用compile,而java子项目使用那个groovy子项目编译。我以为implementationcompile 做了同样的事情,看来他们还是不同的......
  • 读了之后还是不知道什么时候用compile,链接有点复杂,对于这样的错误情况,我暂时只用compile
  • implementation 禁用传递依赖并将它们保留在项目中
  • 好的,我明白了,但我也试过api,它也不起作用......因此似乎compile对于这种情况是必要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 2011-02-09
  • 2020-08-12
相关资源
最近更新 更多