gpt4 book ai didi

java - 如何在 Gradle 和 Eclipse 之间同步 Java 源和目标?

转载 作者:太空宇宙 更新时间:2023-11-04 09:55:07 25 4
gpt4 key购买 nike

Gradle 具有可以设置的源兼容性和目标兼容性变量。 Eclipse 具有 JDK 合规性、生成的类文件兼容性和源代码兼容性。

有什么方法可以自动设置其中一个吗?理想情况下,Gradle 的内容将从 Eclipse 的内容中设置。

编辑:这些东西似乎存储在:org.eclipse.jdt.core.prefs

edit2:它们看起来像:

D:\ray\dev\conradapps\printg>cat .settings\org.eclipse.jdt.core.prefs eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=11 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.source=1.8

我可以让它工作如下,但这是一个黑客:)

import java.io.IOException;
import java.nio.file.*;
import java.util.*;
plugins {
id 'java-library'
id 'application'
id 'distribution'
}

repositories {
jcenter()
}
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
dependencies {
testImplementation 'junit:junit:4.12'
}
String myMainClass='p.Main'
jar {
manifest {
attributes(
'Main-Class': myMainClass
)
}
}
application {
mainClassName = myMainClass
}
class Hack {
static String[] hack() throws IOException {
System.out.println("Working Directory = "+System.getProperty("user.dir"));
String dir="./.settings";
String name="org.eclipse.jdt.core.prefs";
File file=new File(dir,name);
String[] strings=new String[3];
for(int i=0;i<strings.length;i++)
strings[i]="";
if(file.exists()) System.out.println(file.toString()+" exists.");
else return strings;
List<String> lines=new ArrayList<>();
try {
if(usePath) {
Path path=FileSystems.getDefault().getPath(dir,name);
lines=java.nio.file.Files.readAllLines(path);
} else {
BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
for(String line=bufferedReader.readLine();line!=null;line=bufferedReader.readLine())
lines.add(line);
bufferedReader.close();
}
int index;
for(String line:lines) {
if(line.startsWith("org.eclipse.jdt.core.compiler.compliance")) {
index=line.indexOf("=");
if(index>0) {
System.out.println("compliance: "+line.substring(index+1));
strings[0]=line.substring(index+1);
}
}
if(line.startsWith("org.eclipse.jdt.core.compiler.source=1.8")) {
index=line.indexOf("=");
if(index>0) {
System.out.println("source: "+line.substring(index+1));
strings[1]=line.substring(index+1);
}
}
if(line.startsWith("org.eclipse.jdt.core.compiler.codegen.targetPlatform")) {
index=line.indexOf("=");
if(index>0) {
System.out.println("target: "+line.substring(index+1));
strings[2]=line.substring(index+1);
}
}
}
} catch(Exception e) {
System.out.println("caught: "+e);
}
return strings;
}
public static void main(String[] args) throws IOException {
hack();
}
static boolean usePath;
}
println("java version is: ${JavaVersion.current()}")
String[] strings=Hack.hack();
if(strings[1]!="") {
println 'setting source'
sourceCompatibility = strings[1]
}
if(strings[2]!="") {
println 'setting target'
targetCompatibility = strings[2]
}

最佳答案

是的。如果您希望 Gradle 将您的配置提供给 Eclipse,基本上,从 Gradle 5.1.1 开始,只需添加:

sourceCompatibility = '1.7'
targetCompatibility = '1.8'

到您的 build.gradle 文件。请注意,在 java 10 之前,枚举为 1.8、1.9、1.10,但从 Java 11 及 future 版本开始,枚举为 11、12 等。请检查 Gradle docs 。如果您偶然发现此answer :对我来说,使用 Gradle 5.0,java 版本可以带引号或不带引号(1.8 或“1.8”),并且这是在最新版本的 javadocs 中指定的。当添加到compileJava{}内部和外部时,它也可以工作。我在多项目构建上对此进行了测试。

我不确定 Eclipse 到 Gradle 配置传输的情况。难道不应该反过来吗? Gradle 是中央配置工具,用于配置构建过程以及您正在使用的任何 IDE(您或您的协作者)。即使可能,Gradle 也会操作 .classpath 和其他 Eclipse 文件。因此,可以肯定的是,如果这是一个关键点,我更愿意将配置添加到 Gradle 并让它处理 Eclipse 或任何其他 IDE 的文件。

关于java - 如何在 Gradle 和 Eclipse 之间同步 Java 源和目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54247447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com