diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-11 19:59:09 +0300 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-01-18 15:11:11 +0300 |
commit | 231a993de184196c75be888dab2da539a600fac0 (patch) | |
tree | 22b7054e9317b8bd8077500e8613554ee3a337b4 | |
parent | a88512126dc63e77c19c62a495b984379a01f020 (diff) | |
download | sonarqube-231a993de184196c75be888dab2da539a600fac0.tar.gz sonarqube-231a993de184196c75be888dab2da539a600fac0.zip |
SONAR-1514: Add support for a new Ant task to analyze projects
* Add new environment - Ant.
* Introduce new component (Reactor) in order to break dependency on MavenSession.
* Add annotation SupportedEnvironment and corresponding check to BatchPluginRepository.
To allow specify in which environments BatchExtension should be active.
* Use this new annotation to disable MavenDependenciesSensor in Ant environment.
11 files changed, 162 insertions, 26 deletions
diff --git a/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java b/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java index aee592ad97f..3bb6f053dd2 100644 --- a/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java +++ b/plugins/sonar-design-plugin/src/main/java/org/sonar/plugins/design/batch/MavenDependenciesSensor.java @@ -34,6 +34,7 @@ import org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeV import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor; import org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor; import org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor; +import org.sonar.api.batch.SupportedEnvironment; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.SonarIndex; @@ -43,6 +44,7 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.utils.SonarException; +@SupportedEnvironment({ "maven2", "maven3" }) public class MavenDependenciesSensor implements Sensor { private ArtifactRepository localRepository; @@ -100,7 +102,7 @@ public class MavenDependenciesSensor implements Sensor { } protected void saveDependency(DependencyNode node, SensorContext context) { - Resource from = (node.getParent().getParent()==null) ? index.getProject() : toResource(node.getParent().getArtifact(), context); + Resource from = (node.getParent().getParent() == null) ? index.getProject() : toResource(node.getParent().getArtifact(), context); Resource to = toResource(node.getArtifact(), context); Dependency dependency = new Dependency(from, to); dependency.setUsage(node.getArtifact().getScope()); diff --git a/sonar-application/pom.xml b/sonar-application/pom.xml index c5d8fb844bc..9bb17b40874 100644 --- a/sonar-application/pom.xml +++ b/sonar-application/pom.xml @@ -150,12 +150,14 @@ <version>${project.version}</version> <scope>runtime</scope> </dependency> + <!-- <dependency> <groupId>org.codehaus.sonar.plugins</groupId> <artifactId>sonar-design-plugin</artifactId> <version>${project.version}</version> <scope>runtime</scope> </dependency> + --> <dependency> <groupId>org.codehaus.sonar.plugins</groupId> <artifactId>sonar-googleanalytics-plugin</artifactId> diff --git a/sonar-batch/src/main/java/org/sonar/batch/MavenReactor.java b/sonar-batch/src/main/java/org/sonar/batch/MavenReactor.java new file mode 100644 index 00000000000..a33e6889ec8 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/MavenReactor.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch; + +import org.apache.maven.execution.MavenSession; + +public class MavenReactor extends Reactor { + + public MavenReactor(MavenSession mavenSession) { + super(mavenSession.getSortedProjects()); + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java index e586106b310..1f969befd10 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java +++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectTree.java @@ -21,7 +21,6 @@ package org.sonar.batch; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.slf4j.LoggerFactory; import org.sonar.api.database.DatabaseSession; @@ -37,8 +36,8 @@ public class ProjectTree { private List<MavenProject> poms; private MavenProjectBuilder projectBuilder; - public ProjectTree(MavenSession mavenSession, DatabaseSession databaseSession) { - this.poms = mavenSession.getSortedProjects(); + public ProjectTree(Reactor reactor, DatabaseSession databaseSession) { + this.poms = reactor.getSortedProjects(); this.projectBuilder = new MavenProjectBuilder(databaseSession); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/Reactor.java b/sonar-batch/src/main/java/org/sonar/batch/Reactor.java new file mode 100644 index 00000000000..7dd900b7e4c --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/Reactor.java @@ -0,0 +1,38 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch; + +import org.apache.maven.project.MavenProject; + +import java.util.List; + +public class Reactor { + + private List<MavenProject> sortedProjects; + + public Reactor(List<MavenProject> sortedProjects) { + this.sortedProjects = sortedProjects; + } + + public List<MavenProject> getSortedProjects() { + return sortedProjects; + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java index 6588cfc0f70..7a42b11fe8b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java @@ -30,8 +30,11 @@ import org.slf4j.LoggerFactory; import org.sonar.api.BatchExtension; import org.sonar.api.Plugin; import org.sonar.api.batch.AbstractCoverageExtension; +import org.sonar.api.batch.SupportedEnvironment; +import org.sonar.api.platform.Environment; import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; +import org.sonar.api.utils.AnnotationUtils; import org.sonar.api.utils.SonarException; import org.sonar.core.classloaders.ClassLoadersCollection; import org.sonar.core.plugin.AbstractPluginRepository; @@ -52,10 +55,12 @@ public class BatchPluginRepository extends AbstractPluginRepository { private ClassLoadersCollection classLoaders; private ExtensionDownloader extensionDownloader; + private Environment environment; - public BatchPluginRepository(JpaPluginDao dao, ExtensionDownloader extensionDownloader) { + public BatchPluginRepository(JpaPluginDao dao, ExtensionDownloader extensionDownloader, Environment environment) { this.dao = dao; this.extensionDownloader = extensionDownloader; + this.environment = environment; } /** @@ -74,7 +79,7 @@ public class BatchPluginRepository extends AbstractPluginRepository { File file = extensionDownloader.downloadExtension(pluginFile); try { urls.add(file.toURI().toURL()); - + } catch (MalformedURLException e) { throw new SonarException("Can not get the URL of: " + file, e); } @@ -107,15 +112,33 @@ public class BatchPluginRepository extends AbstractPluginRepository { @Override protected boolean shouldRegisterExtension(PicoContainer container, String pluginKey, Object extension) { boolean ok = isType(extension, BatchExtension.class); + if (ok && !isSupportsEnvironment(extension)) { + ok = false; + LOG.debug("The following extension is ignored: " + extension + " due to execution environment."); + } if (ok && isType(extension, AbstractCoverageExtension.class)) { ok = shouldRegisterCoverageExtension(pluginKey, container.getComponent(Project.class), container.getComponent(Configuration.class)); - if ( !ok) { + if (!ok) { LOG.debug("The following extension is ignored: " + extension + ". See the parameter " + AbstractCoverageExtension.PARAM_PLUGIN); } } return ok; } + private boolean isSupportsEnvironment(Object extension) { + Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); + SupportedEnvironment env = AnnotationUtils.getClassAnnotation(clazz, SupportedEnvironment.class); + if (env == null) { + return true; + } + for (String supported : env.value()) { + if (StringUtils.equalsIgnoreCase(environment.toString(), supported)) { + return true; + } + } + return false; + } + boolean shouldRegisterCoverageExtension(String pluginKey, Project project, Configuration conf) { boolean ok = true; if (StringUtils.equals(project.getLanguageKey(), Java.KEY)) { diff --git a/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/BatchMojo.java b/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/BatchMojo.java index 8916b47e66a..e2f6e30d0dc 100644 --- a/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/BatchMojo.java +++ b/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/BatchMojo.java @@ -19,6 +19,8 @@ */ package org.sonar.maven2; +import org.sonar.batch.MavenReactor; + import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; @@ -135,7 +137,8 @@ public final class BatchMojo extends AbstractMojo { private void executeBatch() throws MojoExecutionException { - Batch batch = new Batch(getInitialConfiguration(), session, project, + MavenReactor reactor = new MavenReactor(session); + Batch batch = new Batch(getInitialConfiguration(), reactor, session, project, getLog(), lifecycleExecutor, pluginManager, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder, projectBuilder, Environment.MAVEN2, Maven2PluginExecutor.class); diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java index 516b2500c3b..51dd2a82d0e 100644 --- a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java +++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java @@ -40,6 +40,7 @@ import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; import org.slf4j.LoggerFactory; import org.sonar.api.platform.Environment; import org.sonar.batch.Batch; +import org.sonar.batch.MavenReactor; import java.io.InputStream; @@ -78,7 +79,7 @@ public final class SonarMojo extends AbstractMojo { /** * The artifact factory to use. - * + * * @component * @required * @readonly @@ -87,7 +88,7 @@ public final class SonarMojo extends AbstractMojo { /** * The artifact repository to use. - * + * * @parameter expression="${localRepository}" * @required * @readonly @@ -96,7 +97,7 @@ public final class SonarMojo extends AbstractMojo { /** * The artifact metadata source to use. - * + * * @component * @required * @readonly @@ -105,7 +106,7 @@ public final class SonarMojo extends AbstractMojo { /** * The artifact collector to use. - * + * * @component * @required * @readonly @@ -114,7 +115,7 @@ public final class SonarMojo extends AbstractMojo { /** * The dependency tree builder to use. - * + * * @component * @required * @readonly @@ -133,16 +134,15 @@ public final class SonarMojo extends AbstractMojo { executeBatch(); } - private void executeBatch() throws MojoExecutionException { + MavenReactor reactor = new MavenReactor(session); Batch batch = new Batch(getInitialConfiguration(), - session, project, getLog(), lifecycleExecutor, pluginManager, artifactFactory, + reactor, session, project, getLog(), lifecycleExecutor, pluginManager, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder, projectBuilder, Environment.MAVEN2, Maven2PluginExecutor.class); batch.execute(); } - private void initLogging() throws MojoExecutionException { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator jc = new JoranConfigurator(); diff --git a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java b/sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java index c8e65ab678e..8c9b821722a 100644 --- a/sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java +++ b/sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java @@ -39,6 +39,7 @@ import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; import org.slf4j.LoggerFactory; import org.sonar.api.platform.Environment; import org.sonar.batch.Batch; +import org.sonar.batch.MavenReactor; import java.io.InputStream; @@ -69,10 +70,9 @@ public final class SonarMojo extends AbstractMojo { */ private LifecycleExecutor lifecycleExecutor; - /** * The artifact factory to use. - * + * * @component * @required * @readonly @@ -81,7 +81,7 @@ public final class SonarMojo extends AbstractMojo { /** * The artifact repository to use. - * + * * @parameter expression="${localRepository}" * @required * @readonly @@ -90,7 +90,7 @@ public final class SonarMojo extends AbstractMojo { /** * The artifact metadata source to use. - * + * * @component * @required * @readonly @@ -99,7 +99,7 @@ public final class SonarMojo extends AbstractMojo { /** * The artifact collector to use. - * + * * @component * @required * @readonly @@ -108,7 +108,7 @@ public final class SonarMojo extends AbstractMojo { /** * The dependency tree builder to use. - * + * * @component * @required * @readonly @@ -127,16 +127,15 @@ public final class SonarMojo extends AbstractMojo { executeBatch(); } - private void executeBatch() throws MojoExecutionException { + MavenReactor reactor = new MavenReactor(session); Batch batch = new Batch(getInitialConfiguration(), - session, project, getLog(), lifecycleExecutor, artifactFactory, + reactor, session, project, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder, projectBuilder, Environment.MAVEN3, Maven3PluginExecutor.class); batch.execute(); } - private void initLogging() throws MojoExecutionException { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator jc = new JoranConfigurator(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SupportedEnvironment.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SupportedEnvironment.java new file mode 100644 index 00000000000..4c62a97d5d2 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SupportedEnvironment.java @@ -0,0 +1,40 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.batch; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation allows to specify in which environments {@link org.sonar.api.BatchExtension} would be active. + * Consult to {@link org.sonar.api.platform.Environment} to find possible values, for example - "maven2". + * We strictly recommend you to not overuse this annotation - most preferable is to design extensions to work in all environments. + * + * @since 2.6 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +public @interface SupportedEnvironment { + + String[] value(); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/platform/Environment.java b/sonar-plugin-api/src/main/java/org/sonar/api/platform/Environment.java index b9dd664f72c..6279c473ed3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/platform/Environment.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/platform/Environment.java @@ -30,7 +30,7 @@ public enum Environment implements BatchComponent, ServerComponent { /* * When will GRADLE, ANT, ECLIPSE, INTELLIJ_IDEA be added to this list ? :-) */ - SERVER, MAVEN3, MAVEN2; + SERVER, MAVEN3, MAVEN2, ANT; public boolean isServer() { return this==SERVER; |