001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.api.batch.maven;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.apache.maven.model.Plugin;
024 import org.apache.maven.model.ReportPlugin;
025 import org.apache.maven.project.MavenProject;
026 import org.slf4j.LoggerFactory;
027
028 import java.nio.charset.Charset;
029 import java.util.Collection;
030
031 /**
032 * An utility class to manipulate Maven concepts
033 *
034 * @since 1.10
035 */
036 public final class MavenUtils {
037
038 private static final String MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin";
039 public static final String GROUP_ID_APACHE_MAVEN = "org.apache.maven.plugins";
040 public static final String GROUP_ID_CODEHAUS_MOJO = "org.codehaus.mojo";
041
042 private MavenUtils() {
043 // utility class with only static methods
044 }
045
046 /**
047 * Returns the version of Java used by the maven compiler plugin
048 *
049 * @param pom the project pom
050 * @return the java version
051 */
052 public static String getJavaVersion(MavenProject pom) {
053 MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, MAVEN_COMPILER_PLUGIN);
054 if (compilerPlugin != null) {
055 return compilerPlugin.getParameter("target");
056 }
057 return null;
058 }
059
060 public static String getJavaSourceVersion(MavenProject pom) {
061 MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, MAVEN_COMPILER_PLUGIN);
062 if (compilerPlugin != null) {
063 return compilerPlugin.getParameter("source");
064 }
065 return null;
066 }
067
068 /**
069 * Queries a collection of plugins based on a group id and an artifact id and returns the plugin if it exists
070 *
071 * @param plugins the plugins collection
072 * @param groupId the group id
073 * @param artifactId the artifact id
074 * @return the corresponding plugin if it exists, null otherwise
075 */
076 public static Plugin getPlugin(Collection<Plugin> plugins, String groupId, String artifactId) {
077 if (plugins != null) {
078 for (Plugin plugin : plugins) {
079 if (equals(plugin, groupId, artifactId)) {
080 return plugin;
081 }
082 }
083 }
084 return null;
085 }
086
087 /**
088 * Tests whether a plugin has got a given artifact id and group id
089 *
090 * @param plugin the plugin to test
091 * @param groupId the group id
092 * @param artifactId the artifact id
093 * @return whether the plugin has got group + artifact ids
094 */
095 public static boolean equals(Plugin plugin, String groupId, String artifactId) {
096 if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
097 if (plugin.getGroupId() == null) {
098 return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
099 }
100 return plugin.getGroupId().equals(groupId);
101 }
102 return false;
103 }
104
105 /**
106 * Tests whether a ReportPlugin has got a given artifact id and group id
107 *
108 * @param plugin the ReportPlugin to test
109 * @param groupId the group id
110 * @param artifactId the artifact id
111 * @return whether the ReportPlugin has got group + artifact ids
112 */
113 public static boolean equals(ReportPlugin plugin, String groupId, String artifactId) {
114 if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
115 if (plugin.getGroupId() == null) {
116 return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
117 }
118 return plugin.getGroupId().equals(groupId);
119 }
120 return false;
121 }
122
123 /**
124 * @return source encoding
125 */
126 public static String getSourceEncoding(MavenProject pom) {
127 return pom.getProperties().getProperty("project.build.sourceEncoding");
128 }
129
130 /**
131 * Returns the charset of a pom
132 *
133 * @param pom the project pom
134 * @return the charset
135 */
136 public static Charset getSourceCharset(MavenProject pom) {
137 String encoding = getSourceEncoding(pom);
138 if (StringUtils.isNotEmpty(encoding)) {
139 try {
140 return Charset.forName(encoding);
141
142 } catch (Exception e) {
143 LoggerFactory.getLogger(MavenUtils.class).warn("Can not get project charset", e);
144 }
145 }
146 return Charset.defaultCharset();
147 }
148 }