|
| 1 | +//DEPS org.apache.maven:maven-model:3.9.9 |
| 2 | + |
| 3 | +import org.apache.maven.model.Model; |
| 4 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 5 | +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.nio.file.*; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +public class mvn2jbang { |
| 13 | + |
| 14 | + static Model readModel(Path pom) throws IOException, XmlPullParserException { |
| 15 | + try (BufferedReader br = Files.newBufferedReader(pom)) { |
| 16 | + Model model = readModel(br); |
| 17 | + model.setPomFile(pom.toFile()); |
| 18 | + return model; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + static Model readModel(Reader rdr) throws IOException, XmlPullParserException { |
| 23 | + try (Reader reader = rdr) { |
| 24 | + MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader(); |
| 25 | + return mavenXpp3Reader.read(reader); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + static void writeTags(PrintWriter writer, Model model, boolean usePrefix) { |
| 30 | + String prefix = usePrefix ? "//" : ""; |
| 31 | + writer.println(prefix + "DEPS " + model.getDependencies().stream() |
| 32 | + .map(d -> d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getVersion()) |
| 33 | + .collect(Collectors.joining("\n//DEPS "))); |
| 34 | + if (model.getGroupId() != null && model.getArtifactId() != null && model.getVersion() != null) { |
| 35 | + writer.println(prefix + "GAV " + model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion()); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static void main(String... args) { |
| 40 | + if (args.length == 1 && ("--help".equals(args[0])) || args.length > 2) { |
| 41 | + System.err.println("Usage: mvn2jbang [<pom_file_or_folder>] [<output_file>]"); |
| 42 | + System.exit(1); |
| 43 | + } |
| 44 | + try { |
| 45 | + Path pom = Paths.get(args.length > 0 ? args[0] : "pom.xml"); |
| 46 | + Path output = Paths.get(args.length > 1 ? args[1] : "build.jbang"); |
| 47 | + if (!Files.exists(pom)) { |
| 48 | + System.err.println("Pom file not found: " + pom); |
| 49 | + System.exit(2); |
| 50 | + } |
| 51 | + Model model = readModel(pom); |
| 52 | + boolean isJavaFile = output.toString().endsWith(".java"); |
| 53 | + boolean append = isJavaFile && Files.exists(output); |
| 54 | + Path output2 = append ? Paths.get(output.toString() + ".tmp") : output; |
| 55 | + try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(output2))) { |
| 56 | + writeTags(writer, model, isJavaFile); |
| 57 | + if (append) { |
| 58 | + try (Stream<String> lines = Files.lines(output)) { |
| 59 | + lines.forEach(writer::println); |
| 60 | + } |
| 61 | + } |
| 62 | + } catch (IOException e) { |
| 63 | + System.err.println("Error writing output: " + e.getMessage()); |
| 64 | + System.exit(4); |
| 65 | + } |
| 66 | + if (append) { |
| 67 | + Files.move(output, Paths.get(output.toString() + ".bak"), StandardCopyOption.REPLACE_EXISTING); |
| 68 | + Files.move(output2, output); |
| 69 | + } |
| 70 | + } catch (IOException | XmlPullParserException e) { |
| 71 | + System.err.println("Error reading pom: " + e.getMessage()); |
| 72 | + System.exit(3); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments