1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.toolchain.jdk;
20
21 import javax.inject.Inject;
22
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.io.Writer;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.toolchain.model.PersistedToolchains;
35 import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Writer;
36
37
38
39
40
41
42 @Mojo(name = "generate-jdk-toolchains-xml", requiresProject = false)
43 public class GenerateJdkToolchainsXmlMojo extends AbstractMojo {
44
45
46
47
48
49 @Parameter(property = "toolchain.file")
50 String file;
51
52
53
54
55 @Inject
56 ToolchainDiscoverer discoverer;
57
58 @Override
59 public void execute() throws MojoFailureException {
60 try {
61 PersistedToolchains toolchains = discoverer.discoverToolchains();
62 if (file != null) {
63 Path file = Paths.get(this.file).toAbsolutePath();
64 Files.createDirectories(file.getParent());
65 try (Writer writer = Files.newBufferedWriter(file)) {
66 new MavenToolchainsXpp3Writer().write(writer, toolchains);
67 }
68 } else {
69 StringWriter writer = new StringWriter();
70 new MavenToolchainsXpp3Writer().write(writer, toolchains);
71 System.out.println(writer);
72 }
73 } catch (IOException e) {
74 throw new MojoFailureException("Unable to generate toolchains.xml", e);
75 }
76 }
77 }