|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.Paths; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +/** |
| 28 | + * Parses .mvn/jvm.config file for Windows batch scripts. |
| 29 | + * This avoids the complexity of parsing special characters (pipes, quotes, etc.) in batch scripts. |
| 30 | + * |
| 31 | + * Usage: java JvmConfigParser.java <jvm.config-path> <maven-project-basedir> |
| 32 | + * |
| 33 | + * Outputs: Space-separated JVM arguments with proper escaping for batch scripts |
| 34 | + */ |
| 35 | +public class JvmConfigParser { |
| 36 | + public static void main(String[] args) { |
| 37 | + if (args.length != 2) { |
| 38 | + System.err.println("Usage: java JvmConfigParser.java <jvm.config-path> <maven-project-basedir>"); |
| 39 | + System.exit(1); |
| 40 | + } |
| 41 | + |
| 42 | + Path jvmConfigPath = Paths.get(args[0]); |
| 43 | + String mavenProjectBasedir = args[1]; |
| 44 | + |
| 45 | + if (!Files.exists(jvmConfigPath)) { |
| 46 | + // No jvm.config file - output nothing |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + try (Stream<String> lines = Files.lines(jvmConfigPath, StandardCharsets.UTF_8)) { |
| 51 | + StringBuilder result = new StringBuilder(); |
| 52 | + |
| 53 | + lines.forEach(line -> { |
| 54 | + // Remove comments |
| 55 | + int commentIndex = line.indexOf('#'); |
| 56 | + if (commentIndex >= 0) { |
| 57 | + line = line.substring(0, commentIndex); |
| 58 | + } |
| 59 | + |
| 60 | + // Trim whitespace |
| 61 | + line = line.trim(); |
| 62 | + |
| 63 | + // Skip empty lines |
| 64 | + if (line.isEmpty()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Replace MAVEN_PROJECTBASEDIR placeholders |
| 69 | + line = line.replace("${MAVEN_PROJECTBASEDIR}", mavenProjectBasedir); |
| 70 | + line = line.replace("$MAVEN_PROJECTBASEDIR", mavenProjectBasedir); |
| 71 | + |
| 72 | + // Append to result with space separator |
| 73 | + if (result.length() > 0) { |
| 74 | + result.append(' '); |
| 75 | + } |
| 76 | + result.append(line); |
| 77 | + }); |
| 78 | + |
| 79 | + System.out.print(result.toString()); |
| 80 | + } catch (IOException e) { |
| 81 | + System.err.println("Error reading jvm.config: " + e.getMessage()); |
| 82 | + System.exit(1); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments