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