Search in sources :

Example 6 with BuildConfiguration

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(java.nio.file.Path) BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) GradleBuild(org.eclipse.buildship.core.GradleBuild)

Example 7 with BuildConfiguration

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(java.nio.file.Path) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) GradleEnvironment(org.gradle.tooling.model.build.GradleEnvironment) BuildEnvironment(org.gradle.tooling.model.build.BuildEnvironment) SynchronizationResult(org.eclipse.buildship.core.SynchronizationResult) StandardVMType(org.eclipse.jdt.internal.launching.StandardVMType) File(java.io.File) GradleBuild(org.eclipse.buildship.core.GradleBuild)

Example 8 with BuildConfiguration

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());
        }
    }
}
Also used : BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IVMInstall(org.eclipse.jdt.launching.IVMInstall) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IPath(org.eclipse.core.runtime.IPath) IVMInstallType(org.eclipse.jdt.launching.IVMInstallType) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Test(org.junit.Test)

Example 9 with BuildConfiguration

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());
        }
    }
}
Also used : BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IVMInstall(org.eclipse.jdt.launching.IVMInstall) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IPath(org.eclipse.core.runtime.IPath) IVMInstallType(org.eclipse.jdt.launching.IVMInstallType) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Test(org.junit.Test)

Example 10 with BuildConfiguration

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();
        }
    }
}
Also used : BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) HashMap(java.util.HashMap) Properties(java.util.Properties) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Test(org.junit.Test)

Aggregations

BuildConfiguration (org.eclipse.buildship.core.BuildConfiguration)10 File (java.io.File)7 IPath (org.eclipse.core.runtime.IPath)7 Test (org.junit.Test)6 Path (java.nio.file.Path)5 GradleBuild (org.eclipse.buildship.core.GradleBuild)3 IFile (org.eclipse.core.resources.IFile)3 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)3 Preferences (org.eclipse.jdt.ls.core.internal.preferences.Preferences)3 IOException (java.io.IOException)2 Properties (java.util.Properties)2 CoreException (org.eclipse.core.runtime.CoreException)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IVMInstall (org.eclipse.jdt.launching.IVMInstall)2 IVMInstallType (org.eclipse.jdt.launching.IVMInstallType)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 GradleDistribution (org.eclipse.buildship.core.GradleDistribution)1 SynchronizationResult (org.eclipse.buildship.core.SynchronizationResult)1