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     * @deprecated since 4.5 we don't want any dependency on Maven anymore
036     */
037    @Deprecated
038    public final class MavenUtils {
039    
040      private static final String MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin";
041      public static final String GROUP_ID_APACHE_MAVEN = "org.apache.maven.plugins";
042      public static final String GROUP_ID_CODEHAUS_MOJO = "org.codehaus.mojo";
043    
044      private MavenUtils() {
045        // utility class with only static methods
046      }
047    
048      /**
049       * Returns the version of Java used by the maven compiler plugin
050       *
051       * @param pom the project pom
052       * @return the java version
053       */
054      public static String getJavaVersion(MavenProject pom) {
055        MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, MAVEN_COMPILER_PLUGIN);
056        if (compilerPlugin != null) {
057          return compilerPlugin.getParameter("target");
058        }
059        return null;
060      }
061    
062      public static String getJavaSourceVersion(MavenProject pom) {
063        MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, MAVEN_COMPILER_PLUGIN);
064        if (compilerPlugin != null) {
065          return compilerPlugin.getParameter("source");
066        }
067        return null;
068      }
069    
070      /**
071       * Queries a collection of plugins based on a group id and an artifact id and returns the plugin if it exists
072       *
073       * @param plugins the plugins collection
074       * @param groupId the group id
075       * @param artifactId the artifact id
076       * @return the corresponding plugin if it exists, null otherwise
077       */
078      public static Plugin getPlugin(Collection<Plugin> plugins, String groupId, String artifactId) {
079        if (plugins != null) {
080          for (Plugin plugin : plugins) {
081            if (equals(plugin, groupId, artifactId)) {
082              return plugin;
083            }
084          }
085        }
086        return null;
087      }
088    
089      /**
090       * Tests whether a plugin has got a given artifact id and group id
091       *
092       * @param plugin the plugin to test
093       * @param groupId the group id
094       * @param artifactId the artifact id
095       * @return whether the plugin has got group + artifact ids
096       */
097      public static boolean equals(Plugin plugin, String groupId, String artifactId) {
098        if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
099          if (plugin.getGroupId() == null) {
100            return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
101          }
102          return plugin.getGroupId().equals(groupId);
103        }
104        return false;
105      }
106    
107      /**
108       * Tests whether a ReportPlugin has got a given artifact id and group id
109       *
110       * @param plugin the ReportPlugin to test
111       * @param groupId the group id
112       * @param artifactId the artifact id
113       * @return whether the ReportPlugin has got group + artifact ids
114       */
115      public static boolean equals(ReportPlugin plugin, String groupId, String artifactId) {
116        if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
117          if (plugin.getGroupId() == null) {
118            return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
119          }
120          return plugin.getGroupId().equals(groupId);
121        }
122        return false;
123      }
124    
125      /**
126       * @return source encoding
127       */
128      public static String getSourceEncoding(MavenProject pom) {
129        return pom.getProperties().getProperty("project.build.sourceEncoding");
130      }
131    
132      /**
133       * Returns the charset of a pom
134       *
135       * @param pom the project pom
136       * @return the charset
137       */
138      public static Charset getSourceCharset(MavenProject pom) {
139        String encoding = getSourceEncoding(pom);
140        if (StringUtils.isNotEmpty(encoding)) {
141          try {
142            return Charset.forName(encoding);
143    
144          } catch (Exception e) {
145            LoggerFactory.getLogger(MavenUtils.class).warn("Can not get project charset", e);
146          }
147        }
148        return Charset.defaultCharset();
149      }
150    }