2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.updatecenter.mavenplugin;
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.project.MavenProject;
26 import org.apache.maven.project.MavenProjectHelper;
29 import java.util.HashSet;
30 import java.util.List;
34 * Base class for Sonar-plugin-packaging related tasks.
36 * @author Evgeny Mandrikov
38 public abstract class AbstractSonarPluginMojo extends AbstractMojo {
40 public static final String SONAR_GROUPID = "org.codehaus.sonar";
41 public static final String SONAR_PLUGIN_API_ARTIFACTID = "sonar-plugin-api";
42 public static final String SONAR_PLUGIN_API_TYPE = "jar";
47 * @parameter expression="${project}"
51 private MavenProject project;
54 * Directory containing the generated JAR.
56 * @parameter expression="${project.build.directory}"
59 private File outputDirectory;
62 * Directory containing the classes and resource files that should be packaged into the JAR.
64 * @parameter expression="${project.build.outputDirectory}"
67 private File classesDirectory;
70 * The directory where the app is built.
72 * @parameter expression="${project.build.directory}/${project.build.finalName}"
75 private File appDirectory;
78 * Name of the generated JAR.
80 * @parameter alias="jarName" expression="${jar.finalName}" default-value="${project.build.finalName}"
83 private String finalName;
86 * Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.
90 private String classifier;
95 protected MavenProjectHelper projectHelper;
100 * @parameter expression="${sonar.pluginKey}"
102 protected String pluginKey;
105 * @parameter expression="${sonar.pluginTermsConditionsUrl}"
107 private String pluginTermsConditionsUrl;
110 * Name of plugin class.
112 * @parameter expression="${sonar.pluginClass}"
115 private String pluginClass;
118 * @parameter expression="${sonar.pluginName}" default-value="${project.name}"
120 private String pluginName;
123 * @parameter default-value="${project.description}"
125 private String pluginDescription;
128 * @parameter default-value="${project.url}"
130 private String pluginUrl;
133 * @parameter default-value="${project.issueManagement.url}"
135 private String pluginIssueTrackerUrl;
141 private boolean useChildFirstClassLoader = false;
146 private String extendPlugin;
149 * @parameter expression="${sonar.skipDependenciesPackaging}"
151 private boolean skipDependenciesPackaging = false;
153 protected final MavenProject getProject() {
157 protected final File getOutputDirectory() {
158 return outputDirectory;
162 * @return the main classes directory, so it's used as the root of the jar.
164 protected final File getClassesDirectory() {
165 return classesDirectory;
168 public File getAppDirectory() {
172 protected final String getFinalName() {
176 protected final String getClassifier() {
180 public String getExplicitPluginKey() {
184 protected final String getPluginClass() {
188 protected final String getPluginName() {
192 protected final String getPluginDescription() {
193 return pluginDescription;
196 protected final String getPluginUrl() {
200 protected String getPluginTermsConditionsUrl() {
201 return pluginTermsConditionsUrl;
204 protected String getPluginIssueTrackerUrl() {
205 return pluginIssueTrackerUrl;
208 public boolean isUseChildFirstClassLoader() {
209 return useChildFirstClassLoader;
212 public String getExtendPlugin() {
216 protected boolean isSkipDependenciesPackaging() {
217 return skipDependenciesPackaging;
220 @SuppressWarnings({ "unchecked" })
221 protected Set<Artifact> getDependencyArtifacts() {
222 return getProject().getDependencyArtifacts();
225 protected Set<Artifact> getDependencyArtifacts(String scope) {
226 Set<Artifact> result = new HashSet<Artifact>();
227 for (Artifact dep : getDependencyArtifacts()) {
228 if (scope.equals(dep.getScope())) {
235 @SuppressWarnings({ "unchecked" })
236 protected Set<Artifact> getIncludedArtifacts() {
237 Set<Artifact> result = new HashSet<Artifact>();
238 Set<Artifact> artifacts = getProject().getArtifacts();
239 ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
240 for (Artifact artifact : artifacts) {
241 if (filter.include(artifact)) {
242 result.add(artifact);
248 protected final Artifact getSonarPluginApiArtifact() {
249 Set<Artifact> dependencies = getDependencyArtifacts();
250 if (dependencies != null) {
251 for (Artifact dep : dependencies) {
252 if (SONAR_GROUPID.equals(dep.getGroupId()) && SONAR_PLUGIN_API_ARTIFACTID.equals(dep.getArtifactId())
253 && SONAR_PLUGIN_API_TYPE.equals(dep.getType())) {
261 protected String getMessage(String title, List<String> ids) {
262 StringBuilder message = new StringBuilder();
263 message.append(title);
264 message.append("\n\n");
265 for (String id : ids) {
266 message.append("\t").append(id).append("\n");
268 return message.toString();