Gradle sourceCompatibility 对子项目没有影响 [英] Gradle sourceCompatibility has no effect to subprojects

查看:35
本文介绍了Gradle sourceCompatibility 对子项目没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的机器上安装了 Java 6 和 7.Gradle 使用 1.7(使用 gradle -v 检查).但是我需要编译我的代码以与 Java 1.6 兼容.据我了解文档,我可以使用 sourceCompatibility 属性来执行此操作(间接使用默认为 sourceCompatibilitytargetCompatibility).

I have Java 6 and 7 installed on my machine. Gradle uses 1.7 (checked using gradle -v). But I need to compile my code to be compatible with Java 1.6. As far as I understand the documentation I can use the sourceCompatibility property to do so (and indirectly the targetCompatibility which defaults to the sourceCompatibility).

所以我在我的构建文件中添加了以下行(在根级别,而不是在任何闭包中):

So I added the following line to my build file (on the root level, not in any closure):

sourceCompatibility = 1.6

(可以肯定的是,我还在一些试验中添加了 targetCompatibility = 1.6,但这应该没什么区别)

(to be sure I also added the targetCompatibility = 1.6 in some trials, but that should not make a difference)

为了检查结果是否真的与 1.6 兼容,我将生成的 jar、cd 解压到 WEB-INF/classes 文件夹中并使用 javap -verbose 在我遇到的第一个 .class 文件中.但是不管我设置了target的兼容性,还是用1.5代替了1.6,还是指定为字符串('1.6'),每次javap的结果都是

To check whether the result was actually compatible with 1.6 I unzipped the resulting jar, cd into the WEB-INF/classes folder and used javap -verbose on the first .class file I encountered. But no matter whether I set the target compatibility or whether I used 1.5 instead of 1.6 or whether I specified it as string ('1.6'), each time the result of javap was

minor version: 0
major version: 51

Afaik 这意味着它是 Java 1.7 字节码,这是错误的.

Afaik this means it is Java 1.7 Bytecode, which is wrong.

任何想法为什么 sourceCompatibility 设置不起作用?还是 javap 不是检查兼容性的正确方法?

Any ideas why the sourceCompatibility-setting doesn't work? Or is javap not the correct way to check the compatibility?

更新:是的,这实际上是一个多项目构建,但我只检查了其中一个子项目的构建结果.在这个子项目的构建文件中,我进行了上述更改以确保它们得到实际应用.此外,我在根项目的构建文件中添加了以下内容(正如@Vidya 所建议的那样):

UPDATE: Yes, this is actually a multi-project build but I only checked one of the subprojects' build results. In this subproject's build file I made the mentioned changes to be sure they are actually applied. In addition, I added the following in the root project's build file (as @Vidya proposed as well):

allprojects {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}

但这也无济于事.

更新 2:我在相关的 build.gradle 文件中使用此代码段检查了 sourceCompatibility 的设置:

UPDATE 2: I checked the setting of sourceCompatibility with this snippet in the relevant build.gradle files:

compileJava.doFirst {
    println "source compatibility " + sourceCompatibility
}

虽然我尝试将其设置为 1.6,但它显示我的 sourceCompatibility 设置为 1.7.当我提取最简单的子项目并自行构建时,正确设置了 sourceCompatibility 并且 Java 字节代码与 1.6 兼容.然而,即使是这个子项目在多项目构建中使用时也使用了错误的 sourceCompatibility.

It revealed that my sourceCompatibility is set to 1.7 although I tried to set it to 1.6. When I extracted the simplest subproject and built in on its own the sourceCompatibility is set correctly and the Java Byte code is compatible to 1.6. However, even this sub-project uses the wrong sourceCompatibility when used in the multi project build.

顺便说一句:我在一些子项目中使用的插件有:javawarjettygwt

BTW: The plugins I use in some of the sub projects are: java, war, jetty, gwt

更新 3:我将构建的脚本更改为只使用 java 插件(因此只构建了一些 jar)并删除了 warjettygwt 的使用> 插件.但是尽管我在 allprojects 部分和一些子项目中设置了 sourceCompatibility 1.7,但所有项目仍然设置为 sourceCompatibility 1.7.现在在构建脚本中剩下的就是一些规范(maven、文件和其他子项目)的声明、要使用的存储库的声明、其他一些任务的声明(构建任务不依赖于,所以它不应该受到影响)和创建的 jar 文件的清单文件的配置(我向清单文件添加了规范和实现版本和标题).

UPDATE 3: I changed the built scripts to just use the java plugin (and thus just construct some jars) and removed the usage of the war, jetty and gwt plugin. But still all the projects are set to sourceCompatibility 1.7 despite me setting it in the allprojects section and in some of the sub projects. All that is left now in the build scripts is the declaration of some decencies (maven, files and other sub projects), the declaration of the repositories to use, the declaration of some others tasks (that the build-task does not depend on, so it shouldn't be affected) and the configuration of the manifest file for the created jar files (I add a specification and an implementation version and title to the manifest file).

我看不出这会如何影响 sourceCompatibility 设置.

I don't see how any of that would affect the sourceCompatibility setting.

推荐答案

看来这种行为是由于指定了 sourceCompatibility before apply plugin: 'java',如果您尝试在 allprojects 中设置兼容性选项,就会发生这种情况.

It seems this behavior is caused by specifying the sourceCompatibility before apply plugin: 'java', which happens if you try to set the compatibility option inside allprojects.

在我的设置中,这种情况可以通过替换来解决:

In my setup, the situation can be solved by replacing:

allprojects {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}

与:

allprojects {
    apply plugin: 'java'
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}

如果其他人可以在不同的设置中验证这一点,我会很高兴.

Will be glad if anyone else can verify this in a different setup.

我仍然不确定这是否应该被报告为错误,但我相信这个解决方案比上面提到的解决方法更好(但是这非常有帮助).

I am still not sure whether this should be reported as a bug but I believe this solution is better than the work-around mentioned above (which has been very helpful however).

这篇关于Gradle sourceCompatibility 对子项目没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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