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;
23 import java.util.HashSet;
24 import java.util.List;
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
29 import org.apache.maven.plugin.AbstractMojo;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.project.MavenProjectHelper;
32 import org.sonar.updatecenter.common.PluginKeyUtils;
35 * Base class for Sonar-plugin-packaging related tasks.
37 * @author Evgeny Mandrikov
39 public abstract class AbstractSonarPluginMojo extends AbstractMojo {
41 public static final String SONAR_GROUPID = "org.codehaus.sonar";
42 public static final String SONAR_PLUGIN_API_ARTIFACTID = "sonar-plugin-api";
43 public static final String SONAR_PLUGIN_API_TYPE = "jar";
48 * @parameter expression="${project}"
52 private MavenProject project;
55 * Directory containing the generated JAR.
57 * @parameter expression="${project.build.directory}"
60 private File outputDirectory;
63 * Directory containing the classes and resource files that should be packaged into the JAR.
65 * @parameter expression="${project.build.outputDirectory}"
68 private File classesDirectory;
71 * The directory where the app is built.
73 * @parameter expression="${project.build.directory}/${project.build.finalName}"
76 private File appDirectory;
79 * Name of the generated JAR.
81 * @parameter alias="jarName" expression="${jar.finalName}" default-value="${project.build.finalName}"
84 private String finalName;
87 * Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.
91 private String classifier;
96 protected MavenProjectHelper projectHelper;
101 * @parameter expression="${sonar.pluginKey}"
103 protected String pluginKey;
106 * @parameter expression="${sonar.pluginTermsConditionsUrl}"
108 private String pluginTermsConditionsUrl;
111 * Name of plugin class.
113 * @parameter expression="${sonar.pluginClass}"
116 private String pluginClass;
119 * @parameter expression="${sonar.pluginName}" default-value="${project.name}"
121 private String pluginName;
124 * @parameter default-value="${project.description}"
126 private String pluginDescription;
129 * @parameter default-value="${project.url}"
131 private String pluginUrl;
134 * @parameter default-value="${project.issueManagement.url}"
136 private String pluginIssueTrackerUrl;
142 private boolean useChildFirstClassLoader = false;
145 * @parameter expression="${sonar.skipDependenciesPackaging}"
147 private boolean skipDependenciesPackaging = false;
149 protected final MavenProject getProject() {
153 protected final File getOutputDirectory() {
154 return outputDirectory;
158 * @return the main classes directory, so it's used as the root of the jar.
160 protected final File getClassesDirectory() {
161 return classesDirectory;
164 public File getAppDirectory() {
168 protected final String getFinalName() {
172 protected final String getClassifier() {
176 public String getExplicitPluginKey() {
180 protected final String getPluginClass() {
184 protected final String getPluginName() {
188 protected final String getPluginDescription() {
189 return pluginDescription;
192 protected final String getPluginUrl() {
196 protected String getPluginTermsConditionsUrl() {
197 return pluginTermsConditionsUrl;
200 protected String getPluginIssueTrackerUrl() {
201 return pluginIssueTrackerUrl;
204 public boolean isUseChildFirstClassLoader() {
205 return useChildFirstClassLoader;
208 protected boolean isSkipDependenciesPackaging() {
209 return skipDependenciesPackaging;
212 @SuppressWarnings({ "unchecked" })
213 protected Set<Artifact> getDependencyArtifacts() {
214 return getProject().getDependencyArtifacts();
217 protected Set<Artifact> getDependencyArtifacts(String scope) {
218 Set<Artifact> result = new HashSet<Artifact>();
219 for (Artifact dep : getDependencyArtifacts()) {
220 if (scope.equals(dep.getScope())) {
227 @SuppressWarnings({ "unchecked" })
228 protected Set<Artifact> getIncludedArtifacts() {
229 Set<Artifact> result = new HashSet<Artifact>();
230 Set<Artifact> artifacts = getProject().getArtifacts();
231 ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
232 for (Artifact artifact : artifacts) {
233 if (filter.include(artifact)) {
234 result.add(artifact);
240 protected final Artifact getSonarPluginApiArtifact() {
241 Set<Artifact> dependencies = getDependencyArtifacts();
242 if (dependencies != null) {
243 for (Artifact dep : dependencies) {
244 if (SONAR_GROUPID.equals(dep.getGroupId()) && SONAR_PLUGIN_API_ARTIFACTID.equals(dep.getArtifactId())
245 && SONAR_PLUGIN_API_TYPE.equals(dep.getType())) {
253 protected String getMessage(String title, List<String> ids) {
254 StringBuilder message = new StringBuilder();
255 message.append(title);
256 message.append("\n\n");
257 for (String id : ids) {
258 message.append("\t").append(id).append("\n");
260 return message.toString();