|
| 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 && jdk.length() == 3 && Arrays.asList("1.5", "1.6", "1.7", "1.8").contains(jdk)) { |
| 67 | + jdk = jdk.substring(2); |
| 68 | + } |
| 69 | + return jdk; |
| 70 | + } |
| 71 | + |
| 72 | + public static Plugin getCompilerPlugin(MavenProject project) { |
| 73 | + return getPlugin(project, "org.apache.maven.plugins:maven-compiler-plugin"); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get plugin from project based on coordinates {@code groupId:artifactId}. |
| 78 | + * |
| 79 | + * @param project not null |
| 80 | + * @param pluginGa {@code groupId:artifactId} |
| 81 | + * @return the plugin from build or pluginManagement, if available in project |
| 82 | + */ |
| 83 | + public static Plugin getPlugin(MavenProject project, String pluginGa) { |
| 84 | + Plugin plugin = getPlugin(project.getBuild(), pluginGa); |
| 85 | + if (plugin == null) { |
| 86 | + plugin = getPlugin(project.getPluginManagement(), pluginGa); |
| 87 | + } |
| 88 | + return plugin; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get plugin parameter value if configured in current project. |
| 93 | + * |
| 94 | + * @param project not null |
| 95 | + * @param plugin can be null |
| 96 | + * @param parameter the parameter name when configured in plugin's configuration |
| 97 | + * @param defaultValueProperty the property name when default value is used for the plugin parameter |
| 98 | + * @return the value, or null if not configured at all, but using internal default from plugin. |
| 99 | + */ |
| 100 | + public static String getPluginParameter( |
| 101 | + MavenProject project, Plugin plugin, String parameter, String defaultValueProperty) { |
| 102 | + String value = getPluginParameter(plugin, parameter); |
| 103 | + if (value == null) { |
| 104 | + value = project.getProperties().getProperty(defaultValueProperty); |
| 105 | + } |
| 106 | + return value; |
| 107 | + } |
| 108 | + |
| 109 | + private static Plugin getPlugin(PluginContainer container, String pluginGa) { |
| 110 | + if (container == null) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + Map<String, Plugin> pluginsAsMap = container.getPluginsAsMap(); |
| 114 | + return pluginsAsMap.get(pluginGa); |
| 115 | + } |
| 116 | + |
| 117 | + private static String getPluginParameter(Plugin plugin, String parameter) { |
| 118 | + if (plugin != null) { |
| 119 | + Xpp3Dom pluginConf = (Xpp3Dom) plugin.getConfiguration(); |
| 120 | + |
| 121 | + if (pluginConf != null) { |
| 122 | + Xpp3Dom target = pluginConf.getChild(parameter); |
| 123 | + |
| 124 | + if (target != null) { |
| 125 | + return target.getValue(); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return null; |
| 130 | + } |
| 131 | +} |
0 commit comments