Apache Lucene open-source search software
Move StageArtifacts under build-tools/build-infra and clean up a few minor forbiddenApi glitches. #15388
Dawid Weissd2877e9fcc26
diff --git a/dev-tools/scripts/StageArtifacts.java b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/scripts/StageArtifacts.javasimilarity index 63%rename from dev-tools/scripts/StageArtifacts.javarename to build-tools/build-infra/src/main/java/org/apache/lucene/gradle/scripts/StageArtifacts.javaindex be6f2fab536..368e5263436 100644--- a/dev-tools/scripts/StageArtifacts.java+++ b/build-tools/build-infra/src/main/java/org/apache/lucene/gradle/scripts/StageArtifacts.java@@ -14,15 +14,8 @@* See the License for the specific language governing permissions and* limitations under the License.*/+package org.apache.lucene.gradle.scripts;-import org.w3c.dom.Document;-import org.w3c.dom.Node;-import org.w3c.dom.NodeList;-import org.xml.sax.InputSource;-import org.xml.sax.SAXException;--import javax.xml.parsers.DocumentBuilderFactory;-import javax.xml.parsers.ParserConfigurationException;import java.io.Console;import java.io.IOException;import java.io.StringReader;@@ -42,20 +35,28 @@ import java.nio.file.Paths;import java.util.ArrayList;import java.util.Comparator;import java.util.List;+import java.util.Locale;import java.util.Map;import java.util.Objects;import java.util.stream.Collectors;import java.util.stream.Stream;+import javax.xml.parsers.DocumentBuilderFactory;+import javax.xml.parsers.ParserConfigurationException;+import org.w3c.dom.Document;+import org.w3c.dom.Node;+import org.w3c.dom.NodeList;+import org.xml.sax.InputSource;+import org.xml.sax.SAXException;/**- * Sonatype nexus artifact staging/deployment script. This could be made- * nicer, but this keeps it to JDK classes only.+ * Sonatype nexus artifact staging/deployment script.+ *+ * <p>This could be made nicer, but this keeps it to JDK classes only.*- * <p>The implementation is based on the REST API documentation of- * <a href="https://oss.sonatype.org/nexus-staging-plugin/default/docs/index.html">nexus-staging-plugin</a>- * and on anecdotal evidence and reverse-engineered information from around- * the web... Weird that such a crucial piece of infrastructure has such obscure- * documentation.+ * <p>The implementation is based on the REST API documentation of <a+ * href="https://oss.sonatype.org/nexus-staging-plugin/default/docs/index.html">nexus-staging-plugin</a>+ * and on anecdotal evidence and reverse-engineered information from around the web... Weird that+ * such a crucial piece of infrastructure has such obscure documentation.*/public class StageArtifacts {private static final String DEFAULT_NEXUS_URI = "https://repository.apache.org";@@ -74,8 +75,8 @@ public class StageArtifacts {static void requiresArgument(String[] args, int at) {if (at + 1 >= args.length) {- throw new RuntimeException("Option '" + args[at]- + "' requires an argument, pass --help for help.");+ throw new RuntimeException(+ "Option '" + args[at] + "' requires an argument, pass --help for help.");}}@@ -84,29 +85,25 @@ public class StageArtifacts {var params = new Params();for (int i = 0; i < args.length; i++) {switch (args[i]) {- case "-n":- case "--nexus":+ case "-n", "--nexus" -> {requiresArgument(args, i);params.nexusUri = URI.create(args[++i]);- break;- case "-u":- case "--user":+ }+ case "-u", "--user" -> {requiresArgument(args, i);params.userName = args[++i];- break;- case "-p":- case "--password":+ }+ case "-p", "--password" -> {requiresArgument(args, i);params.userPass = args[++i].toCharArray();- break;- case "--description":+ }+ case "--description" -> {requiresArgument(args, i);params.description = args[++i];- break;-- case "-h":- case "--help":- System.out.println("java " + StageArtifacts.class.getName() + " [options] path-to-maven-artifacts");+ }+ case "-h", "--help" -> {+ System.out.println(+ "java " + StageArtifacts.class.getName() + " [options] path-to-maven-artifacts");System.out.println(" -u, --user User name for authentication.");System.out.println(" better: ASF_USERNAME env. var.");System.out.println(" -p, --password Password for authentication.");@@ -118,13 +115,14 @@ public class StageArtifacts {System.out.println("");System.out.println(" Password can be omitted for console prompt-input.");System.exit(0);-- default:+ }+ default -> {if (params.mavenDir != null) {- throw new RuntimeException("Exactly one maven artifact directory should be provided.");+ throw new RuntimeException(+ "Exactly one maven artifact directory should be provided.");}params.mavenDir = Paths.get(args[i]);- break;+ }}}@@ -154,7 +152,7 @@ public class StageArtifacts {throw new RuntimeException("Maven artifact directory is required and must exist.");}return params;- } catch (IndexOutOfBoundsException e) {+ } catch (IndexOutOfBoundsException _) {throw new RuntimeException("Required argument missing (pass --help for help)?");}}@@ -165,27 +163,28 @@ public class StageArtifacts {private final URI nexusUri;public NexusApi(Params params) {- Authenticator authenticator = new Authenticator() {- @Override- protected PasswordAuthentication getPasswordAuthentication() {- return new PasswordAuthentication(params.userName, params.userPass);- }- };+ Authenticator authenticator =+ new Authenticator() {+ @Override+ protected PasswordAuthentication getPasswordAuthentication() {+ return new PasswordAuthentication(params.userName, params.userPass);+ }+ };- this.client = HttpClient.newBuilder()- .authenticator(authenticator)- .build();+ this.client = HttpClient.newBuilder().authenticator(authenticator).build();this.nexusUri = params.nexusUri;}public String requestProfileId(PomInfo pomInfo) throws IOException {- String result = sendGet("/service/local/staging/profile_evaluate", Map.of(- "g", pomInfo.groupId,- "a", pomInfo.artifactId,- "v", pomInfo.version,- "t", "maven2"- ));+ String result =+ sendGet(+ "/service/local/staging/profile_evaluate",+ Map.of(+ "g", pomInfo.groupId,+ "a", pomInfo.artifactId,+ "v", pomInfo.version,+ "t", "maven2"));return XmlElement.parse(result).onlychild("stagingProfiles")@@ -196,14 +195,21 @@ public class StageArtifacts {}public String createStagingRepository(String profileId, String description) throws IOException {- String result = sendPost("/service/local/staging/profiles/" + URLEncoder.encode(profileId, StandardCharsets.UTF_8) + "/start",- "application/xml",- HttpURLConnection.HTTP_CREATED,- ("<promoteRequest>\n" +- " <data>\n" +- " <description><![CDATA[" + description + "]]></description>\n" +- " </data>\n" +- "</promoteRequest>").getBytes(StandardCharsets.UTF_8));+ String result =+ sendPost(+ "/service/local/staging/profiles/"+ + URLEncoder.encode(profileId, StandardCharsets.UTF_8)+ + "/start",+ "application/xml",+ HttpURLConnection.HTTP_CREATED,+ ("<promoteRequest>\n"+ + " <data>\n"+ + " <description><![CDATA["+ + description+ + "]]></description>\n"+ + " </data>\n"+ + "</promoteRequest>")+ .getBytes(StandardCharsets.UTF_8));return XmlElement.parse(result).onlychild("promoteResponse")@@ -212,8 +218,10 @@ public class StageArtifacts {.text();}- public void uploadArtifact(String stagingRepoId, Path path, String relativePath) throws IOException {- sendPost("/service/local/staging/deployByRepositoryId/"+ public void uploadArtifact(String stagingRepoId, Path path, String relativePath)+ throws IOException {+ sendPost(+ "/service/local/staging/deployByRepositoryId/"+ URLEncoder.encode(stagingRepoId, StandardCharsets.UTF_8)+ "/"+ relativePath,@@ -223,32 +231,45 @@ public class StageArtifacts {}public void closeStagingRepository(String profileId, String stagingRepoId) throws IOException {- sendPost("/service/local/staging/profiles/" + URLEncoder.encode(profileId, StandardCharsets.UTF_8) + "/finish",+ sendPost(+ "/service/local/staging/profiles/"+ + URLEncoder.encode(profileId, StandardCharsets.UTF_8)+ + "/finish","application/xml",HttpURLConnection.HTTP_CREATED,- ("<promoteRequest>\n" +- " <data>\n" +- " <stagedRepositoryId><![CDATA[" + stagingRepoId + "]]></stagedRepositoryId>\n" +- " </data>\n" +- "</promoteRequest>").getBytes(StandardCharsets.UTF_8));+ ("<promoteRequest>\n"+ + " <data>\n"+ + " <stagedRepositoryId><![CDATA["+ + stagingRepoId+ + "]]></stagedRepositoryId>\n"+ + " </data>\n"+ + "</promoteRequest>")+ .getBytes(StandardCharsets.UTF_8));}- private String sendPost(String serviceEndpoint, String contentType, int expectedStatus, byte[] bytes) throws IOException {+ private String sendPost(+ String serviceEndpoint, String contentType, int expectedStatus, byte[] bytes)+ throws IOException {URI target = nexusUri.resolve(serviceEndpoint);try {- HttpResponse.BodyHandler<String> bodyHandler = HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8);- HttpRequest req = HttpRequest.newBuilder()- .POST(HttpRequest.BodyPublishers.ofByteArray(bytes))- .header("Content-Type", contentType)- .uri(target)- // we could use json if XML is too difficult to work with.- // .header("Accept", "application/json")- .build();+ HttpResponse.BodyHandler<String> bodyHandler =+ HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8);+ HttpRequest req =+ HttpRequest.newBuilder()+ .POST(HttpRequest.BodyPublishers.ofByteArray(bytes))+ .header("Content-Type", contentType)+ .uri(target)+ // we could use json if XML is too difficult to work with.+ // .header("Accept", "application/json")+ .build();HttpResponse<String> response = client.send(req, bodyHandler);if (response.statusCode() != expectedStatus) {- throw new IOException("Unexpected HTTP error returned: " + response.statusCode() + ", response body: "- + response.body());+ throw new IOException(+ "Unexpected HTTP error returned: "+ + response.statusCode()+ + ", response body: "+ + response.body());}return response.body();} catch (InterruptedException e) {@@ -261,21 +282,27 @@ public class StageArtifacts {URI target;try {target = nexusUri.resolve(serviceEndpoint);- target = new URI(- target.getScheme(), target.getUserInfo(), target.getHost(), target.getPort(),- target.getPath(),- getArgs.entrySet().stream()- .map(e -> entityEncode(e.getKey()) + "=" + entityEncode(e.getValue()))- .collect(Collectors.joining("&")),- null);-- HttpResponse.BodyHandler<String> bodyHandler = HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8);- HttpRequest req = HttpRequest.newBuilder()- .GET()- .uri(target)- // we could use json if XML is too difficult to work with.- // .header("Accept", "application/json")- .build();+ target =+ new URI(+ target.getScheme(),+ target.getUserInfo(),+ target.getHost(),+ target.getPort(),+ target.getPath(),+ getArgs.entrySet().stream()+ .map(e -> entityEncode(e.getKey()) + "=" + entityEncode(e.getValue()))+ .collect(Collectors.joining("&")),+ null);++ HttpResponse.BodyHandler<String> bodyHandler =+ HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8);+ HttpRequest req =+ HttpRequest.newBuilder()+ .GET()+ .uri(target)+ // we could use json if XML is too difficult to work with.+ // .header("Accept", "application/json")+ .build();HttpResponse<String> response = client.send(req, bodyHandler);if (response.statusCode() != HttpURLConnection.HTTP_OK) {throw new IOException("Unexpected HTTP error returned: " + response.statusCode());@@ -315,9 +342,8 @@ public class StageArtifacts {NodeList childNodes = element.getChildNodes();for (int i = 0, max = childNodes.getLength(); i < max; i++) {var child = childNodes.item(i);- if (child.getNodeType() == Node.ELEMENT_NODE &&- Objects.equals(child.getNodeName(), tagName)- ) {+ if (child.getNodeType() == Node.ELEMENT_NODE+ && Objects.equals(child.getNodeName(), tagName)) {children.add(new XmlElement(child));}}@@ -340,8 +366,7 @@ public class StageArtifacts {public static PomInfo extractPomInfo(Path path) throws IOException {PomInfo pomInfo = new PomInfo();XmlElement project =- XmlElement.parse(Files.readString(path, StandardCharsets.UTF_8))- .onlychild("project");+ XmlElement.parse(Files.readString(path, StandardCharsets.UTF_8)).onlychild("project");pomInfo.groupId = project.onlychild("groupId").text();pomInfo.artifactId = project.onlychild("artifactId").text();pomInfo.version = project.onlychild("version").text();@@ -357,21 +382,23 @@ public class StageArtifacts {// Collect all files to be uploaded.List<Path> artifacts;try (Stream<Path> pathStream = Files.walk(params.mavenDir)) {- artifacts = pathStream- .filter(Files::isRegularFile)- // Ignore locally generated maven metadata files.- .filter(path -> !path.getFileName().toString().startsWith("maven-metadata."))- .sorted(Comparator.comparing(Path::toString))- .toList();+ artifacts =+ pathStream+ .filter(Files::isRegularFile)+ // Ignore locally generated maven metadata files.+ .filter(path -> !path.getFileName().toString().startsWith("maven-metadata."))+ .sorted(Comparator.comparing(Path::toString))+ .toList();}// Figure out nexus profile ID based on POMs. It is assumed that all artifacts// fall under the same profile.- PomInfo pomInfo = PomInfo.extractPomInfo(- artifacts.stream()- .filter(path -> path.getFileName().toString().endsWith(".pom"))- .findFirst()- .orElseThrow());+ PomInfo pomInfo =+ PomInfo.extractPomInfo(+ artifacts.stream()+ .filter(path -> path.getFileName().toString().endsWith(".pom"))+ .findFirst()+ .orElseThrow());// Sanity check for directory structure - all files should have the groupId folder prefix.{@@ -385,25 +412,40 @@ public class StageArtifacts {String relativeUrl = String.join("/", urlSegments);if (!relativeUrl.startsWith(expectedPrefix)) {- throw new RuntimeException("Maven folder structure does not match the expected groupId, "- + "expected prefix: " + expectedPrefix + ", artifact: " + relativeUrl);+ throw new RuntimeException(+ "Maven folder structure does not match the expected groupId, "+ + "expected prefix: "+ + expectedPrefix+ + ", artifact: "+ + relativeUrl);}}}- System.out.println("Requesting profile ID for artifact: "- + pomInfo.groupId + ":" + pomInfo.artifactId + ":" + pomInfo.version);+ System.out.println(+ "Requesting profile ID for artifact: "+ + pomInfo.groupId+ + ":"+ + pomInfo.artifactId+ + ":"+ + pomInfo.version);String profileId = nexus.requestProfileId(pomInfo);System.out.println(" => Profile ID: " + profileId);System.out.println("Creating staging repository.");- String description = Objects.requireNonNullElse(params.description,- "Staging repository: " + pomInfo.groupId + "."- + pomInfo.artifactId + ":" + pomInfo.version);+ String description =+ Objects.requireNonNullElse(+ params.description,+ "Staging repository: "+ + pomInfo.groupId+ + "."+ + pomInfo.artifactId+ + ":"+ + pomInfo.version);String stagingRepoId = nexus.createStagingRepository(profileId, description);System.out.println(" => Staging repository ID: " + stagingRepoId);- System.out.printf("Uploading %s artifact(s).%n", artifacts.size());+ System.out.printf(Locale.ROOT, "Uploading %s artifact(s).%n", artifacts.size());for (Path path : artifacts) {Path relative = params.mavenDir.relativize(path);List<String> urlSegments = new ArrayList<>();@@ -418,10 +460,12 @@ public class StageArtifacts {System.out.println("Closing the staging repository.");nexus.closeStagingRepository(profileId, stagingRepoId);System.out.println(" => Staging repository is available at: ");- System.out.println(" https://repository.apache.org/content/repositories/" + stagingRepoId);+ System.out.println(+ " https://repository.apache.org/content/repositories/" + stagingRepoId);System.out.println();- System.out.println("You must review and release the staging repository manually from Nexus GUI!");+ System.out.println(+ "You must review and release the staging repository manually from Nexus GUI!");} catch (Exception e) {System.err.println("Something went wrong: " + e.getMessage());System.exit(1);diff --git a/dev-tools/scripts/releaseWizard.yaml b/dev-tools/scripts/releaseWizard.yamlindex a273d3cef45..bdde7ab5b03 100644--- a/dev-tools/scripts/releaseWizard.yaml+++ b/dev-tools/scripts/releaseWizard.yaml@@ -969,7 +969,7 @@ groups:commands_text: In the source checkout do the following (note that this step will prompt you for your Apache LDAP credentials)commands:- !Command- cmd: java dev-tools/scripts/StageArtifacts.java --user {{ gpg.apache_id }} --description "{{ 'Apache Lucene ', release_version, ' (commit ', git_sha, ')' }}" "{{ [dist_file_path, dist_folder, 'lucene', 'maven'] | path_join }}"+ cmd: java build-tools/build-infra/src/main/java/org/apache/lucene/scripts/StageArtifacts.java --user {{ gpg.apache_id }} --description "{{ 'Apache Lucene ', release_version, ' (commit ', git_sha, ')' }}" "{{ [dist_file_path, dist_folder, 'lucene', 'maven'] | path_join }}"tee: truelogfile: publish_lucene_maven.logpost_description: The artifacts are not published yet, please proceed with the next step to actually publish!diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex 8c8cbc48c7e..68cda9f55ed 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -15,7 +15,7 @@ errorprone = "2.42.0"# markdown documentation generationflexmark = "0.64.8"# @keep This is GJF version for java source code formatting-googleJavaFormat = "1.30.0"+googleJavaFormat = "1.31.0"# gradle build supportgroovy = "5.0.2"# test assertions