4

我正在尝试从一些 groovy 代码生成文档,但 Gradle 失败,因为它在尝试编译代码时无法导入依赖项。这是意料之中的,因为在这些依赖项可用之前,代码需要在特定的上下文中运行。我不知道为什么它似乎应该只是解析源代码以提取文档,但它甚至试图编译代码,但这是一个附带问题。

我的 build.gradle:

apply plugin: 'groovy'

repositories {
    mavenCentral();
}

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


sourceSets {
    main {
        groovy {
            srcDirs = ['src/org/mysource']
        }
    }
}

我已经尝试了各种方法,例如excludegroovyCompileCompileGroovy任务中,但这没有任何区别。我无法在这种情况下提供依赖项。欢迎提出其他建议。任何能够确定使用 asciidoc 记录 groovy 的可行解决方案的人都可以获得奖励积分,但我也未能实现。

4

1 回答 1

1

:compileGroovy在运行groovydoc任务时,您有两个选项可以禁用。首先是一个简短的例子。我有一个 Groovy Gradle 项目,我在其中引入了一些使其编译失败的更改:

gradle groovydoc

输出:

> Task :compileGroovy FAILED
startup failed:
/home/wololock/workspace/upwork/jenkins-continuous-delivery-pipeline/src/com/upwork/util/MapUtils.groovy: 29: [Static type checking] - Cannot find matching method com.upwork.util.MapUtils#merge(V, java.lang.Object). Please check if the declared type is right and if the method exists.
 @ line 29, column 56.
    = result[k] instanceof Map ? merge(resu
                                 ^
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileGroovy'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s
1 actionable task: 1 executed

现在让我们仔细看看允许我在不编译此源代码的情况下生成 groovydoc 的选项。


compileGroovy1.从命令行禁用

您可以在运行Gradle 任务时使用-xswitch 来禁用:compileGroovygroovydoc

gradle clean groovydoc -x compileGroovy

输出:

> Task :groovydoc 
Trying to override old definition of task fileScanner

BUILD SUCCESSFUL in 2s
2 actionable tasks: 2 executed


compileGroovy2.禁用build.gradle

如果您不想使用-xswitch 并且您希望compileGroovy在运行时groovydoc禁用任务,那么您可以compileGroovy通过修改以下任务图来禁用build.gradle

gradle.taskGraph.whenReady { graph ->
  if (graph.hasTask(':groovydoc')) {
    compileGroovy.enabled = false
  }
}

只需将其添加到build.gradle文件中的某个位置即可。现在当你执行:

gradle groovydoc

该任务compileGroovy将被禁用并且源代码不会被编译。

> Task :groovydoc 
Trying to override old definition of task fileScanner

BUILD SUCCESSFUL in 2s
2 actionable tasks: 2 executed
于 2018-07-14T11:26:48.860 回答