001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 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 public static String getJavaSourceVersion(MavenProject pom) { 060 MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, "maven-compiler-plugin"); 061 if (compilerPlugin != null) { 062 return compilerPlugin.getParameter("source"); 063 } 064 return null; 065 } 066 067 /** 068 * Queries a collection of plugins based on a group id and an artifact id and returns the plugin if it exists 069 * 070 * @param plugins the plugins collection 071 * @param groupId the group id 072 * @param artifactId the artifact id 073 * @return the corresponding plugin if it exists, null otherwise 074 */ 075 public static Plugin getPlugin(Collection<Plugin> plugins, String groupId, String artifactId) { 076 if (plugins != null) { 077 for (Plugin plugin : plugins) { 078 if (equals(plugin, groupId, artifactId)) { 079 return plugin; 080 } 081 } 082 } 083 return null; 084 } 085 086 /** 087 * Tests whether a plugin has got a given artifact id and group id 088 * 089 * @param plugin the plugin to test 090 * @param groupId the group id 091 * @param artifactId the artifact id 092 * @return whether the plugin has got group + artifact ids 093 */ 094 public static boolean equals(Plugin plugin, String groupId, String artifactId) { 095 if (plugin != null && plugin.getArtifactId().equals(artifactId)) { 096 if (plugin.getGroupId() == null) { 097 return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO); 098 } 099 return plugin.getGroupId().equals(groupId); 100 } 101 return false; 102 } 103 104 /** 105 * Tests whether a ReportPlugin has got a given artifact id and group id 106 * 107 * @param plugin the ReportPlugin to test 108 * @param groupId the group id 109 * @param artifactId the artifact id 110 * @return whether the ReportPlugin has got group + artifact ids 111 */ 112 public static boolean equals(ReportPlugin plugin, String groupId, String artifactId) { 113 if (plugin != null && plugin.getArtifactId().equals(artifactId)) { 114 if (plugin.getGroupId() == null) { 115 return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO); 116 } 117 return plugin.getGroupId().equals(groupId); 118 } 119 return false; 120 } 121 122 /** 123 * @return source encoding 124 */ 125 public static String getSourceEncoding(MavenProject pom) { 126 return pom.getProperties().getProperty("project.build.sourceEncoding"); 127 } 128 129 /** 130 * Returns the charset of a pom 131 * 132 * @param pom the project pom 133 * @return the charset 134 */ 135 public static Charset getSourceCharset(MavenProject pom) { 136 String encoding = getSourceEncoding(pom); 137 if (StringUtils.isNotEmpty(encoding)) { 138 try { 139 return Charset.forName(encoding); 140 141 } catch (Exception e) { 142 LoggerFactory.getLogger(MavenUtils.class).warn("Can not get project charset", e); 143 } 144 } 145 return Charset.defaultCharset(); 146 } 147 }