001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
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 * @since 1.10
033 */
034 public final class MavenUtils {
035
036 public static final String GROUP_ID_APACHE_MAVEN = "org.apache.maven.plugins";
037 public static final String GROUP_ID_CODEHAUS_MOJO = "org.codehaus.mojo";
038
039 private MavenUtils() {
040 // utility class with only static methods
041 }
042
043 public static String getJavaVersion(MavenProject pom) {
044 MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, "maven-compiler-plugin");
045 if (compilerPlugin != null) {
046 return compilerPlugin.getParameter("target");
047 }
048 return null;
049 }
050
051
052 public static Plugin getPlugin(Collection<Plugin> plugins, String groupId, String artifactId) {
053 if (plugins != null) {
054 for (Plugin plugin : plugins) {
055 if (equals(plugin, groupId, artifactId)) {
056 return plugin;
057 }
058 }
059 }
060 return null;
061 }
062
063 public static boolean equals(Plugin plugin, String groupId, String artifactId) {
064 if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
065 if (plugin.getGroupId() == null) {
066 return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
067 }
068 return plugin.getGroupId().equals(groupId);
069 }
070 return false;
071 }
072
073 public static boolean equals(ReportPlugin plugin, String groupId, String artifactId) {
074 if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
075 if (plugin.getGroupId() == null) {
076 return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
077 }
078 return plugin.getGroupId().equals(groupId);
079 }
080 return false;
081 }
082
083
084 public static Charset getSourceCharset(MavenProject pom) {
085 String encoding = pom.getProperties().getProperty("project.build.sourceEncoding");
086 if (StringUtils.isNotEmpty(encoding)) {
087 try {
088 return Charset.forName(encoding);
089
090 } catch (Throwable t) {
091 LoggerFactory.getLogger(MavenUtils.class).warn("Can not get project charset", t);
092 }
093 }
094 return Charset.defaultCharset();
095 }
096 }