aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-06-19 15:00:30 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2013-06-19 15:00:30 +0200
commit2a52174013f4ab8407146888b0b0be2d35717d23 (patch)
treeed2697feb31219341a2c279bdc230280c46a2d19 /sonar-batch/src/main/java/org
parentdddfe00ef8ff5bceac64c8a8aeabed0ec3ccf35e (diff)
downloadsonarqube-2a52174013f4ab8407146888b0b0be2d35717d23.tar.gz
sonarqube-2a52174013f4ab8407146888b0b0be2d35717d23.zip
SONAR-4047 Move Maven specific classes to the new Maven Batch plugin
Diffstat (limited to 'sonar-batch/src/main/java/org')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/maven/AbstractMavenPluginExecutor.java76
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/maven/FakeMavenPluginExecutor.java6
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginExecutor.java4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java260
4 files changed, 6 insertions, 340 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/AbstractMavenPluginExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/AbstractMavenPluginExecutor.java
deleted file mode 100644
index f835c600ef3..00000000000
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/AbstractMavenPluginExecutor.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.scan.maven;
-
-import org.apache.maven.project.MavenProject;
-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.api.utils.TimeProfiler;
-import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
-
-/**
- * Abstract implementation of {@link org.sonar.batch.scan.maven.MavenPluginExecutor} to reduce duplications in concrete implementations for different Maven versions.
- */
-public abstract class AbstractMavenPluginExecutor implements MavenPluginExecutor {
-
- public final MavenPluginHandler execute(Project project, DefaultModuleFileSystem fs, MavenPluginHandler handler) {
- for (String goal : handler.getGoals()) {
- MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
- execute(project,
- fs,
- getGoal(handler.getGroupId(), handler.getArtifactId(), (plugin != null && plugin.getPlugin() != null ? plugin.getPlugin().getVersion() : null), goal));
- }
- return handler;
- }
-
- public final void execute(Project project, DefaultModuleFileSystem fs, String goal) {
- if (project.getPom() != null) {
- TimeProfiler profiler = new TimeProfiler().start("Execute " + goal);
- ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
- try {
- concreteExecute(project.getPom(), goal);
- } catch (Exception e) {
- throw new SonarException("Unable to execute maven plugin", e);
- } finally {
- // Reset original ClassLoader that may have been changed during Maven Execution (see SONAR-1800)
- Thread.currentThread().setContextClassLoader(currentClassLoader);
- profiler.stop();
- }
-
- MavenProjectConverter.synchronizeFileSystem(project.getPom(), fs);
- }
- }
-
- public abstract void concreteExecute(MavenProject pom, String goal) throws Exception;
-
- static String getGoal(String groupId, String artifactId, String version, String goal) {
- String defaultVersion = (version == null ? "" : version);
- return new StringBuilder()
- .append(groupId).append(":")
- .append(artifactId).append(":")
- .append(defaultVersion)
- .append(":")
- .append(goal)
- .toString();
- }
-
-}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/FakeMavenPluginExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/FakeMavenPluginExecutor.java
index 6de93f38130..df53510f444 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/FakeMavenPluginExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/FakeMavenPluginExecutor.java
@@ -22,15 +22,17 @@ package org.sonar.batch.scan.maven;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
-import org.sonar.batch.scan.maven.MavenPluginExecutor;
public final class FakeMavenPluginExecutor implements MavenPluginExecutor {
+
+ @Override
public void execute(Project project, DefaultModuleFileSystem fs, String goal) {
// do nothing
}
+ @Override
public MavenPluginHandler execute(Project project, DefaultModuleFileSystem fs, MavenPluginHandler handler) {
// do nothing
return handler;
}
-} \ No newline at end of file
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginExecutor.java
index 646f9c7ddbc..3ff7f0dabfa 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenPluginExecutor.java
@@ -19,12 +19,12 @@
*/
package org.sonar.batch.scan.maven;
-import org.sonar.api.BatchComponent;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
+import org.sonar.api.task.TaskExtension;
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
-public interface MavenPluginExecutor extends BatchComponent {
+public interface MavenPluginExecutor extends TaskExtension {
void execute(Project project, DefaultModuleFileSystem def, String goal);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java
deleted file mode 100644
index 41f0e8f72e1..00000000000
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/maven/MavenProjectConverter.java
+++ /dev/null
@@ -1,260 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2013 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.batch.scan.maven;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Predicate;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import org.apache.commons.lang.StringUtils;
-import org.apache.maven.model.CiManagement;
-import org.apache.maven.model.IssueManagement;
-import org.apache.maven.model.Scm;
-import org.apache.maven.project.MavenProject;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.bootstrap.ProjectDefinition;
-import org.sonar.api.batch.maven.MavenUtils;
-import org.sonar.api.utils.SonarException;
-import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
-import org.sonar.java.api.JavaUtils;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-
-public class MavenProjectConverter {
-
- private static final String UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE = "Unable to determine structure of project." +
- " Probably you use Maven Advanced Reactor Options, which is not supported by Sonar and should not be used.";
-
- private MavenProjectConverter() {
- // only static methods
- }
-
- public static ProjectDefinition convert(List<MavenProject> poms, MavenProject root) {
- ProjectDefinition def = ProjectDefinition.create();
- configure(def, poms, root);
- return def;
- }
-
- public static void configure(ProjectDefinition rootProjectDefinition, List<MavenProject> poms, MavenProject root) {
- // projects by canonical path to pom.xml
- Map<String, MavenProject> paths = Maps.newHashMap();
- Map<MavenProject, ProjectDefinition> defs = Maps.newHashMap();
-
- try {
- for (MavenProject pom : poms) {
- paths.put(pom.getFile().getCanonicalPath(), pom);
- ProjectDefinition def = pom == root ? rootProjectDefinition : ProjectDefinition.create();
- merge(pom, def);
- defs.put(pom, def);
- }
-
- for (Map.Entry<String, MavenProject> entry : paths.entrySet()) {
- MavenProject pom = entry.getValue();
- for (Object m : pom.getModules()) {
- String moduleId = (String) m;
- File modulePath = new File(pom.getBasedir(), moduleId);
- MavenProject module = findMavenProject(modulePath, paths);
-
- ProjectDefinition parentProject = defs.get(pom);
- if (parentProject == null) {
- throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
- }
- ProjectDefinition subProject = defs.get(module);
- if (subProject == null) {
- throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
- }
- parentProject.addSubProject(subProject);
- }
- }
- } catch (IOException e) {
- throw new SonarException(e);
- }
-
- ProjectDefinition rootProject = defs.get(root);
- if (rootProject == null) {
- throw new IllegalStateException(UNABLE_TO_DETERMINE_PROJECT_STRUCTURE_EXCEPTION_MESSAGE);
- }
- }
-
- private static MavenProject findMavenProject(final File modulePath, Map<String, MavenProject> paths) throws IOException {
- if (modulePath.exists() && modulePath.isDirectory()) {
- for (Map.Entry<String, MavenProject> entry : paths.entrySet()) {
- String pomFileParentDir = new File(entry.getKey()).getParent();
- if (pomFileParentDir.equals(modulePath.getCanonicalPath())) {
- return entry.getValue();
- }
- }
- return null;
- } else {
- return paths.get(modulePath.getCanonicalPath());
- }
- }
-
- @VisibleForTesting
- static void merge(MavenProject pom, ProjectDefinition definition) {
- String key = getSonarKey(pom);
- // IMPORTANT NOTE : reference on properties from POM model must not be saved,
- // instead they should be copied explicitly - see SONAR-2896
- definition
- .setProperties(pom.getModel().getProperties())
- .setKey(key)
- .setVersion(pom.getVersion())
- .setName(pom.getName())
- .setDescription(pom.getDescription())
- .addContainerExtension(pom);
- guessJavaVersion(pom, definition);
- guessEncoding(pom, definition);
- convertMavenLinksToProperties(definition, pom);
- synchronizeFileSystem(pom, definition);
- }
-
- public static String getSonarKey(MavenProject pom) {
- return new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString();
- }
-
- private static void guessEncoding(MavenProject pom, ProjectDefinition definition) {
- // See http://jira.codehaus.org/browse/SONAR-2151
- String encoding = MavenUtils.getSourceEncoding(pom);
- if (encoding != null) {
- definition.setProperty(CoreProperties.ENCODING_PROPERTY, encoding);
- }
- }
-
- private static void guessJavaVersion(MavenProject pom, ProjectDefinition definition) {
- // See http://jira.codehaus.org/browse/SONAR-2148
- // Get Java source and target versions from maven-compiler-plugin.
- String version = MavenUtils.getJavaSourceVersion(pom);
- if (version != null) {
- definition.setProperty(JavaUtils.JAVA_SOURCE_PROPERTY, version);
- }
- version = MavenUtils.getJavaVersion(pom);
- if (version != null) {
- definition.setProperty(JavaUtils.JAVA_TARGET_PROPERTY, version);
- }
- }
-
- /**
- * For SONAR-3676
- */
- private static void convertMavenLinksToProperties(ProjectDefinition definition, MavenProject pom) {
- setPropertyIfNotAlreadyExists(definition, CoreProperties.LINKS_HOME_PAGE, pom.getUrl());
-
- Scm scm = pom.getScm();
- if (scm == null) {
- scm = new Scm();
- }
- setPropertyIfNotAlreadyExists(definition, CoreProperties.LINKS_SOURCES, scm.getUrl());
- setPropertyIfNotAlreadyExists(definition, CoreProperties.LINKS_SOURCES_DEV, scm.getDeveloperConnection());
-
- CiManagement ci = pom.getCiManagement();
- if (ci == null) {
- ci = new CiManagement();
- }
- setPropertyIfNotAlreadyExists(definition, CoreProperties.LINKS_CI, ci.getUrl());
-
- IssueManagement issues = pom.getIssueManagement();
- if (issues == null) {
- issues = new IssueManagement();
- }
- setPropertyIfNotAlreadyExists(definition, CoreProperties.LINKS_ISSUE_TRACKER, issues.getUrl());
- }
-
- private static void setPropertyIfNotAlreadyExists(ProjectDefinition definition, String propertyKey, String propertyValue) {
- if (StringUtils.isBlank(definition.getProperties().getProperty(propertyKey))) {
- definition.setProperty(propertyKey, StringUtils.defaultString(propertyValue));
- }
- }
-
- public static void synchronizeFileSystem(MavenProject pom, ProjectDefinition into) {
- into.setBaseDir(pom.getBasedir());
- File buildDir = getBuildDir(pom);
- if (buildDir != null) {
- into.setBuildDir(buildDir);
- into.setWorkDir(getSonarWorkDir(pom));
- }
- List<String> filteredCompileSourceRoots = filterExisting(pom.getCompileSourceRoots(), pom.getBasedir());
- List<String> filteredTestCompileSourceRoots = filterExisting(pom.getTestCompileSourceRoots(), pom.getBasedir());
- into.setSourceDirs((String[]) filteredCompileSourceRoots.toArray(new String[filteredCompileSourceRoots.size()]));
- into.setTestDirs((String[]) filteredTestCompileSourceRoots.toArray(new String[filteredTestCompileSourceRoots.size()]));
- File binaryDir = resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir());
- if (binaryDir != null) {
- into.addBinaryDir(binaryDir);
- }
- }
-
- public static File getSonarWorkDir(MavenProject pom) {
- return new File(getBuildDir(pom), "sonar");
- }
-
- private static File getBuildDir(MavenProject pom) {
- return resolvePath(pom.getBuild().getDirectory(), pom.getBasedir());
- }
-
- private static List<String> filterExisting(List<String> filePaths, final File baseDir) {
- return Lists.newArrayList(Collections2.filter(filePaths, new Predicate<String>() {
- @Override
- public boolean apply(String filePath) {
- File file = resolvePath(filePath, baseDir);
- return file != null && file.exists();
- }
- }));
- }
-
- public static void synchronizeFileSystem(MavenProject pom, DefaultModuleFileSystem into) {
- into.resetDirs(
- pom.getBasedir(),
- getBuildDir(pom),
- resolvePaths((List<String>) pom.getCompileSourceRoots(), pom.getBasedir()),
- resolvePaths((List<String>) pom.getTestCompileSourceRoots(), pom.getBasedir()),
- Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir()))
- );
- }
-
- static File resolvePath(String path, File basedir) {
- if (path != null) {
- File file = new File(path);
- if (!file.isAbsolute()) {
- try {
- file = new File(basedir, path).getCanonicalFile();
- } catch (IOException e) {
- throw new SonarException("Unable to resolve path '" + path + "'", e);
- }
- }
- return file;
- }
- return null;
- }
-
- static List<File> resolvePaths(List<String> paths, File basedir) {
- List<File> result = Lists.newArrayList();
- for (String path : paths) {
- File dir = resolvePath(path, basedir);
- if (dir != null) {
- result.add(dir);
- }
- }
- return result;
- }
-}