aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core-maven-plugin
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-06 14:08:06 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-06 14:08:06 +0000
commitaeadc1f9129274949daaa57738c7c4550bdfbc7b (patch)
tree08dadf5ef7474fc41d1d48f74648f1ba8b55f34d /sonar-core-maven-plugin
downloadsonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.tar.gz
sonarqube-aeadc1f9129274949daaa57738c7c4550bdfbc7b.zip
SONAR-236 remove deprecated code from checkstyle plugin + display default value of rule parameters in Q profile console
Diffstat (limited to 'sonar-core-maven-plugin')
-rw-r--r--sonar-core-maven-plugin/pom.xml102
-rw-r--r--sonar-core-maven-plugin/src/main/java/org/sonar/maven2/BatchMojo.java170
-rw-r--r--sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java81
-rw-r--r--sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java62
-rw-r--r--sonar-core-maven-plugin/src/test/resources/logback-text.xml23
5 files changed, 438 insertions, 0 deletions
diff --git a/sonar-core-maven-plugin/pom.xml b/sonar-core-maven-plugin/pom.xml
new file mode 100644
index 00000000000..af6706ef6b2
--- /dev/null
+++ b/sonar-core-maven-plugin/pom.xml
@@ -0,0 +1,102 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar</artifactId>
+ <version>2.3-SNAPSHOT</version>
+ </parent>
+
+ <!-- do not change, it's used when deploying in sonar internal repository -->
+ <groupId>org.codehaus.sonar.runtime</groupId>
+ <artifactId>sonar-core-maven-plugin</artifactId>
+ <packaging>maven-plugin</packaging>
+ <name>Sonar :: Deprecated Maven2 Plugin</name>
+ <dependencies>
+
+ <!--
+
+
+
+
+
+
+
+
+ IMPORTANT
+ the following must be copied in
+ sonar-server/src/main/resources/org/sonar/server/mavendeployer/sonar-core-mojo-pom.template
+
+
+
+
+
+
+
+
+ -->
+
+ <dependency>
+ <groupId>org.apache.maven.shared</groupId>
+ <artifactId>maven-dependency-tree</artifactId>
+ <version>1.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-plugin-api</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-batch</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0.7</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-core</artifactId>
+ <version>2.0.7</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0.7</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.sonar</groupId>
+ <artifactId>sonar-plugin-api</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-deploy-plugin</artifactId>
+ <configuration>
+ <skip>true</skip>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project> \ No newline at end of file
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
new file mode 100644
index 00000000000..8916b47e66a
--- /dev/null
+++ b/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/BatchMojo.java
@@ -0,0 +1,170 @@
+/*
+ * 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.maven2;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
+import org.apache.commons.configuration.*;
+import org.apache.commons.io.IOUtils;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.resolver.ArtifactCollector;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.lifecycle.LifecycleExecutor;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.PluginManager;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectBuilder;
+import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.platform.Environment;
+import org.sonar.batch.Batch;
+
+import java.io.InputStream;
+
+/**
+ * @goal internal
+ * @aggregator
+ * @requiresDependencyResolution test
+ */
+public final class BatchMojo extends AbstractMojo {
+
+ /**
+ * @parameter expression="${session}"
+ * @required
+ * @readonly
+ */
+ private MavenSession session;
+
+ /**
+ * @parameter expression="${project}"
+ * @required
+ * @readonly
+ */
+ private MavenProject project;
+
+ /**
+ * @component
+ * @required
+ */
+ private LifecycleExecutor lifecycleExecutor;
+
+ /**
+ * @component
+ * @required
+ */
+ private PluginManager pluginManager;
+
+ /**
+ * The artifact factory to use.
+ *
+ * @component
+ * @required
+ * @readonly
+ */
+ private ArtifactFactory artifactFactory;
+
+ /**
+ * The artifact repository to use.
+ *
+ * @parameter expression="${localRepository}"
+ * @required
+ * @readonly
+ */
+ private ArtifactRepository localRepository;
+
+ /**
+ * The artifact metadata source to use.
+ *
+ * @component
+ * @required
+ * @readonly
+ */
+ private ArtifactMetadataSource artifactMetadataSource;
+
+ /**
+ * The artifact collector to use.
+ *
+ * @component
+ * @required
+ * @readonly
+ */
+ private ArtifactCollector artifactCollector;
+
+ /**
+ * The dependency tree builder to use.
+ *
+ * @component
+ * @required
+ * @readonly
+ */
+ private DependencyTreeBuilder dependencyTreeBuilder;
+
+ /**
+ * @component
+ * @required
+ * @readonly
+ */
+ private MavenProjectBuilder projectBuilder;
+
+ public void execute() throws MojoExecutionException, MojoFailureException {
+ initLogging();
+ executeBatch();
+ }
+
+
+ private void executeBatch() throws MojoExecutionException {
+ Batch batch = new Batch(getInitialConfiguration(), session, project,
+ getLog(), lifecycleExecutor, pluginManager, artifactFactory,
+ localRepository, artifactMetadataSource, artifactCollector,
+ dependencyTreeBuilder, projectBuilder, Environment.MAVEN2, Maven2PluginExecutor.class);
+ batch.execute();
+ }
+
+ private Configuration getInitialConfiguration() {
+ CompositeConfiguration configuration = new CompositeConfiguration();
+ configuration.addConfiguration(new SystemConfiguration());
+ configuration.addConfiguration(new EnvironmentConfiguration());
+ configuration.addConfiguration(new MapConfiguration(project.getModel().getProperties()));
+ return configuration;
+ }
+
+ private void initLogging() throws MojoExecutionException {
+ LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
+ JoranConfigurator jc = new JoranConfigurator();
+ jc.setContext(context);
+ context.reset();
+ InputStream input = getClass().getResourceAsStream("/org/sonar/batch/logback.xml");
+ System.setProperty("ROOT_LOGGER_LEVEL", getLog().isDebugEnabled() ? "DEBUG" : "INFO");
+ try {
+ jc.doConfigure(input);
+
+ } catch (JoranException e) {
+ throw new MojoExecutionException("can not initialize logging", e);
+
+ } finally {
+ IOUtils.closeQuietly(input);
+ }
+ }
+}
diff --git a/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java b/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java
new file mode 100644
index 00000000000..858f2e52a95
--- /dev/null
+++ b/sonar-core-maven-plugin/src/main/java/org/sonar/maven2/Maven2PluginExecutor.java
@@ -0,0 +1,81 @@
+/*
+ * 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.maven2;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.execution.ReactorManager;
+import org.apache.maven.lifecycle.LifecycleExecutor;
+import org.sonar.api.batch.maven.MavenPlugin;
+import org.sonar.api.batch.maven.MavenPluginHandler;
+import org.sonar.api.resources.Project;
+import org.sonar.api.utils.SonarException;
+import org.sonar.batch.MavenPluginExecutor;
+
+import java.util.Arrays;
+
+public class Maven2PluginExecutor implements MavenPluginExecutor {
+
+ private LifecycleExecutor lifecycleExecutor;
+ private MavenSession mavenSession;
+
+ public Maven2PluginExecutor(LifecycleExecutor le, MavenSession mavenSession) {
+ this.lifecycleExecutor = le;
+ this.mavenSession = mavenSession;
+ }
+
+ public MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
+ for (String goal : handler.getGoals()) {
+ MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
+ execute(project, getGoal(handler.getGroupId(), handler.getArtifactId(), plugin.getPlugin().getVersion(), goal));
+ }
+ return handler;
+ }
+
+ public void execute(Project project, String goal) {
+ try {
+ ReactorManager reactor = new ReactorManager(Arrays.asList(project.getPom()));
+ MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
+ mavenSession.getSettings(),
+ mavenSession.getLocalRepository(),
+ mavenSession.getEventDispatcher(),
+ reactor,
+ Arrays.asList(goal),
+ mavenSession.getExecutionRootDirectory(),
+ mavenSession.getExecutionProperties(),
+ mavenSession.getStartTime()
+ );
+ lifecycleExecutor.execute(clonedSession, reactor, clonedSession.getEventDispatcher());
+
+ } catch (Exception e) {
+ throw new SonarException("Unable to execute maven plugin", e);
+
+ }
+ }
+
+ protected static String getGoal(String groupId, String artifactId, String version, String goal) {
+ return new StringBuilder()
+ .append(groupId).append(":")
+ .append(artifactId).append(":")
+ .append(StringUtils.defaultString(version, "")).append(":")
+ .append(goal)
+ .toString();
+ }
+}
diff --git a/sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java b/sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java
new file mode 100644
index 00000000000..f59f98df3f9
--- /dev/null
+++ b/sonar-core-maven-plugin/src/test/java/org/sonar/maven2/Maven2PluginExecutorTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.maven2;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import org.junit.Test;
+import org.sonar.api.batch.maven.MavenPlugin;
+import org.sonar.api.batch.maven.MavenPluginHandler;
+import org.sonar.api.resources.Project;
+
+public class Maven2PluginExecutorTest {
+
+ @Test
+ public void pluginVersionIsOptional() {
+ assertThat(Maven2PluginExecutor.getGoal("group", "artifact", null, "goal"), is("group:artifact::goal"));
+ }
+
+ static class FakeCheckstyleMavenPluginHandler implements MavenPluginHandler {
+
+ public String getGroupId() {
+ return "org.apache.maven.plugins";
+ }
+
+ public String getArtifactId() {
+ return "maven-checkstyle-plugin";
+ }
+
+ public String getVersion() {
+ return "2.2";
+ }
+
+ public boolean isFixedVersion() {
+ return false;
+ }
+
+ public String[] getGoals() {
+ return new String[]{"checkstyle"};
+ }
+
+ public void configure(Project project, MavenPlugin plugin) {
+
+ }
+ }
+}
diff --git a/sonar-core-maven-plugin/src/test/resources/logback-text.xml b/sonar-core-maven-plugin/src/test/resources/logback-text.xml
new file mode 100644
index 00000000000..e9e78e5c318
--- /dev/null
+++ b/sonar-core-maven-plugin/src/test/resources/logback-text.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<configuration>
+
+ <appender name="STDOUT"
+ class="ch.qos.logback.core.ConsoleAppender">
+ <layout class="ch.qos.logback.classic.PatternLayout">
+ <pattern>
+ %d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
+ </pattern>
+ </layout>
+ </appender>
+
+ <logger name="org.sonar.DBSTATISTICS">
+ <level value="INFO"/>
+ </logger>
+
+ <root>
+ <level value="WARN"/>
+ <appender-ref ref="STDOUT"/>
+ </root>
+
+</configuration> \ No newline at end of file