Skip to content

Commit e15b32a

Browse files
Validate mrjar plugin versions (elastic#120823) (elastic#120942) (elastic#122608)
* Validate mrjar plugin versions (elastic#120823) The mrjar plugin adds support for sourcesets named in the form mainNN, which adds the appropriate compiler and other settings for that version of Java, and produces a multi-release jar. Having multi-release jars only makes sense for versions of java newer than the minimum compile version. This commit adds validation that the version is not too old. Note that the check is slightly relaxed; it allows mainNN where NN is equal to the min java version. This is due to the desire to keep code using incubating modules separate because warnings must be disabled. * Restore deleted files * restore classifier fix --------- Co-authored-by: Elastic Machine <[email protected]>
1 parent 989e24c commit e15b32a

File tree

1 file changed

+21
-3
lines changed
  • build-tools-internal/src/main/java/org/elasticsearch/gradle/internal

1 file changed

+21
-3
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ public void apply(Project project) {
7272
var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class);
7373
var isIdeaSync = System.getProperty("idea.sync.active", "false").equals("true");
7474
var ideaSourceSetsEnabled = project.hasProperty(MRJAR_IDEA_ENABLED) && project.property(MRJAR_IDEA_ENABLED).equals("true");
75+
int minJavaVersion = Integer.parseInt(buildParams.getMinimumCompilerVersion().getMajorVersion());
7576

7677
// Ignore version-specific source sets if we are importing into IntelliJ and have not explicitly enabled this.
7778
// Avoids an IntelliJ bug:
7879
// https://youtrack.jetbrains.com/issue/IDEA-285640/Compiler-Options-Settings-language-level-is-set-incorrectly-with-JDK-19ea
7980
if (isIdeaSync == false || ideaSourceSetsEnabled) {
80-
List<Integer> mainVersions = findSourceVersions(project);
81+
List<Integer> mainVersions = findSourceVersions(project, minJavaVersion);
8182
List<String> mainSourceSets = new ArrayList<>();
8283
mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME);
8384
List<String> testSourceSets = new ArrayList<>(mainSourceSets);
@@ -101,6 +102,7 @@ public void apply(Project project) {
101102
}
102103

103104
private void configureMrjar(Project project) {
105+
104106
var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME);
105107
jarTask.configure(task -> { task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); });
106108

@@ -216,7 +218,7 @@ private void createTestTask(
216218
project.getTasks().named("check").configure(checkTask -> checkTask.dependsOn(testTaskProvider));
217219
}
218220

219-
private static List<Integer> findSourceVersions(Project project) {
221+
private static List<Integer> findSourceVersions(Project project, int minJavaVersion) {
220222
var srcDir = project.getProjectDir().toPath().resolve("src");
221223
List<Integer> versions = new ArrayList<>();
222224
try (var subdirStream = Files.list(srcDir)) {
@@ -225,7 +227,23 @@ private static List<Integer> findSourceVersions(Project project) {
225227
String sourcesetName = sourceSetPath.getFileName().toString();
226228
Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN.matcher(sourcesetName);
227229
if (sourcesetMatcher.matches()) {
228-
versions.add(Integer.parseInt(sourcesetMatcher.group(1)));
230+
int version = Integer.parseInt(sourcesetMatcher.group(1));
231+
if (version < minJavaVersion) {
232+
// NOTE: We allow mainNN for the min java version so that incubating modules can be used without warnings.
233+
// It is a workaround for https://bugs.openjdk.org/browse/JDK-8187591. Once min java is 22, we
234+
// can use the SuppressWarnings("preview") in the code using incubating modules and this check
235+
// can change to <=
236+
throw new IllegalArgumentException(
237+
"Found src dir '"
238+
+ sourcesetName
239+
+ "' for Java "
240+
+ version
241+
+ " but multi-release jar sourceset should have version "
242+
+ minJavaVersion
243+
+ " or greater"
244+
);
245+
}
246+
versions.add(version);
229247
}
230248
}
231249
} catch (IOException e) {

0 commit comments

Comments
 (0)