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.util.List;
24  
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.toolchain.model.PersistedToolchains;
29  import org.apache.maven.toolchain.model.ToolchainModel;
30  import org.codehaus.plexus.util.xml.Xpp3Dom;
31  
32  import static java.util.Comparator.comparing;
33  import static org.apache.maven.plugins.toolchain.jdk.ToolchainDiscoverer.SORTED_PROVIDES;
34  
35  /**
36   * Discover the JDK toolchains and print them to the console.
37   *
38   * @since 3.2.0
39   */
40  @Mojo(name = "display-discovered-jdk-toolchains", requiresProject = false)
41  public class DisplayDiscoveredJdkToolchainsMojo extends AbstractMojo {
42  
43      /**
44       * Comparator used to sort JDK toolchains for selection.
45       * This property is a comma separated list of values which may contains:
46       * <ul>
47       * <li>{@code lts}: prefer JDK with LTS version</li>
48       * <li>{@code current}: prefer the current JDK</li>
49       * <li>{@code env}: prefer JDKs defined using {@code JAVA\{xx\}_HOME} environment variables</li>
50       * <li>{@code version}: prefer JDK with higher versions</li>
51       * <li>{@code vendor}: order JDK by vendor name (usually as a last comparator to ensure a stable order)</li>
52       * </ul>
53       */
54      @Parameter(property = "toolchain.jdk.comparator", defaultValue = "lts,current,env,version,vendor")
55      String comparator;
56  
57      /**
58       * Toolchain discoverer
59       */
60      @Inject
61      ToolchainDiscoverer discoverer;
62  
63      @Override
64      public void execute() {
65          PersistedToolchains toolchains = discoverer.discoverToolchains(comparator);
66          List<ToolchainModel> models = toolchains.getToolchains();
67          getLog().info("Discovered " + models.size() + " JDK toolchains:");
68          for (ToolchainModel model : models) {
69              getLog().info("  - "
70                      + ((Xpp3Dom) model.getConfiguration()).getChild("jdkHome").getValue());
71              getLog().info("    provides:");
72              model.getProvides().entrySet().stream()
73                      .sorted(comparing(e -> SORTED_PROVIDES.indexOf(e.getKey().toString())))
74                      .forEach(e -> getLog().info("      " + e.getKey() + ": " + e.getValue()));
75          }
76      }
77  }