use of org.eclipse.buildship.core.BuildConfiguration in project eclipse.jdt.ls by eclipse.
the class GradleProjectImporter method upgradeGradleVersion.
public static boolean upgradeGradleVersion(String projectUri, IProgressMonitor monitor) {
String newDistributionUrl = String.format("https://services.gradle.org/distributions/gradle-%s-bin.zip", GradleCompatibilityChecker.CURRENT_GRADLE);
Path projectFolder = Paths.get(URI.create(projectUri));
File propertiesFile = projectFolder.resolve("gradle").resolve("wrapper").resolve("gradle-wrapper.properties").toFile();
Properties properties = new Properties();
if (propertiesFile.exists()) {
try (FileInputStream stream = new FileInputStream(propertiesFile)) {
properties.load(stream);
properties.setProperty("distributionUrl", newDistributionUrl);
} catch (IOException e) {
return false;
}
} else {
properties.setProperty("distributionBase", "GRADLE_USER_HOME");
properties.setProperty("distributionPath", "wrapper/dists");
properties.setProperty("distributionUrl", newDistributionUrl);
properties.setProperty("zipStoreBase", "GRADLE_USER_HOME");
properties.setProperty("zipStorePath", "wrapper/dists");
}
try {
properties.store(new FileOutputStream(propertiesFile), null);
} catch (Exception e) {
return false;
}
BuildConfiguration build = getBuildConfiguration(projectFolder);
GradleBuild gradleBuild = GradleCore.getWorkspace().createBuild(build);
try {
gradleBuild.withConnection(connection -> {
connection.newBuild().forTasks("wrapper").run();
return null;
}, monitor);
} catch (Exception e) {
return false;
}
return true;
}
use of org.eclipse.buildship.core.BuildConfiguration in project eclipse.jdt.ls by eclipse.
the class GradleProjectImporter method startSynchronization.
protected IStatus startSynchronization(Path projectFolder, IProgressMonitor monitor) {
File location = projectFolder.toFile();
boolean shouldSynchronize = shouldSynchronize(location);
if (shouldSynchronize) {
BuildConfiguration build = getBuildConfiguration(projectFolder);
GradleBuild gradleBuild = GradleCore.getWorkspace().createBuild(build);
SynchronizationResult result = gradleBuild.synchronize(monitor);
IStatus resultStatus = result.getStatus();
if (isFailedStatus(resultStatus)) {
try {
BuildEnvironment environment = gradleBuild.withConnection(connection -> connection.getModel(BuildEnvironment.class), monitor);
GradleEnvironment gradleEnvironment = environment.getGradle();
String gradleVersion = gradleEnvironment.getGradleVersion();
File javaHome = getJavaHome(getPreferences());
String javaVersion;
if (javaHome == null) {
javaVersion = System.getProperty("java.version");
} else {
StandardVMType type = new StandardVMType();
javaVersion = type.readReleaseVersion(javaHome);
}
if (GradleCompatibilityChecker.isIncompatible(GradleVersion.version(gradleVersion), javaVersion)) {
Path projectName = projectFolder.getName(projectFolder.getNameCount() - 1);
String message = String.format("Can't use Java %s and Gradle %s to import Gradle project %s.", javaVersion, gradleVersion, projectName.toString());
String highestJavaVersion = GradleCompatibilityChecker.getHighestSupportedJava(GradleVersion.version(gradleVersion));
return new GradleCompatibilityStatus(resultStatus, message, projectFolder.toUri().toString(), highestJavaVersion);
}
} catch (Exception e) {
// Do nothing
}
}
return resultStatus;
}
return Status.OK_STATUS;
}
use of org.eclipse.buildship.core.BuildConfiguration in project eclipse.jdt.ls by eclipse.
the class GradleProjectImporterTest method testJavaHome.
@Test
public void testJavaHome() throws Exception {
Preferences prefs = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
String javaHomePreference = prefs.getJavaHome();
IVMInstall defaultVM = JavaRuntime.getDefaultVMInstall();
try {
IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
if (installType == null || installType.getVMInstalls().length == 0) {
// https://github.com/eclipse/eclipse.jdt.ls/issues/1646
installType = JavaRuntime.getVMInstallType(JVMConfigurator.MAC_OSX_VM_TYPE);
}
IVMInstall[] vms = installType.getVMInstalls();
IVMInstall vm = vms[0];
JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
String javaHome = new File(TestVMType.getFakeJDKsLocation(), "11").getAbsolutePath();
prefs.setJavaHome(javaHome);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath rootFolder = root.getLocation().append("/projects/gradle/simple-gradle");
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(rootFolder.toFile().toPath());
assertEquals(vm.getInstallLocation().getAbsolutePath(), build.getJavaHome().get().getAbsolutePath());
} finally {
prefs.setJavaHome(javaHomePreference);
if (defaultVM != null) {
JavaRuntime.setDefaultVMInstall(defaultVM, new NullProgressMonitor());
}
}
}
use of org.eclipse.buildship.core.BuildConfiguration in project eclipse.jdt.ls by eclipse.
the class GradleProjectImporterTest method testGradleJavaHome.
@Test
public void testGradleJavaHome() throws Exception {
Preferences prefs = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
String gradleJavaHomePreference = prefs.getGradleJavaHome();
IVMInstall defaultVM = JavaRuntime.getDefaultVMInstall();
try {
IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
if (installType == null || installType.getVMInstalls().length == 0) {
// https://github.com/eclipse/eclipse.jdt.ls/issues/1646
installType = JavaRuntime.getVMInstallType(JVMConfigurator.MAC_OSX_VM_TYPE);
}
IVMInstall[] vms = installType.getVMInstalls();
IVMInstall vm = vms[0];
JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
String javaHome = new File(TestVMType.getFakeJDKsLocation(), "1.8").getAbsolutePath();
prefs.setGradleJavaHome(javaHome);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath rootFolder = root.getLocation().append("/projects/gradle/simple-gradle");
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(rootFolder.toFile().toPath());
assertEquals(javaHome, build.getJavaHome().get().getAbsolutePath());
} finally {
prefs.setJavaHome(gradleJavaHomePreference);
if (defaultVM != null) {
JavaRuntime.setDefaultVMInstall(defaultVM, new NullProgressMonitor());
}
}
}
use of org.eclipse.buildship.core.BuildConfiguration in project eclipse.jdt.ls by eclipse.
the class GradleProjectImporterTest method testGradleHome.
@Test
public void testGradleHome() {
Map<String, String> env = new HashMap<>();
Properties sysprops = new Properties();
File file = null;
File projectFile = null;
try {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
File rootFile = root.getLocation().toFile();
file = new File(rootFile, "fakeGradleHome");
sysprops.setProperty(GradleProjectImporter.GRADLE_HOME, file.getAbsolutePath());
boolean overrideWorkspaceSettings = GradleProjectImporter.getGradleHomeFile(env, sysprops) != null;
assertFalse(overrideWorkspaceSettings);
file.mkdir();
file.deleteOnExit();
overrideWorkspaceSettings = GradleProjectImporter.getGradleHomeFile(env, sysprops) != null;
assertTrue(overrideWorkspaceSettings);
projectFile = new File(rootFile, "fakeProject");
projectFile.mkdir();
projectFile.deleteOnExit();
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(file.toPath());
assertFalse(build.getGradleUserHome().isPresent());
} finally {
if (file != null) {
file.delete();
}
if (projectFile != null) {
projectFile.delete();
}
}
}
Aggregations