[英]How do I make Gradle compile a source libary that uses newer java version?
I have a gradle project that is compiled for Java 7, and I'm told this project cannot be updated to use a newer version of Java because of a bug introduced in later java versions.我有一个为 Java 7 编译的 gradle 项目,我被告知这个项目无法更新为使用较新版本的 Java,因为在更高版本的 Java 版本中引入了一个错误。
I am trying to get this project to use a library that is compiled via java 8, and as a result I'm getting a major minor version 52 conflict during run time.我试图让这个项目使用通过 java 8 编译的库,结果我在运行时遇到了一个主要的次要版本 52 冲突。
Is there a way to tell gradle to try and compile this library in compatability mode with Java 7?有没有办法告诉 gradle 尝试在与 Java 7 兼容的模式下编译这个库?
Right now, to get it to build, I'm using this line in my build.gradle:现在,为了构建它,我在 build.gradle 中使用了这一行:
sourceCompatibility = 1.7
targetCompatibility = 1.7
compile(group: 'com.blah.example', name: 'some-java-8-library', version: '1.0') {
transitive = false
}
The problem you'll run into is less of a Gradle problem, and more of a Java problem.您将遇到的问题不是 Gradle 问题,而是 Java 问题。 If your original library jar was build with targetCompatibility = 1.8
, you will not be able to use it on your Java7 JVM.如果您的原始库 jar 是使用targetCompatibility = 1.8
构建的,您将无法在 Java7 JVM 上使用它。 You will see the version conflict error message.您将看到版本冲突错误消息。
If you however have access to the library source, you can try building from source with targetCompatibility = 1.7
and then use the resulting jar with Java7 JVM.但是,如果您可以访问库源代码,则可以尝试从targetCompatibility = 1.7
源代码构建,然后将生成的 jar 与 Java7 JVM 一起使用。
The complication here, unlike earlier versions of java is that the java compiler will force you to use targetCompatibility=1.8
if you set sourceCompatibility to 1.8 - which means, if your library source uses any 1.8 features like Lambdas, you will not be able to compile to targetCompatibility = 1.7
这里的复杂之处在于,与早期版本的 java 不同,如果您将 sourceCompatibility 设置为 1.8,java 编译器将强制您使用targetCompatibility=1.8
1.8 - 这意味着,如果您的库源使用任何 1.8 功能,如 Lambdas,您将无法编译目标targetCompatibility = 1.7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.