mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-28 09:28:55 -04:00
Validate mrjar plugin versions (#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.
This commit is contained in:
parent
4783d1f991
commit
095621f801
4 changed files with 61 additions and 125 deletions
|
@ -72,18 +72,19 @@ public class MrjarPlugin implements Plugin<Project> {
|
||||||
var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class);
|
var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class);
|
||||||
var isIdeaSync = System.getProperty("idea.sync.active", "false").equals("true");
|
var isIdeaSync = System.getProperty("idea.sync.active", "false").equals("true");
|
||||||
var ideaSourceSetsEnabled = project.hasProperty(MRJAR_IDEA_ENABLED) && project.property(MRJAR_IDEA_ENABLED).equals("true");
|
var ideaSourceSetsEnabled = project.hasProperty(MRJAR_IDEA_ENABLED) && project.property(MRJAR_IDEA_ENABLED).equals("true");
|
||||||
|
int minJavaVersion = Integer.parseInt(buildParams.getMinimumCompilerVersion().getMajorVersion());
|
||||||
|
|
||||||
// Ignore version-specific source sets if we are importing into IntelliJ and have not explicitly enabled this.
|
// Ignore version-specific source sets if we are importing into IntelliJ and have not explicitly enabled this.
|
||||||
// Avoids an IntelliJ bug:
|
// Avoids an IntelliJ bug:
|
||||||
// https://youtrack.jetbrains.com/issue/IDEA-285640/Compiler-Options-Settings-language-level-is-set-incorrectly-with-JDK-19ea
|
// https://youtrack.jetbrains.com/issue/IDEA-285640/Compiler-Options-Settings-language-level-is-set-incorrectly-with-JDK-19ea
|
||||||
if (isIdeaSync == false || ideaSourceSetsEnabled) {
|
if (isIdeaSync == false || ideaSourceSetsEnabled) {
|
||||||
List<Integer> mainVersions = findSourceVersions(project);
|
List<Integer> mainVersions = findSourceVersions(project, minJavaVersion);
|
||||||
List<String> mainSourceSets = new ArrayList<>();
|
List<String> mainSourceSets = new ArrayList<>();
|
||||||
mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME);
|
mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME);
|
||||||
configurePreviewFeatures(project, javaExtension.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME), 21);
|
configurePreviewFeatures(project, javaExtension.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME), minJavaVersion);
|
||||||
List<String> testSourceSets = new ArrayList<>(mainSourceSets);
|
List<String> testSourceSets = new ArrayList<>(mainSourceSets);
|
||||||
testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME);
|
testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME);
|
||||||
configurePreviewFeatures(project, javaExtension.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME), 21);
|
configurePreviewFeatures(project, javaExtension.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME), minJavaVersion);
|
||||||
for (int javaVersion : mainVersions) {
|
for (int javaVersion : mainVersions) {
|
||||||
String mainSourceSetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion;
|
String mainSourceSetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion;
|
||||||
SourceSet mainSourceSet = addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion, true);
|
SourceSet mainSourceSet = addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion, true);
|
||||||
|
@ -103,6 +104,7 @@ public class MrjarPlugin implements Plugin<Project> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void configureMrjar(Project project) {
|
private void configureMrjar(Project project) {
|
||||||
|
|
||||||
var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME);
|
var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME);
|
||||||
jarTask.configure(task -> { task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); });
|
jarTask.configure(task -> { task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); });
|
||||||
|
|
||||||
|
@ -222,7 +224,7 @@ public class MrjarPlugin implements Plugin<Project> {
|
||||||
project.getTasks().named("check").configure(checkTask -> checkTask.dependsOn(testTaskProvider));
|
project.getTasks().named("check").configure(checkTask -> checkTask.dependsOn(testTaskProvider));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Integer> findSourceVersions(Project project) {
|
private static List<Integer> findSourceVersions(Project project, int minJavaVersion) {
|
||||||
var srcDir = project.getProjectDir().toPath().resolve("src");
|
var srcDir = project.getProjectDir().toPath().resolve("src");
|
||||||
List<Integer> versions = new ArrayList<>();
|
List<Integer> versions = new ArrayList<>();
|
||||||
try (var subdirStream = Files.list(srcDir)) {
|
try (var subdirStream = Files.list(srcDir)) {
|
||||||
|
@ -231,7 +233,23 @@ public class MrjarPlugin implements Plugin<Project> {
|
||||||
String sourcesetName = sourceSetPath.getFileName().toString();
|
String sourcesetName = sourceSetPath.getFileName().toString();
|
||||||
Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN.matcher(sourcesetName);
|
Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN.matcher(sourcesetName);
|
||||||
if (sourcesetMatcher.matches()) {
|
if (sourcesetMatcher.matches()) {
|
||||||
versions.add(Integer.parseInt(sourcesetMatcher.group(1)));
|
int version = Integer.parseInt(sourcesetMatcher.group(1));
|
||||||
|
if (version < minJavaVersion) {
|
||||||
|
// NOTE: We allow mainNN for the min java version so that incubating modules can be used without warnings.
|
||||||
|
// It is a workaround for https://bugs.openjdk.org/browse/JDK-8187591. Once min java is 22, we
|
||||||
|
// can use the SuppressWarnings("preview") in the code using incubating modules and this check
|
||||||
|
// can change to <=
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Found src dir '"
|
||||||
|
+ sourcesetName
|
||||||
|
+ "' for Java "
|
||||||
|
+ version
|
||||||
|
+ " but multi-release jar sourceset should have version "
|
||||||
|
+ minJavaVersion
|
||||||
|
+ " or greater"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
versions.add(version);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -14,21 +14,51 @@ import java.net.URI;
|
||||||
import java.net.http.HttpClient;
|
import java.net.http.HttpClient;
|
||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
|
import java.net.spi.InetAddressResolver;
|
||||||
|
import java.net.spi.InetAddressResolverProvider;
|
||||||
|
|
||||||
class VersionSpecificNetworkChecks {
|
class VersionSpecificNetworkChecks {
|
||||||
static void createInetAddressResolverProvider() {}
|
static void createInetAddressResolverProvider() {
|
||||||
|
var x = new InetAddressResolverProvider() {
|
||||||
|
@Override
|
||||||
|
public InetAddressResolver get(Configuration configuration) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String name() {
|
||||||
|
return "TEST";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
static void httpClientSend() throws InterruptedException {
|
static void httpClientSend() throws InterruptedException {
|
||||||
HttpClient httpClient = HttpClient.newBuilder().build();
|
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
|
||||||
try {
|
// Shutdown the client, so the send action will shortcut before actually executing any network operation
|
||||||
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
|
// (but after it run our check in the prologue)
|
||||||
} catch (IOException e) {
|
httpClient.shutdown();
|
||||||
// Expected, the send action may fail with these parameters (but after it run the entitlement check in the prologue)
|
try {
|
||||||
|
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Expected, since we shut down the client
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void httpClientSendAsync() {
|
static void httpClientSendAsync() {
|
||||||
HttpClient httpClient = HttpClient.newBuilder().build();
|
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
|
||||||
httpClient.sendAsync(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
|
// Shutdown the client, so the send action will return before actually executing any network operation
|
||||||
|
// (but after it run our check in the prologue)
|
||||||
|
httpClient.shutdown();
|
||||||
|
var future = httpClient.sendAsync(
|
||||||
|
HttpRequest.newBuilder(URI.create("http://localhost")).build(),
|
||||||
|
HttpResponse.BodyHandlers.discarding()
|
||||||
|
);
|
||||||
|
assert future.isCompletedExceptionally();
|
||||||
|
future.exceptionally(ex -> {
|
||||||
|
assert ex instanceof IOException;
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,48 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
||||||
* or more contributor license agreements. Licensed under the "Elastic License
|
|
||||||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
|
||||||
* Public License v 1"; you may not use this file except in compliance with, at
|
|
||||||
* your election, the "Elastic License 2.0", the "GNU Affero General Public
|
|
||||||
* License v3.0 only", or the "Server Side Public License, v 1".
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.elasticsearch.entitlement.qa.test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.net.http.HttpResponse;
|
|
||||||
import java.net.spi.InetAddressResolver;
|
|
||||||
import java.net.spi.InetAddressResolverProvider;
|
|
||||||
|
|
||||||
class VersionSpecificNetworkChecks {
|
|
||||||
static void createInetAddressResolverProvider() {
|
|
||||||
var x = new InetAddressResolverProvider() {
|
|
||||||
@Override
|
|
||||||
public InetAddressResolver get(Configuration configuration) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "TEST";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static void httpClientSend() throws InterruptedException {
|
|
||||||
HttpClient httpClient = HttpClient.newBuilder().build();
|
|
||||||
try {
|
|
||||||
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Expected, the send action may fail with these parameters (but after it run the entitlement check in the prologue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void httpClientSendAsync() {
|
|
||||||
HttpClient httpClient = HttpClient.newBuilder().build();
|
|
||||||
httpClient.sendAsync(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,64 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
||||||
* or more contributor license agreements. Licensed under the "Elastic License
|
|
||||||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
|
|
||||||
* Public License v 1"; you may not use this file except in compliance with, at
|
|
||||||
* your election, the "Elastic License 2.0", the "GNU Affero General Public
|
|
||||||
* License v3.0 only", or the "Server Side Public License, v 1".
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.elasticsearch.entitlement.qa.test;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.net.http.HttpResponse;
|
|
||||||
import java.net.spi.InetAddressResolver;
|
|
||||||
import java.net.spi.InetAddressResolverProvider;
|
|
||||||
|
|
||||||
class VersionSpecificNetworkChecks {
|
|
||||||
static void createInetAddressResolverProvider() {
|
|
||||||
var x = new InetAddressResolverProvider() {
|
|
||||||
@Override
|
|
||||||
public InetAddressResolver get(Configuration configuration) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String name() {
|
|
||||||
return "TEST";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static void httpClientSend() throws InterruptedException {
|
|
||||||
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
|
|
||||||
// Shutdown the client, so the send action will shortcut before actually executing any network operation
|
|
||||||
// (but after it run our check in the prologue)
|
|
||||||
httpClient.shutdown();
|
|
||||||
try {
|
|
||||||
httpClient.send(HttpRequest.newBuilder(URI.create("http://localhost")).build(), HttpResponse.BodyHandlers.discarding());
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Expected, since we shut down the client
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void httpClientSendAsync() {
|
|
||||||
try (HttpClient httpClient = HttpClient.newBuilder().build()) {
|
|
||||||
// Shutdown the client, so the send action will return before actually executing any network operation
|
|
||||||
// (but after it run our check in the prologue)
|
|
||||||
httpClient.shutdown();
|
|
||||||
var future = httpClient.sendAsync(
|
|
||||||
HttpRequest.newBuilder(URI.create("http://localhost")).build(),
|
|
||||||
HttpResponse.BodyHandlers.discarding()
|
|
||||||
);
|
|
||||||
assert future.isCompletedExceptionally();
|
|
||||||
future.exceptionally(ex -> {
|
|
||||||
assert ex instanceof IOException;
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue