5

我正在尝试将我们过时的 Ant 构建转换为 Gradle。该项目包含大约 50 个 Java 子项目和 10 个 Scala 子项目。Java 项目仅包含 Java,Scala 项目仅包含 Scala。每个项目都是由 Java 然后 Scala 构建的,这大大减慢了我们的构建速度。

我想在根 build.gradle 文件中放置尽可能多的通用逻辑,如下所示:

subprojects {
    apply plugin: 'java'
    apply plugin: 'scala'

    sourceCompatibility=1.7
    targetCompatibility=1.7

    sourceSets {
        main {
            scala {
                srcDir 'src'
            }
            java {
                srcDir 'src'
            }
        }
        test {
            scala {
                srcDir 'test'
            }
            java {
                srcDir 'test'
            }
        }
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        scalaTools "org.scala-lang:scala-compiler:2.9.3"
        compile "org.scala-lang:scala-library:2.9.3"
    }

    // Use the sbt compiler (not scalac ant task)
    tasks.withType(ScalaCompile) {
        scalaCompileOptions.useAnt = false
    }

    task showCompileClasspath << {
       sourceSets.main.compileClasspath.each { println it }
    }

    test {
        systemProperty "user.dir", projectDir
    }
}

当我运行我的构建时,我得到以下输出,请注意:esb-server:compileJava:esb-server:compileScala包含相同的警告,因此类被构建了两次。另请注意,每个其他项目都包含一个compileJavacompileScala

:common:assemble
:esb-server:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:esb-server:compileScala
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:esb-server:processResources UP-TO-DATE
:esb-server:classes
:esb-server:jar
:esb-server:assemble
:plugins:compileJava UP-TO-DATE
:plugins:compileScala UP-TO-DATE
:plugins:processResources UP-TO-DATE
:plugins:classes UP-TO-DATE
:plugins:jar
:plugins:assemble
:raas-transform:assemble
:saxonee-helper:assemble
:scala-common:compileJava UP-TO-DATE
:scala-common:compileScala
/Users/iain.hull/code/trunk/ccdev/scala-common/src/com/workday/esb/assembly/audit/TreeAudit.scala:138: method first in trait IterableLike is deprecated: use `head' instead
    override def getLast():Option[AuditNode] = if (children.isEmpty) None else children.first.getLast
                                                                                        ^
one warning found
:scala-common:processResources UP-TO-DATE
:scala-common:classess
:scala-common:jar
:scala-common:assemble
:plugins:hibernatepersistence:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:plugins:hibernatepersistence:compileScala
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:plugins:hibernatepersistence:processResources
:plugins:hibernatepersistence:classes
:plugins:hibernatepersistence:jar
:plugins:hibernatepersistence:assemble

我知道apply plugin我的根目录顶部的两行build.gradle导致了这种行为。但是,如果我只对每个子项目应用正确的插件,我就无法将通用配置重构到我的 rootbuild.gradle中。另一种方法是对所有东西都使用 Scala 插件,但这是最好的方法吗?

我可以使用 Java 插件编译 Java 项目并使用 Scala 插件编译 Scala 项目,并且仍然将我所有项目的通用构建配置重构到根目录build.gradle吗?在 Gradle 中支持多种语言的最佳方式是什么?

4

1 回答 1

5

像这样的东西怎么样:

configure(subprojects.findAll {project  ->
    file('src/main/java').exists()
}) {
    apply plugin: 'java'
}

configure(subprojects.findAll {project  ->
    file('src/main/scala').exists()
}) {
    apply plugin: 'scala'
}

因为您的源目录是src,所以需要闭包内的其他一些鉴别器机制。

于 2013-04-05T08:55:10.943 回答