Java Gradle sourceCompatibility 对子项目没有影响
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21028438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle sourceCompatibility has no effect to subprojects
提问by Joachim Kurz
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
).
我的机器上安装了 Java 6 和 7。Gradle 使用 1.7(使用 进行检查gradle -v
)。但是我需要编译我的代码以与 Java 1.6 兼容。据我了解文档,我可以使用该sourceCompatibility
属性来执行此操作(并间接targetCompatibility
默认为 sourceCompatibility
)。
So I added the following line to my build file (on the root level, not in any closure):
所以我将以下行添加到我的构建文件中(在根级别,而不是在任何闭包中):
sourceCompatibility = 1.6
(to be sure I also added the targetCompatibility = 1.6
in some trials, but that should not make a difference)
(可以肯定的是,我还在targetCompatibility = 1.6
一些试验中添加了,但这应该没有区别)
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
为了检查结果是否真的与 1.6 兼容,我将生成的 jar 解压缩cd
到WEB-INF/classes
文件夹中并用于我遇到javap -verbose
的第一个.class
文件。但是不管我设置了target的兼容性,还是用1.5代替了1.6,还是指定为string( '1.6'
),每次javap的结果都是
minor version: 0
major version: 51
Afaik this means it is Java 1.7 Bytecode, which is wrong.
Afaik 这意味着它是 Java 1.7 字节码,这是错误的。
Any ideas why the sourceCompatibility
-setting doesn't work? Or is javap
not the correct way to check the compatibility?
任何想法为什么sourceCompatibility
-setting 不起作用?或者javap
不是检查兼容性的正确方法?
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):
更新:是的,这实际上是一个多项目构建,但我只检查了一个子项目的构建结果。在这个子项目的构建文件中,我进行了上述更改以确保它们得到实际应用。此外,我在根项目的构建文件中添加了以下内容(正如@Vidya 所建议的那样):
allprojects {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
But this didn't help either.
但这也无济于事。
UPDATE 2:I checked the setting of sourceCompatibility with this snippet in the relevant build.gradle files:
更新 2:我在相关的 build.gradle 文件中使用此代码段检查了 sourceCompatibility 的设置:
compileJava.doFirst {
println "source compatibility " + 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.
它显示我的 sourceCompatibility 设置为 1.7,尽管我尝试将其设置为 1.6。当我提取最简单的子项目并自行构建时,sourceCompatibility 设置正确,Java 字节代码与 1.6 兼容。但是,即使是这个子项目在多项目构建中使用时也使用了错误的 sourceCompatibility。
BTW: The plugins I use in some of the sub projects are: java
, war
, jetty
, gwt
顺便说一句:我在一些子项目中使用的插件是:java
, war
, jetty
,gwt
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).
更新 3:我将构建的脚本更改为只使用 java 插件(因此只构建了一些 jar)并删除了war
,jetty
和gwt
插件的使用。但是尽管我在该allprojects
部分和一些子项目中设置了它,但所有项目仍然设置为 sourceCompatibility 1.7 。现在在构建脚本中剩下的就是一些规范(maven、文件和其他子项目)的声明、要使用的存储库的声明、其他一些任务的声明(构建任务不依赖于,所以它不应该受到影响)以及创建的 jar 文件的清单文件的配置(我向清单文件添加了规范和实现版本和标题)。
I don't see how any of that would affect the sourceCompatibility setting.
我看不出这会如何影响 sourceCompatibility 设置。
采纳答案by Marwin
It seems this behavior is caused by specifying the sourceCompatibility
beforeapply plugin: 'java'
, which happens if you try to set the compatibility option inside allprojects
.
这种行为似乎是由指定sourceCompatibility
before引起的apply plugin: 'java'
,如果您尝试在其中设置兼容性选项,就会发生这种情况allprojects
。
In my setup, the situation can be solved by replacing:
在我的设置中,这种情况可以通过替换来解决:
allprojects {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
with:
和:
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).
我仍然不确定这是否应该报告为错误,但我相信这个解决方案比上面提到的解决方法更好(但是这非常有帮助)。
回答by Peter Niederwieser
Symptoms indicate that somewhere somebody is overwriting project.sourceCompatibility
. But given that there are many ways to customize Gradle, I can't say from a distance who that is.
症状表明某处有人正在覆盖project.sourceCompatibility
。但是鉴于有很多自定义 Gradle 的方法,我无法从远处说这是谁。
As a workaround, you can set the properties on the task level, which is what ultimately counts:
作为一种解决方法,您可以在任务级别设置属性,这才是最重要的:
tasks.withType(JavaCompile) {
sourceCompatibility = "1.6"
targetCompatibility = "1.6"
}
Add this to allProjects { ... }
block.
将此添加到allProjects { ... }
阻止。
回答by Rajkumar Nagarajan
You need to define compileJava tasks in build.gradle file, if you are using sourceCompatibility or targetCompatibility. Without compileJava tasks, both compatibility variables are displayed as unused variables in Intellij. I am using Gradle version 2.10.
如果使用 sourceCompatibility 或 targetCompatibility,则需要在 build.gradle 文件中定义 compileJava 任务。在没有 compileJava 任务的情况下,这两个兼容性变量在 Intellij 中都显示为未使用的变量。我正在使用 Gradle 2.10 版。