|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.gradle.internal.conventions; |
| 10 | + |
| 11 | +import org.apache.tools.ant.taskdefs.condition.Os; |
| 12 | +import org.gradle.api.Action; |
| 13 | +import org.gradle.api.GradleException; |
| 14 | +import org.gradle.api.Plugin; |
| 15 | +import org.gradle.api.Project; |
| 16 | +import org.gradle.api.Transformer; |
| 17 | +import org.gradle.api.plugins.JavaBasePlugin; |
| 18 | +import org.gradle.api.plugins.JavaPluginExtension; |
| 19 | +import org.gradle.api.tasks.Copy; |
| 20 | +import org.gradle.api.tasks.Delete; |
| 21 | +import org.gradle.plugins.ide.eclipse.EclipsePlugin; |
| 22 | +import org.gradle.plugins.ide.eclipse.model.Classpath; |
| 23 | +import org.gradle.plugins.ide.eclipse.model.EclipseModel; |
| 24 | +import org.gradle.plugins.ide.eclipse.model.EclipseProject; |
| 25 | +import org.gradle.plugins.ide.eclipse.model.ProjectDependency; |
| 26 | +import org.gradle.plugins.ide.eclipse.model.SourceFolder; |
| 27 | +import org.gradle.plugins.ide.eclipse.model.ClasspathEntry; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.util.List; |
| 31 | +import java.io.IOException; |
| 32 | +import static java.util.stream.Collectors.toList; |
| 33 | + |
| 34 | +import static org.apache.commons.io.FileUtils.readFileToString; |
| 35 | + |
| 36 | +public class EclipseConventionPlugin implements Plugin<Project> { |
| 37 | + @Override |
| 38 | + public void apply(Project project) { |
| 39 | + project.getPlugins().apply(EclipsePlugin.class); |
| 40 | + EclipseModel eclipseModel = project.getExtensions().getByType(EclipseModel.class); |
| 41 | + EclipseProject eclipseProject = eclipseModel.getProject(); |
| 42 | + |
| 43 | + // Name all the non-root projects after their path so that paths get grouped together when imported into eclipse. |
| 44 | + if (project.getPath().equals(":") == false) { |
| 45 | + eclipseProject.setName(project.getPath()); |
| 46 | + if (Os.isFamily(Os.FAMILY_WINDOWS)) { |
| 47 | + eclipseProject.setName(eclipseProject.getName().replace(':', '_')); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + File licenseHeaderFile; |
| 53 | + String prefix = ":x-pack"; |
| 54 | + if (Os.isFamily(Os.FAMILY_WINDOWS)) { |
| 55 | + prefix = prefix.replace(':', '_'); |
| 56 | + } |
| 57 | + File root = root(project); |
| 58 | + if (eclipseProject.getName().startsWith(prefix)) { |
| 59 | + licenseHeaderFile = new File(root, "build-tools-internal/src/main/resources/license-headers/elastic-license-2.0-header.txt"); |
| 60 | + } else { |
| 61 | + licenseHeaderFile = new File(root, "build-tools-internal/src/main/resources/license-headers/sspl+elastic-license-header.txt"); |
| 62 | + } |
| 63 | + |
| 64 | + String lineSeparator = Os.isFamily(Os.FAMILY_WINDOWS) ? "\\\\r\\\\n" : "\\\\n"; |
| 65 | + String licenseHeader = null; |
| 66 | + try { |
| 67 | + licenseHeader = readFileToString(licenseHeaderFile, "UTF-8").replace(System.lineSeparator(), lineSeparator); |
| 68 | + } catch (IOException e) { |
| 69 | + throw new GradleException("Cannot configure eclipse", e); |
| 70 | + } |
| 71 | + |
| 72 | + String finalLicenseHeader = licenseHeader; |
| 73 | + project.getTasks().register("copyEclipseSettings", Copy.class, copy -> { |
| 74 | + copy.mustRunAfter("wipeEclipseSettings"); |
| 75 | + // TODO: "package this up" for external builds |
| 76 | + copy.from(new File(root, "build-tools-internal/src/main/resources/eclipse.settings")); |
| 77 | + copy.into(".settings"); |
| 78 | + copy.filter(new Transformer<String, String>() { |
| 79 | + @Override |
| 80 | + public String transform(String s) { |
| 81 | + return s.replaceAll("@@LICENSE_HEADER_TEXT@@", finalLicenseHeader); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + // otherwise .settings is not nuked entirely |
| 86 | + project.getTasks().register("wipeEclipseSettings", Delete.class, new Action<Delete>() { |
| 87 | + @Override |
| 88 | + public void execute(Delete delete) { |
| 89 | + delete.delete(".settings"); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + project.getTasks().named("cleanEclipse").configure(t -> t.dependsOn("wipeEclipseSettings")); |
| 94 | + |
| 95 | + // otherwise the eclipse merging is *super confusing* |
| 96 | + project.getTasks().named("eclipse").configure(t -> t.dependsOn("cleanEclipse", "copyEclipseSettings")); |
| 97 | + |
| 98 | + project.getPlugins().withType(JavaBasePlugin.class, javaBasePlugin -> { |
| 99 | + JavaPluginExtension java = project.getExtensions().getByType(JavaPluginExtension.class); |
| 100 | + java.getModularity().getInferModulePath().set(false); |
| 101 | + |
| 102 | + eclipseModel.getClasspath().getFile().whenMerged(c -> { |
| 103 | + Classpath classpath = (Classpath) c; |
| 104 | + |
| 105 | + /* |
| 106 | + * give each source folder a unique corresponding output folder |
| 107 | + * outside of the usual `build` folder. We can't put the build |
| 108 | + * in the usual build folder because eclipse becomes *very* sad |
| 109 | + * if we delete it. Which `gradlew clean` does all the time. |
| 110 | + */ |
| 111 | + int i = 0; |
| 112 | + List<ClasspathEntry> sourceFolderList = classpath.getEntries().stream().filter(e -> e instanceof SourceFolder).collect(toList()); |
| 113 | + for (ClasspathEntry sourceFolder : sourceFolderList) { |
| 114 | + ((SourceFolder)sourceFolder).setOutput("out/eclipse/" + i++); |
| 115 | + } |
| 116 | + |
| 117 | + // Starting with Gradle 6.7 test dependencies are not exposed by eclipse |
| 118 | + // projects by default. This breaks project dependencies using the `java-test-fixtures` plugin |
| 119 | + // or dependencies on other projects manually declared testArtifacts configurations |
| 120 | + // This issue is tracked in gradle issue tracker. |
| 121 | + // See https://github.com/gradle/gradle/issues/14932 for further details |
| 122 | + |
| 123 | + classpath.getEntries().stream().filter(e -> e instanceof ProjectDependency).forEach(it -> |
| 124 | + ((ProjectDependency) it).getEntryAttributes().remove("without_test_code") |
| 125 | + ); |
| 126 | + |
| 127 | + }); |
| 128 | + |
| 129 | + project.getTasks().named("eclipseJdt").configure(t -> t.dependsOn("copyEclipseSettings")); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + private File root(Project project) { |
| 134 | + return project.getGradle().getParent() == null ? |
| 135 | + project.getRootDir() : |
| 136 | + root(project.getGradle().getParent().getRootProject()); |
| 137 | + } |
| 138 | +} |
0 commit comments