Search in sources :

Example 1 with GradleBuild

use of org.eclipse.buildship.core.GradleBuild in project eclipse.jdt.ls by eclipse.

the class GradleBuildSupport method update.

@Override
public void update(IProject project, boolean force, IProgressMonitor monitor) throws CoreException {
    if (!applies(project)) {
        return;
    }
    JavaLanguageServerPlugin.logInfo("Starting Gradle update for " + project.getName());
    Optional<GradleBuild> build = GradleCore.getWorkspace().getBuild(project);
    if (build.isPresent()) {
        GradleBuild gradleBuild = build.get();
        boolean isRoot = isRoot(project, gradleBuild, monitor);
        if (force && isRoot) {
            String projectPath = project.getLocation().toFile().getAbsolutePath();
            BuildConfiguration buildConfiguration = GradleProjectImporter.getBuildConfiguration(Paths.get(projectPath));
            gradleBuild = GradleCore.getWorkspace().createBuild(buildConfiguration);
        }
        File buildFile = project.getFile(GradleProjectImporter.BUILD_GRADLE_DESCRIPTOR).getLocation().toFile();
        File settingsFile = project.getFile(GradleProjectImporter.SETTINGS_GRADLE_DESCRIPTOR).getLocation().toFile();
        File buildKtsFile = project.getFile(GradleProjectImporter.BUILD_GRADLE_KTS_DESCRIPTOR).getLocation().toFile();
        File settingsKtsFile = project.getFile(GradleProjectImporter.SETTINGS_GRADLE_KTS_DESCRIPTOR).getLocation().toFile();
        boolean shouldUpdate = (buildFile.exists() && JavaLanguageServerPlugin.getDigestStore().updateDigest(buildFile.toPath())) || (settingsFile.exists() && JavaLanguageServerPlugin.getDigestStore().updateDigest(settingsFile.toPath())) || (buildKtsFile.exists() && JavaLanguageServerPlugin.getDigestStore().updateDigest(buildKtsFile.toPath())) || (settingsKtsFile.exists() && JavaLanguageServerPlugin.getDigestStore().updateDigest(settingsKtsFile.toPath()));
        if (isRoot || shouldUpdate) {
            gradleBuild.synchronize(monitor);
        }
    }
}
Also used : BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) File(java.io.File) InternalGradleBuild(org.eclipse.buildship.core.internal.workspace.InternalGradleBuild) GradleBuild(org.eclipse.buildship.core.GradleBuild)

Example 2 with GradleBuild

use of org.eclipse.buildship.core.GradleBuild in project xtext-eclipse by eclipse.

the class IgnoreBuildDirContribution method configure.

/**
 * Store the build dir for the given project.
 */
@Override
public void configure(ProjectContext context, IProgressMonitor monitor) {
    /*
		 * Implementation note: This is called for each project in the workspace that
		 * participates in a Gradle build. We keep track of the gradle build dir and the
		 * Eclipse project to allow efficient rejection of the build dir when the Xtext
		 * builder runs.
		 */
    IProject project = context.getProject();
    if (project.isAccessible()) {
        Optional<GradleBuild> gradleBuild = GradleCore.getWorkspace().getBuild(project);
        if (gradleBuild.isPresent()) {
            try {
                GradleProject gradleProject = gradleBuild.get().withConnection(connection -> connection.getModel(GradleProject.class), new NullProgressMonitor());
                File buildDirectory = gradleProject.getBuildDirectory();
                File projectDirectory = gradleProject.getProjectDirectory();
                IPath relativePath = Path.fromOSString(projectDirectory.toPath().relativize(buildDirectory.toPath()).toString());
                project.setPersistentProperty(BUILDSHIP_BUILD_DIR_KEY, relativePath.toString());
            } catch (Exception e) {
                LOGGER.error(e);
            }
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) GradleProject(org.gradle.tooling.model.GradleProject) File(java.io.File) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) GradleBuild(org.eclipse.buildship.core.GradleBuild)

Example 3 with GradleBuild

use of org.eclipse.buildship.core.GradleBuild 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 4 with GradleBuild

use of org.eclipse.buildship.core.GradleBuild 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)

Aggregations

File (java.io.File)4 GradleBuild (org.eclipse.buildship.core.GradleBuild)4 BuildConfiguration (org.eclipse.buildship.core.BuildConfiguration)3 CoreException (org.eclipse.core.runtime.CoreException)3 IPath (org.eclipse.core.runtime.IPath)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 Properties (java.util.Properties)1 SynchronizationResult (org.eclipse.buildship.core.SynchronizationResult)1 InternalGradleBuild (org.eclipse.buildship.core.internal.workspace.InternalGradleBuild)1 IProject (org.eclipse.core.resources.IProject)1 IStatus (org.eclipse.core.runtime.IStatus)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 StandardVMType (org.eclipse.jdt.internal.launching.StandardVMType)1 GradleProject (org.gradle.tooling.model.GradleProject)1 BuildEnvironment (org.gradle.tooling.model.build.BuildEnvironment)1 GradleEnvironment (org.gradle.tooling.model.build.GradleEnvironment)1