如何支持在摇篮多语言(Java和Scala)多个项目?(How to support multipl

2019-08-19 17:08发布

我想我们过时的Ant构建转换成摇篮。 该项目包含有关Java的50个子项目和10个斯卡拉子项目。 在Java项目仅包含Java和Scala项目只有斯卡拉。 每个项目都过得去的Java然后斯卡拉这大大减缓我们的构建建。

我想尽可能多的公共逻辑放在根目录的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导致此行为。 但是,如果我只应用正确的插件,以每个子项目我不能重构常见的配置为我的根build.gradle 。 另一种方法是只使用Scala的插件的一切,但是这是最好的办法吗?

我可以使用Java插件编译Java项目和Scala插件来编译Scala的项目,还是重构共同构建配置在所有我的项目到根build.gradle ? 什么是支持多国语言像这样在摇篮的最佳方式?

Answer 1:

怎么样是这样的:

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 ,需要封盖内的一些其他鉴别机制。



文章来源: How to support multiple projects in multiple languages (Java and Scala) in Gradle?