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.jlink;
20  
21  /*
22   * Licensed to the Apache Software Foundation (ASF) under one
23   * or more contributor license agreements.  See the NOTICE file
24   * distributed with this work for additional information
25   * regarding copyright ownership.  The ASF licenses this file
26   * to you under the Apache License, Version 2.0 (the
27   * "License"); you may not use this file except in compliance
28   * with the License.  You may obtain a copy of the License at
29   *
30   *   http://www.apache.org/licenses/LICENSE-2.0
31   *
32   * Unless required by applicable law or agreed to in writing,
33   * software distributed under the License is distributed on an
34   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35   * KIND, either express or implied.  See the License for the
36   * specific language governing permissions and limitations
37   * under the License.
38   */
39  
40  import java.io.File;
41  import java.lang.reflect.Method;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Optional;
45  
46  import org.apache.maven.execution.MavenSession;
47  import org.apache.maven.plugin.AbstractMojo;
48  import org.apache.maven.plugins.annotations.Parameter;
49  import org.apache.maven.project.MavenProject;
50  import org.apache.maven.toolchain.Toolchain;
51  import org.apache.maven.toolchain.ToolchainManager;
52  
53  /**
54   * @author Karl Heinz Marbaise <a href="mailto:khmarbaise@apache.org">khmarbaise@apache.org</a>
55   */
56  public abstract class AbstractJLinkMojo extends AbstractMojo {
57      /**
58       * <p>
59       * Specify the requirements for this JDK toolchain. This overrules the toolchain selected by the
60       * maven-toolchain-plugin.
61       * </p>
62       * <strong>note:</strong> requires at least Maven 3.3.1.
63       */
64      @Parameter
65      private Map<String, String> jdkToolchain;
66  
67      @Parameter(defaultValue = "${project}", readonly = true, required = true)
68      private MavenProject project;
69  
70      @Parameter(defaultValue = "${session}", readonly = true, required = true)
71      private MavenSession session;
72  
73      private final ToolchainManager toolchainManager;
74  
75      public AbstractJLinkMojo(ToolchainManager toolchainManager) {
76          this.toolchainManager = toolchainManager;
77      }
78  
79      /**
80       * Overload this to produce a zip with another classifier, for example a jlink-zip.
81       *
82       * @return get the classifier
83       */
84      protected abstract String getClassifier();
85  
86      protected JLinkExecutor getJlinkExecutor() {
87          return new JLinkExecutor(getToolchain().orElse(null), getLog());
88      }
89  
90      protected Optional<Toolchain> getToolchain() {
91          Toolchain tc = null;
92  
93          if (jdkToolchain != null) {
94              // Maven 3.3.1 has plugin execution scoped Toolchain Support
95              try {
96                  Method getToolchainsMethod = toolchainManager
97                          .getClass()
98                          .getMethod("getToolchains", MavenSession.class, String.class, Map.class);
99  
100                 @SuppressWarnings("unchecked")
101                 List<Toolchain> tcs = (List<Toolchain>)
102                         getToolchainsMethod.invoke(toolchainManager, getSession(), "jdk", jdkToolchain);
103 
104                 if (tcs != null && tcs.size() > 0) {
105                     tc = tcs.get(0);
106                 }
107             } catch (ReflectiveOperationException | SecurityException | IllegalArgumentException e) {
108                 // ignore
109             }
110         }
111 
112         if (tc == null) {
113             // TODO: Check if we should make the type configurable?
114             tc = toolchainManager.getToolchainFromBuildContext("jdk", getSession());
115         }
116 
117         return Optional.ofNullable(tc);
118     }
119 
120     protected MavenProject getProject() {
121         return project;
122     }
123 
124     protected MavenSession getSession() {
125         return session;
126     }
127 
128     /**
129      * This will convert a module path separated by either {@code :} or {@code ;} into a string which uses the platform
130      * path separator uniformly.
131      *
132      * @param pluginModulePath the module path
133      * @return the platform separated module path
134      */
135     protected StringBuilder convertSeparatedModulePathToPlatformSeparatedModulePath(String pluginModulePath) {
136         StringBuilder sb = new StringBuilder();
137         // Split the module path by either ":" or ";" Linux/Windows path separator and
138         // convert uniformly to the platform separator.
139         String[] splitModule = pluginModulePath.split("[;:]");
140         for (String module : splitModule) {
141             if (sb.length() > 0) {
142                 sb.append(File.pathSeparatorChar);
143             }
144             sb.append(module);
145         }
146         return sb;
147     }
148 }