View Javadoc
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  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   * Run the JDK toolchain discovery mechanism and generates a toolchains XML.
39   *
40   * @since 3.2.0
41   */
42  @Mojo(name = "generate-jdk-toolchains-xml", requiresProject = false)
43  public class GenerateJdkToolchainsXmlMojo extends AbstractMojo {
44  
45      /**
46       * The path and name pf the toolchain XML file that will be generated.
47       * If not provided, the XML will be written to the standard output.
48       */
49      @Parameter(property = "toolchain.file")
50      String file;
51  
52      /**
53       * Toolchain discoverer
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  }