|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and in any patent rights owned or freely |
| 11 | + * licensable by each licensor hereunder covering either (i) the unmodified |
| 12 | + * Software as contributed to or provided by such licensor, or (ii) the Larger |
| 13 | + * Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and the |
| 23 | + * Larger Work(s), and to make, use, sell, offer for sale, import, export, have |
| 24 | + * made, and have sold the Software and the Larger Work(s), and to sublicense the |
| 25 | + * foregoing rights on either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at |
| 30 | + * a minimum a reference to the UPL must be included in all copies or |
| 31 | + * substantial portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package org.graalvm.build.maven; |
| 42 | + |
| 43 | +import java.io.ByteArrayOutputStream; |
| 44 | +import java.io.OutputStream; |
| 45 | +import java.net.URI; |
| 46 | +import java.net.URISyntaxException; |
| 47 | +import java.nio.charset.StandardCharsets; |
| 48 | +import java.util.regex.Matcher; |
| 49 | +import java.util.regex.Pattern; |
| 50 | + |
| 51 | +/** |
| 52 | + * Bounds embedded Maven output and renders one safe actionable failure. §E2E-functional-tests.4 |
| 53 | + */ |
| 54 | +final class MavenOutput extends OutputStream { |
| 55 | + private static final int MAXIMUM_BYTES = 4 * 1024 * 1024; |
| 56 | + private static final Pattern ANSI_ESCAPE = Pattern.compile("\\x1B(?:\\[[0-?]*[ -/]*[@-~]|\\][^\\x07]*(?:\\x07|\\x1B\\\\))"); |
| 57 | + private static final Pattern RESOLUTION_FAILURE = Pattern.compile( |
| 58 | + "Could not (?:transfer|find) artifact ([^\\s]+) (?:from/to|in) ([^\\s(]+) \\(([^)]+)\\)", |
| 59 | + Pattern.CASE_INSENSITIVE); |
| 60 | + private static final Pattern CACHED_NOT_FOUND = Pattern.compile( |
| 61 | + "([^\\s]+) was not found in ([^\\s]+) during a previous attempt", |
| 62 | + Pattern.CASE_INSENSITIVE); |
| 63 | + private static final Pattern SLF4J_ERROR = Pattern.compile("^\\[[^]]+]\\s+ERROR\\s+\\S+\\s+-\\s*(.*)$"); |
| 64 | + private static final Pattern URL = Pattern.compile("[a-zA-Z][a-zA-Z0-9+.-]*://[^\\s)]+(?:\\)[^\\s]*)?"); |
| 65 | + private static final String DIAGNOSTIC_HINT = "Rerun with --info or --debug for Maven diagnostics."; |
| 66 | + |
| 67 | + private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 68 | + private boolean truncated; |
| 69 | + |
| 70 | + @Override |
| 71 | + public synchronized void write(int value) { |
| 72 | + if (bytes.size() < MAXIMUM_BYTES) { |
| 73 | + bytes.write(value); |
| 74 | + } else { |
| 75 | + truncated = true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public synchronized void write(byte[] data, int offset, int length) { |
| 81 | + int remaining = MAXIMUM_BYTES - bytes.size(); |
| 82 | + if (remaining > 0) { |
| 83 | + bytes.write(data, offset, Math.min(length, remaining)); |
| 84 | + } |
| 85 | + if (length > remaining) { |
| 86 | + truncated = true; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + synchronized String contents() { |
| 91 | + String output = bytes.toString(StandardCharsets.UTF_8); |
| 92 | + if (truncated) { |
| 93 | + return output + System.lineSeparator() + "[embedded Maven output truncated]"; |
| 94 | + } |
| 95 | + return output; |
| 96 | + } |
| 97 | + |
| 98 | + static String failureMessage(String diagnostics) { |
| 99 | + String plainOutput = ANSI_ESCAPE.matcher(diagnostics).replaceAll(""); |
| 100 | + Matcher resolution = RESOLUTION_FAILURE.matcher(plainOutput); |
| 101 | + if (resolution.find()) { |
| 102 | + String artifact = resolution.group(1); |
| 103 | + String repository = resolution.group(2); |
| 104 | + String url = sanitizeUrl(resolution.group(3)); |
| 105 | + String location = url == null ? repository : repository + " (" + url + ")"; |
| 106 | + return "Embedded Maven could not resolve artifact " + artifact + " from repository " + location + ". " + DIAGNOSTIC_HINT; |
| 107 | + } |
| 108 | + Matcher cachedNotFound = CACHED_NOT_FOUND.matcher(plainOutput); |
| 109 | + if (cachedNotFound.find()) { |
| 110 | + String artifact = cachedNotFound.group(1); |
| 111 | + String repository = sanitizeUrl(cachedNotFound.group(2)); |
| 112 | + String location = repository == null ? "[repository URL omitted]" : repository; |
| 113 | + return "Embedded Maven could not resolve artifact " + artifact + " from repository " + location + ". " + DIAGNOSTIC_HINT; |
| 114 | + } |
| 115 | + String detail = finalMeaningfulError(plainOutput); |
| 116 | + return "Embedded Maven failed: " + detail + ". " + DIAGNOSTIC_HINT; |
| 117 | + } |
| 118 | + |
| 119 | + private static String finalMeaningfulError(String output) { |
| 120 | + String[] lines = output.split("\\R"); |
| 121 | + for (int index = lines.length - 1; index >= 0; index--) { |
| 122 | + String detail = errorDetail(lines[index].trim()); |
| 123 | + if (detail == null) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + if (!detail.isEmpty() && !detail.startsWith("-> [Help ") && !detail.startsWith("[Help ") |
| 127 | + && !detail.startsWith("For more information")) { |
| 128 | + return stripTrailingPeriod(sanitizeUrls(detail)); |
| 129 | + } |
| 130 | + } |
| 131 | + return "Maven exited with a non-zero status"; |
| 132 | + } |
| 133 | + |
| 134 | + private static String errorDetail(String line) { |
| 135 | + if (line.startsWith("[ERROR]")) { |
| 136 | + return line.substring("[ERROR]".length()).trim(); |
| 137 | + } |
| 138 | + Matcher matcher = SLF4J_ERROR.matcher(line); |
| 139 | + return matcher.matches() ? matcher.group(1).trim() : null; |
| 140 | + } |
| 141 | + |
| 142 | + private static String sanitizeUrls(String text) { |
| 143 | + Matcher matcher = URL.matcher(text); |
| 144 | + StringBuffer sanitized = new StringBuffer(); |
| 145 | + while (matcher.find()) { |
| 146 | + String replacement = sanitizeUrl(matcher.group()); |
| 147 | + matcher.appendReplacement(sanitized, Matcher.quoteReplacement(replacement == null ? "[repository URL omitted]" : replacement)); |
| 148 | + } |
| 149 | + matcher.appendTail(sanitized); |
| 150 | + return sanitized.toString(); |
| 151 | + } |
| 152 | + |
| 153 | + private static String sanitizeUrl(String value) { |
| 154 | + try { |
| 155 | + URI uri = new URI(value); |
| 156 | + if (uri.getScheme() == null) { |
| 157 | + return null; |
| 158 | + } |
| 159 | + if (uri.isOpaque()) { |
| 160 | + return uri.getScheme() + ":"; |
| 161 | + } |
| 162 | + return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null).toASCIIString(); |
| 163 | + } catch (URISyntaxException ex) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private static String stripTrailingPeriod(String value) { |
| 169 | + return value.endsWith(".") ? value.substring(0, value.length() - 1) : value; |
| 170 | + } |
| 171 | +} |
0 commit comments