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.archiver;
20
21 import java.util.Arrays;
22 import java.util.Map;
23
24 import org.apache.maven.model.Plugin;
25 import org.apache.maven.model.PluginContainer;
26 import org.apache.maven.project.MavenProject;
27 import org.codehaus.plexus.util.xml.Xpp3Dom;
28
29 /**
30 * Helper to detect info about build info in a MavenProject, as configured in plugins.
31 *
32 * @since 3.6.5
33 */
34 public class BuildHelper {
35 /**
36 * Tries to determine the target Java release from the following sources (until one is found)
37 * <ol>
38 * <li>use {@code release} configuration of {@code org.apache.maven.plugins:maven-compiler-plugin}</li>
39 * <li>use {@code maven.compiler.release<} property</li>
40 * <li>use {@code target} configuration of {@code org.apache.maven.plugins:maven-compiler-plugin}</li>
41 * <li>use {@code maven.compiler.target} property</li>
42 * </ol>
43 *
44 * @param project not null
45 * @return the Java release version configured in the project, or null if not configured
46 */
47 public static String discoverJavaRelease(MavenProject project) {
48 Plugin compiler = getCompilerPlugin(project);
49
50 String jdk = getPluginParameter(project, compiler, "release", "maven.compiler.release");
51
52 if (jdk == null) {
53 jdk = getPluginParameter(project, compiler, "target", "maven.compiler.target");
54 }
55
56 return normalizeJavaVersion(jdk);
57 }
58
59 /**
60 * Normalize Java version, for versions 5 to 8 where there is a 1.x alias.
61 *
62 * @param jdk can be null
63 * @return normalized version if an alias was used
64 */
65 public static String normalizeJavaVersion(String jdk) {
66 if (jdk != null
67 && jdk.length() == 3
68 && Arrays.asList("1.5", "1.6", "1.7", "1.8").contains(jdk)) {
69 jdk = jdk.substring(2);
70 }
71 return jdk;
72 }
73
74 public static Plugin getCompilerPlugin(MavenProject project) {
75 return getPlugin(project, "org.apache.maven.plugins:maven-compiler-plugin");
76 }
77
78 /**
79 * Get plugin from project based on coordinates {@code groupId:artifactId}.
80 *
81 * @param project not null
82 * @param pluginGa {@code groupId:artifactId}
83 * @return the plugin from build or pluginManagement, if available in project
84 */
85 public static Plugin getPlugin(MavenProject project, String pluginGa) {
86 Plugin plugin = getPlugin(project.getBuild(), pluginGa);
87 if (plugin == null) {
88 plugin = getPlugin(project.getPluginManagement(), pluginGa);
89 }
90 return plugin;
91 }
92
93 /**
94 * Get plugin parameter value if configured in current project.
95 *
96 * @param project not null
97 * @param plugin can be null
98 * @param parameter the parameter name when configured in plugin's configuration
99 * @param defaultValueProperty the property name when default value is used for the plugin parameter
100 * @return the value, or null if not configured at all, but using internal default from plugin.
101 */
102 public static String getPluginParameter(
103 MavenProject project, Plugin plugin, String parameter, String defaultValueProperty) {
104 String value = getPluginParameter(plugin, parameter);
105 if (value == null) {
106 value = project.getProperties().getProperty(defaultValueProperty);
107 }
108 return value;
109 }
110
111 private static Plugin getPlugin(PluginContainer container, String pluginGa) {
112 if (container == null) {
113 return null;
114 }
115 Map<String, Plugin> pluginsAsMap = container.getPluginsAsMap();
116 return pluginsAsMap.get(pluginGa);
117 }
118
119 private static String getPluginParameter(Plugin plugin, String parameter) {
120 if (plugin != null) {
121 Xpp3Dom pluginConf = (Xpp3Dom) plugin.getConfiguration();
122
123 if (pluginConf != null) {
124 Xpp3Dom target = pluginConf.getChild(parameter);
125
126 if (target != null) {
127 return target.getValue();
128 }
129 }
130 }
131 return null;
132 }
133 }