编译具有不同java源代码兼容性的项目 [英] Compiling a project with different java source compatibility

查看:158
本文介绍了编译具有不同java源代码兼容性的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个不同项目的多项目配置。

其中两个项目取决于第三个项目,我命名为'core'。
根据项目的不同,'core'必须被编译成一个jar,源代码分别为1.4和1.6,输出到core-1.4.jar和core-1.6.jar。



是否可以使用单个build.gradle来完成此操作,或者执行此操作的最佳方法是什么?如何在两个项目的每一个的依赖项中指定特定的jar文件?解决方案

这个问题从根本上来说是关于如何生成并消费基于相同Java代码的工件的两种变体。假如你真的需要生产两个Jars,它们的目标兼容性不同(我首先想到的),实现这一目标的一种方法是使用Java插件的 main 源代码设置(以及随之而来的任务)以生成第一个变体,并设置一个新源以生成第二个变体。另外,需要通过自己的配置发布第二个变体,以便依赖项目可以引用它。这可能如下所示:

core / build.gradle:

pre $ code > apply plugin:java

sourceCompatibility = 1.4

sourceSets {
main1_4 {
def main = sourceSets.main
java。 srcDirs = main.java.srcDirs
resources.srcDirs = main.resources.srcDirs
compileClasspath = main.compileClasspath
runtimeClasspath = main.runtimeClasspath
}
}

compileJava {
targetCompatibility = 1.6
}

compileMain1_4Java {
targetCompatibility = 1.4
}

jar {
archiveName =core-1.6.jar
}

main1_4Jar {
archiveName =core-1.4.jar
}

配置{
archives1_4
}

工件{
archives1_4 main1_4Jar
}


在依赖项目中:

  dependencies {
编译项目(:core)//依赖于1.6版本
编译项目(路径::core,配置:archives1_4)//依赖于1.4版本
}

所有这些都可以(但不一定)在同一个构建脚本中完成。有关详细信息,请参阅 Gradle用户指南中的多项目构建一章。

I have a multiproject configuration with 3 different projects.

2 of the projects depend on the 3rd project, which I named 'core'. Depending on the project, 'core' has to compile to a jar, with source compatibility for 1.4 and 1.6 respectively, into outputs core-1.4.jar and core-1.6.jar.

Is it possible to do this with a single build.gradle, or what would be the best way to do this? How can I specify which jar in particular in my dependencies for each of the 2 projects?

解决方案

The question is fundamentally about how to produce and consume two variations of an artifact that are based off the same Java code. Provided that you really need to produce two Jars that only differ in their target compatibility (which I would first question), one way to achieve this is to use the Java plugin's main source set (and the tasks that come along with it) to produce the first variation, and a new source set to produce the second variation. Additionally, the second variation needs to be published via its own configuration so that depending projects can reference it. This could look as follows:

core/build.gradle:

apply plugin: "java"

sourceCompatibility = 1.4

sourceSets {
    main1_4 {
        def main = sourceSets.main
        java.srcDirs = main.java.srcDirs
        resources.srcDirs = main.resources.srcDirs
        compileClasspath = main.compileClasspath
        runtimeClasspath = main.runtimeClasspath
    }
}

compileJava {
    targetCompatibility = 1.6
}

compileMain1_4Java {
    targetCompatibility = 1.4    
}

jar {
    archiveName = "core-1.6.jar"
}

main1_4Jar {
    archiveName = "core-1.4.jar"
}

configurations {
    archives1_4
}

artifacts {
    archives1_4 main1_4Jar
}

In depending projects:

dependencies {
    compile project(":core") // depend on 1.6 version
    compile project(path: ":core", configuration: "archives1_4") // depend on 1.4 version
}

All of this can (but doesn't have to) be done in the same build script. See the "multi-project builds" chapter in the Gradle User Guide for details.

这篇关于编译具有不同java源代码兼容性的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆