|
| 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.shared.archiver; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.apache.maven.api.model.Model; |
| 25 | +import org.apache.maven.api.model.Plugin; |
| 26 | +import org.apache.maven.api.model.PluginContainer; |
| 27 | +import org.apache.maven.api.xml.XmlNode; |
| 28 | + |
| 29 | +/** |
| 30 | + * Helper to detect info about build info in a Maven model, as configured in plugins. |
| 31 | + * |
| 32 | + * @since 4.0.0-beta-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 model not null |
| 45 | + * @return the Java release version configured in the model, or null if not configured |
| 46 | + */ |
| 47 | + public static String discoverJavaRelease(Model model) { |
| 48 | + Plugin compiler = getCompilerPlugin(model); |
| 49 | + |
| 50 | + String jdk = getPluginParameter(model, compiler, "release", "maven.compiler.release"); |
| 51 | + |
| 52 | + if (jdk == null) { |
| 53 | + jdk = getPluginParameter(model, 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(Model model) { |
| 75 | + return getPlugin(model, "org.apache.maven.plugins:maven-compiler-plugin"); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get plugin from model based on coordinates {@code groupId:artifactId}. |
| 80 | + * |
| 81 | + * @param model 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(Model model, String pluginGa) { |
| 86 | + Plugin plugin = getPlugin(model.getBuild(), pluginGa); |
| 87 | + if (model.getBuild() != null && plugin == null) { |
| 88 | + plugin = getPlugin(model.getBuild().getPluginManagement(), pluginGa); |
| 89 | + } |
| 90 | + return plugin; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get plugin parameter value if configured in current model. |
| 95 | + * |
| 96 | + * @param model 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(Model model, Plugin plugin, String parameter, String defaultValueProperty) { |
| 103 | + String value = getPluginParameter(plugin, parameter); |
| 104 | + if (value == null) { |
| 105 | + value = model.getProperties().get(defaultValueProperty); |
| 106 | + } |
| 107 | + return value; |
| 108 | + } |
| 109 | + |
| 110 | + private static Plugin getPlugin(PluginContainer container, String pluginGa) { |
| 111 | + if (container == null) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + Map<String, Plugin> pluginsAsMap = container.getPluginsAsMap(); |
| 115 | + return pluginsAsMap.get(pluginGa); |
| 116 | + } |
| 117 | + |
| 118 | + private static String getPluginParameter(Plugin plugin, String parameter) { |
| 119 | + if (plugin != null) { |
| 120 | + XmlNode pluginConf = plugin.getConfiguration(); |
| 121 | + |
| 122 | + if (pluginConf != null) { |
| 123 | + XmlNode target = pluginConf.getChild(parameter); |
| 124 | + |
| 125 | + if (target != null) { |
| 126 | + return target.getValue(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return null; |
| 131 | + } |
| 132 | +} |
0 commit comments