Skip to content

Commit b3b4214

Browse files
authored
Skip multi-release source sets in idea project import by default (elastic#107123)
There is an existing IntelliJ bug that prevents doing a full project build when source sets for multi-release jars are present. This changes the project import behavior so that these source sets are ignored by default and can be explicitly enabled by adding `org.gradle.mrjar.idea.enabled=true` to your `~/.gradle/gradle.properties` file should you need to actively work on that code.
1 parent 3486a08 commit b3b4214

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

build-tools-internal/src/main/groovy/elasticsearch.ide.gradle

+15-12
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
130130
':server:generateModulesList',
131131
':server:generatePluginsList',
132132
':generateProviderImpls',
133-
':libs:elasticsearch-native:elasticsearch-native-libraries:extractLibs'].collect { elasticsearchProject.right()?.task(it) ?: it })
133+
':libs:elasticsearch-native:elasticsearch-native-libraries:extractLibs',
134+
':x-pack:libs:es-opensaml-security-api:shadowJar'].collect { elasticsearchProject.right()?.task(it) ?: it })
134135
}
135136

136137
// this path is produced by the extractLibs task above
@@ -239,20 +240,22 @@ if (providers.systemProperty('idea.active').getOrNull() == 'true') {
239240
* but before the XML document, e.g. a doctype or comment
240241
*/
241242
void modifyXml(Object path, Action<? super Node> action, String preface = null) {
242-
Node xml = parseXml(path)
243-
action.execute(xml)
243+
if (project.file(path).exists()) {
244+
Node xml = parseXml(path)
245+
action.execute(xml)
244246

245-
File xmlFile = project.file(path)
246-
xmlFile.withPrintWriter { writer ->
247-
def printer = new XmlNodePrinter(writer)
248-
printer.namespaceAware = true
249-
printer.preserveWhitespace = true
250-
writer.write("<?xml version=\"1.0\"?>\n")
247+
File xmlFile = project.file(path)
248+
xmlFile.withPrintWriter { writer ->
249+
def printer = new XmlNodePrinter(writer)
250+
printer.namespaceAware = true
251+
printer.preserveWhitespace = true
252+
writer.write("<?xml version=\"1.0\"?>\n")
251253

252-
if (preface != null) {
253-
writer.write(preface)
254+
if (preface != null) {
255+
writer.write(preface)
256+
}
257+
printer.print(xml)
254258
}
255-
printer.print(xml)
256259
}
257260
}
258261

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java

+25-17
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
public class MrjarPlugin implements Plugin<Project> {
5050

5151
private static final Pattern MRJAR_SOURCESET_PATTERN = Pattern.compile("main(\\d{2})");
52+
private static final String MRJAR_IDEA_ENABLED = "org.gradle.mrjar.idea.enabled";
5253

5354
private final JavaToolchainService javaToolchains;
5455

@@ -61,23 +62,30 @@ public class MrjarPlugin implements Plugin<Project> {
6162
public void apply(Project project) {
6263
project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class);
6364
var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class);
64-
65-
List<Integer> mainVersions = findSourceVersions(project);
66-
List<String> mainSourceSets = new ArrayList<>();
67-
mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME);
68-
List<String> testSourceSets = new ArrayList<>(mainSourceSets);
69-
testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME);
70-
for (int javaVersion : mainVersions) {
71-
String mainSourceSetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion;
72-
SourceSet mainSourceSet = addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion);
73-
configureSourceSetInJar(project, mainSourceSet, javaVersion);
74-
mainSourceSets.add(mainSourceSetName);
75-
testSourceSets.add(mainSourceSetName);
76-
77-
String testSourceSetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion;
78-
SourceSet testSourceSet = addSourceSet(project, javaExtension, testSourceSetName, testSourceSets, javaVersion);
79-
testSourceSets.add(testSourceSetName);
80-
createTestTask(project, testSourceSet, javaVersion, mainSourceSets);
65+
var isIdea = System.getProperty("idea.active", "false").equals("true");
66+
var ideaSourceSetsEnabled = project.hasProperty(MRJAR_IDEA_ENABLED) && project.property(MRJAR_IDEA_ENABLED).equals("true");
67+
68+
// Ignore version-specific source sets if we are importing into IntelliJ and have not explicitly enabled this.
69+
// Avoids an IntelliJ bug:
70+
// https://youtrack.jetbrains.com/issue/IDEA-285640/Compiler-Options-Settings-language-level-is-set-incorrectly-with-JDK-19ea
71+
if (isIdea == false || ideaSourceSetsEnabled) {
72+
List<Integer> mainVersions = findSourceVersions(project);
73+
List<String> mainSourceSets = new ArrayList<>();
74+
mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME);
75+
List<String> testSourceSets = new ArrayList<>(mainSourceSets);
76+
testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME);
77+
for (int javaVersion : mainVersions) {
78+
String mainSourceSetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion;
79+
SourceSet mainSourceSet = addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion);
80+
configureSourceSetInJar(project, mainSourceSet, javaVersion);
81+
mainSourceSets.add(mainSourceSetName);
82+
testSourceSets.add(mainSourceSetName);
83+
84+
String testSourceSetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion;
85+
SourceSet testSourceSet = addSourceSet(project, javaExtension, testSourceSetName, testSourceSets, javaVersion);
86+
testSourceSets.add(testSourceSetName);
87+
createTestTask(project, testSourceSet, javaVersion, mainSourceSets);
88+
}
8189
}
8290

8391
configureMrjar(project);

0 commit comments

Comments
 (0)