aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2011-06-06 14:27:49 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2011-06-06 14:28:04 +0200
commitd346d2313443339ded7b86bca7765a7ee850a8bb (patch)
tree9ee28f020559465a003f952ed538d3a6ddd68802 /sonar-batch
parent00d12c9382a8c4161455e2203b9b074b15278111 (diff)
downloadsonarqube-d346d2313443339ded7b86bca7765a7ee850a8bb.tar.gz
sonarqube-d346d2313443339ded7b86bca7765a7ee850a8bb.zip
Allow the extension ProjectBuilder to change the source directories of root module
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java10
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java40
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/FakeMavenPluginExecutor.java5
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/MavenPluginExecutor.java3
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java24
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrapper/ProjectDefinition.java17
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java6
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java19
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java8
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java23
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java15
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java36
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/DefaultProjectFileSystem2Test.java47
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java5
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java2
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java2
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java6
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs/fake.txt1
18 files changed, 188 insertions, 81 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java
index 85e5614c0d7..2c4c0d12ed8 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/AbstractMavenPluginExecutor.java
@@ -20,6 +20,7 @@
package org.sonar.batch;
import org.apache.maven.project.MavenProject;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.MavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
@@ -31,11 +32,16 @@ import org.sonar.api.utils.TimeProfiler;
*/
public abstract class AbstractMavenPluginExecutor implements MavenPluginExecutor {
- public final MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
+ public final MavenPluginHandler execute(Project project, ProjectDefinition projectDefinition, 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));
+ execute(project, getGoal(handler.getGroupId(), handler.getArtifactId(), (plugin!=null && plugin.getPlugin()!=null ? plugin.getPlugin().getVersion() : null), goal));
}
+
+ if (project.getPom()!=null) {
+ MavenProjectConverter.synchronizeFileSystem(project.getPom(), projectDefinition);
+ }
+
return handler;
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java
index 632ee139c8c..941ff57269e 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultProjectFileSystem2.java
@@ -21,7 +21,6 @@ package org.sonar.batch;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.maven.project.MavenProject;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
@@ -32,7 +31,6 @@ import org.sonar.api.utils.SonarException;
import java.io.File;
import java.io.IOException;
-import java.util.Collections;
import java.util.List;
/**
@@ -57,11 +55,7 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
}
public File getBasedir() {
- if (pom != null) {
- return pom.getBasedir();
- } else {
- return def.getBaseDir();
- }
+ return def.getBaseDir();
}
public File getBuildDir() {
@@ -88,13 +82,7 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
@Override
public List<File> getSourceDirs() {
- List<File> unfiltered;
- if (pom != null) {
- // Maven can modify source directories during Sonar execution - see MavenPhaseExecutor.
- unfiltered = resolvePaths(pom.getCompileSourceRoots());
- } else {
- unfiltered = resolvePaths(def.getSourceDirs());
- }
+ List<File> unfiltered = resolvePaths(def.getSourceDirs());
return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS));
}
@@ -108,9 +96,8 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
}
if (pom != null) {
pom.getCompileSourceRoots().add(0, dir.getAbsolutePath());
- } else {
- def.addSourceDirs(dir.getAbsolutePath());
}
+ def.addSourceDirs(dir.getAbsolutePath());
return this;
}
@@ -119,13 +106,7 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
*/
@Override
public List<File> getTestDirs() {
- List<File> unfiltered;
- if (pom != null) {
- // Maven can modify test directories during Sonar execution - see MavenPhaseExecutor.
- unfiltered = resolvePaths(pom.getTestCompileSourceRoots());
- } else {
- unfiltered = resolvePaths(def.getTestDirs());
- }
+ List<File> unfiltered = resolvePaths(def.getTestDirs());
return ImmutableList.copyOf(Iterables.filter(unfiltered, DIRECTORY_EXISTS));
}
@@ -139,9 +120,8 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
}
if (pom != null) {
pom.getTestCompileSourceRoots().add(0, dir.getAbsolutePath());
- } else {
- def.addTestDirs(dir.getAbsolutePath());
}
+ def.addTestDirs(dir.getAbsolutePath());
return this;
}
@@ -158,18 +138,12 @@ public class DefaultProjectFileSystem2 extends DefaultProjectFileSystem {
@Override
public File getSonarWorkingDirectory() {
- File dir;
- if (pom != null) {
- dir = new File(getBuildDir(), "sonar");
- } else {
- dir = def.getWorkDir();
- }
try {
- FileUtils.forceMkdir(dir);
+ FileUtils.forceMkdir(def.getWorkDir());
+ return def.getWorkDir();
} catch (IOException e) {
throw new SonarException("Unable to retrieve Sonar working directory.", e);
}
- return dir;
}
@Override
diff --git a/sonar-batch/src/main/java/org/sonar/batch/FakeMavenPluginExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/FakeMavenPluginExecutor.java
index a10a74012b0..539d2962ec2 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/FakeMavenPluginExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/FakeMavenPluginExecutor.java
@@ -19,15 +19,16 @@
*/
package org.sonar.batch;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
-public class FakeMavenPluginExecutor implements MavenPluginExecutor {
+public final class FakeMavenPluginExecutor implements MavenPluginExecutor {
public void execute(Project project, String goal) {
// do nothing
}
- public MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
+ public MavenPluginHandler execute(Project project, ProjectDefinition projectDefinition, MavenPluginHandler handler) {
// do nothing
return handler;
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/MavenPluginExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/MavenPluginExecutor.java
index dd16c99d89e..891c9fd1c4b 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/MavenPluginExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/MavenPluginExecutor.java
@@ -20,6 +20,7 @@
package org.sonar.batch;
import org.sonar.api.BatchComponent;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
@@ -27,6 +28,6 @@ public interface MavenPluginExecutor extends BatchComponent {
void execute(Project project, String goal);
- MavenPluginHandler execute(Project project, MavenPluginHandler handler);
+ MavenPluginHandler execute(Project project, ProjectDefinition def, MavenPluginHandler handler);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java
index be0ac861cd7..ec3104f755c 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/MavenProjectConverter.java
@@ -66,11 +66,33 @@ public final class MavenProjectConverter {
*/
static ProjectDefinition convert(MavenProject pom) {
String key = new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString();
- return new ProjectDefinition(pom.getBasedir(), null, pom.getModel().getProperties()) // TODO work directory ?
+ ProjectDefinition definition = ProjectDefinition.create();
+ definition.setProperties(pom.getModel().getProperties())
.setKey(key)
.setVersion(pom.getVersion())
.setName(pom.getName())
.setDescription(pom.getDescription())
.addContainerExtension(pom);
+ synchronizeFileSystem(pom, definition);
+ return definition;
+ }
+
+ public static void synchronizeFileSystem(MavenProject pom, ProjectDefinition into) {
+ into.setBaseDir(pom.getBasedir());
+ into.setWorkDir(new File(resolvePath(pom.getBuild().getDirectory(), pom.getBasedir()), "sonar"));
+ into.setSourceDirs((String[]) pom.getCompileSourceRoots().toArray(new String[pom.getCompileSourceRoots().size()]));
+ into.setTestDirs((String[]) pom.getTestCompileSourceRoots().toArray(new String[pom.getTestCompileSourceRoots().size()]));
+ }
+
+ static File resolvePath(String path, File basedir) {
+ 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;
}
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/ProjectDefinition.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/ProjectDefinition.java
index f3fbe726c9f..0c59c799935 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/ProjectDefinition.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/ProjectDefinition.java
@@ -27,7 +27,7 @@ import java.util.Properties;
/**
* Describes project in a form suitable to bootstrap Sonar batch.
* We assume that project is just a set of configuration properties and directories.
- *
+ *
* @since 2.6
* @deprecated since 2.9. Move into org.sonar.api.batch.bootstrap
*/
@@ -38,11 +38,14 @@ public class ProjectDefinition {
private List<ProjectDefinition> children = new ArrayList<ProjectDefinition>();
/**
- * @param baseDir project base directory
+ * @param baseDir project base directory
* @param properties project properties
*/
public ProjectDefinition(File baseDir, File workDir, Properties properties) {
- target = new org.sonar.api.batch.bootstrap.ProjectDefinition(baseDir, workDir, properties);
+ target = org.sonar.api.batch.bootstrap.ProjectDefinition.create()
+ .setBaseDir(baseDir)
+ .setWorkDir(workDir)
+ .setProperties(properties);
}
public File getBaseDir() {
@@ -71,7 +74,7 @@ public class ProjectDefinition {
/**
* @param path path to directory with test sources.
- * It can be absolute or relative to project directory.
+ * It can be absolute or relative to project directory.
*/
public void addTestDir(String path) {
target.addTestDirs(path);
@@ -83,7 +86,7 @@ public class ProjectDefinition {
/**
* @param path path to directory with compiled source. In case of Java this is directory with class files.
- * It can be absolute or relative to project directory.
+ * It can be absolute or relative to project directory.
* @TODO currently Sonar supports only one such directory due to dependency on MavenProject
*/
public void addBinaryDir(String path) {
@@ -96,7 +99,7 @@ public class ProjectDefinition {
/**
* @param path path to file with third-party library. In case of Java this is path to jar file.
- * It can be absolute or relative to project directory.
+ * It can be absolute or relative to project directory.
*/
public void addLibrary(String path) {
target.addLibrary(path);
@@ -104,7 +107,7 @@ public class ProjectDefinition {
/**
* Adds an extension, which would be available in PicoContainer during analysis of this project.
- *
+ *
* @since 2.8
*/
public void addContainerExtension(Object extension) {
diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java
index 1605a1623d4..ef3738dda3f 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/phases/DecoratorsExecutor.java
@@ -40,14 +40,16 @@ public class DecoratorsExecutor implements BatchComponent {
private DecoratorsSelector decoratorsSelector;
private SonarIndex index;
private EventBus eventBus;
+ private Project project;
- public DecoratorsExecutor(BatchExtensionDictionnary extensionDictionnary, SonarIndex index, EventBus eventBus) {
+ public DecoratorsExecutor(BatchExtensionDictionnary extensionDictionnary, Project project, SonarIndex index, EventBus eventBus) {
this.decoratorsSelector = new DecoratorsSelector(extensionDictionnary);
this.index = index;
this.eventBus = eventBus;
+ this.project = project;
}
- public void execute(Project project) {
+ public void execute() {
Collection<Decorator> decorators = decoratorsSelector.select(project);
eventBus.fireEvent(new DecoratorsPhaseEvent(Lists.newArrayList(decorators), true));
decorateResource(project, decorators, true);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java
index e84d4527ede..c205df16034 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java
@@ -19,19 +19,20 @@
*/
package org.sonar.batch.phases;
-import java.util.Collection;
-
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.BatchExtensionDictionnary;
import org.sonar.api.batch.Initializer;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.batch.MavenPluginExecutor;
+import java.util.Collection;
+
public class InitializersExecutor {
private static final Logger logger = LoggerFactory.getLogger(SensorsExecutor.class);
@@ -39,19 +40,23 @@ public class InitializersExecutor {
private MavenPluginExecutor mavenExecutor;
private Collection<Initializer> initializers;
+ private ProjectDefinition projectDef;
+ private Project project;
- public InitializersExecutor(BatchExtensionDictionnary selector, Project project, MavenPluginExecutor mavenExecutor) {
+ public InitializersExecutor(BatchExtensionDictionnary selector, Project project, ProjectDefinition projectDef, MavenPluginExecutor mavenExecutor) {
this.initializers = selector.select(Initializer.class, project, true);
this.mavenExecutor = mavenExecutor;
+ this.project = project;
+ this.projectDef = projectDef;
}
- public void execute(Project project) {
+ public void execute() {
if (logger.isDebugEnabled()) {
logger.debug("Initializers : {}", StringUtils.join(initializers, " -> "));
}
for (Initializer initializer : initializers) {
- executeMavenPlugin(project, initializer);
+ executeMavenPlugin(initializer);
TimeProfiler profiler = new TimeProfiler(logger).start("Initializer " + initializer);
initializer.execute(project);
@@ -59,12 +64,12 @@ public class InitializersExecutor {
}
}
- private void executeMavenPlugin(Project project, Initializer sensor) {
+ private void executeMavenPlugin(Initializer sensor) {
if (sensor instanceof DependsUponMavenPlugin) {
MavenPluginHandler handler = ((DependsUponMavenPlugin) sensor).getMavenPluginHandler(project);
if (handler != null) {
TimeProfiler profiler = new TimeProfiler(logger).start("Execute maven plugin " + handler.getArtifactId());
- mavenExecutor.execute(project, handler);
+ mavenExecutor.execute(project, projectDef, handler);
profiler.stop();
}
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java
index 5ca5be54a8e..a9d69e72a23 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java
@@ -74,17 +74,17 @@ public final class Phases {
eventBus.fireEvent(new ProjectAnalysisEvent(project, true));
mavenPluginsConfigurator.execute(project);
mavenPhaseExecutor.execute(project);
- initializersExecutor.execute(project);
+ initializersExecutor.execute();
persistenceManager.setDelayedMode(true);
- sensorsExecutor.execute(project, sensorContext);
- decoratorsExecutor.execute(project);
+ sensorsExecutor.execute(sensorContext);
+ decoratorsExecutor.execute();
persistenceManager.dump();
persistenceManager.setDelayedMode(false);
if (project.isRoot()) {
updateStatusJob.execute();
- postJobsExecutor.execute(project, sensorContext);
+ postJobsExecutor.execute(sensorContext);
}
cleanMemory();
eventBus.fireEvent(new ProjectAnalysisEvent(project, false));
diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java
index 9c42b329378..3f6b6433cbc 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/phases/PostJobsExecutor.java
@@ -26,35 +26,40 @@ import org.sonar.api.BatchComponent;
import org.sonar.api.batch.BatchExtensionDictionnary;
import org.sonar.api.batch.PostJob;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
import org.sonar.batch.MavenPluginExecutor;
import java.util.Collection;
+import java.util.List;
public class PostJobsExecutor implements BatchComponent {
private static final Logger LOG = LoggerFactory.getLogger(PostJobsExecutor.class);
private Collection<PostJob> postJobs;
private MavenPluginExecutor mavenExecutor;
+ private ProjectDefinition projectDefinition;
+ private Project project;
- public PostJobsExecutor(Project project, BatchExtensionDictionnary selector, MavenPluginExecutor mavenExecutor) {
- postJobs = selector.select(PostJob.class, project, true);
- this.mavenExecutor = mavenExecutor;
+ public PostJobsExecutor(BatchExtensionDictionnary selector, Project project, ProjectDefinition projectDefinition, MavenPluginExecutor mavenExecutor) {
+ this(selector.select(PostJob.class, project, true), project, projectDefinition, mavenExecutor);
}
- protected PostJobsExecutor(Collection<PostJob> postJobs, MavenPluginExecutor mavenExecutor) {
- this.postJobs = postJobs;
+ PostJobsExecutor(Collection<PostJob> jobs, Project project, ProjectDefinition projectDefinition, MavenPluginExecutor mavenExecutor) {
+ this.postJobs = jobs;
this.mavenExecutor = mavenExecutor;
+ this.project = project;
+ this.projectDefinition = projectDefinition;
}
- public void execute(Project project, SensorContext context) {
+ public void execute(SensorContext context) {
logPostJobs();
for (PostJob postJob : postJobs) {
LOG.info("Executing post-job {}", postJob.getClass());
- executeMavenPlugin(project, postJob);
+ executeMavenPlugin(postJob);
postJob.executeOn(project, context);
}
}
@@ -65,11 +70,11 @@ public class PostJobsExecutor implements BatchComponent {
}
}
- private void executeMavenPlugin(Project project, PostJob job) {
+ private void executeMavenPlugin(PostJob job) {
if (job instanceof DependsUponMavenPlugin) {
MavenPluginHandler handler = ((DependsUponMavenPlugin) job).getMavenPluginHandler(project);
if (handler != null) {
- mavenExecutor.execute(project, handler);
+ mavenExecutor.execute(project, projectDefinition, handler);
}
}
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java
index 474f53be173..f59d9c5fe55 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/phases/SensorsExecutor.java
@@ -26,6 +26,7 @@ import org.sonar.api.BatchComponent;
import org.sonar.api.batch.BatchExtensionDictionnary;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
@@ -41,18 +42,22 @@ public class SensorsExecutor implements BatchComponent {
private Collection<Sensor> sensors;
private MavenPluginExecutor mavenExecutor;
private EventBus eventBus;
+ private Project project;
+ private ProjectDefinition projectDefinition;
- public SensorsExecutor(BatchExtensionDictionnary selector, Project project, MavenPluginExecutor mavenExecutor, EventBus eventBus) {
+ public SensorsExecutor(BatchExtensionDictionnary selector, Project project, ProjectDefinition projectDefinition, MavenPluginExecutor mavenExecutor, EventBus eventBus) {
this.sensors = selector.select(Sensor.class, project, true);
this.mavenExecutor = mavenExecutor;
this.eventBus = eventBus;
+ this.project = project;
+ this.projectDefinition = projectDefinition;
}
- public void execute(Project project, SensorContext context) {
+ public void execute(SensorContext context) {
eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), true));
for (Sensor sensor : sensors) {
- executeMavenPlugin(project, sensor);
+ executeMavenPlugin(sensor);
eventBus.fireEvent(new SensorExecutionEvent(sensor, true));
sensor.analyse(project, context);
@@ -62,12 +67,12 @@ public class SensorsExecutor implements BatchComponent {
eventBus.fireEvent(new SensorsPhaseEvent(Lists.newArrayList(sensors), false));
}
- private void executeMavenPlugin(Project project, Sensor sensor) {
+ private void executeMavenPlugin(Sensor sensor) {
if (sensor instanceof DependsUponMavenPlugin) {
MavenPluginHandler handler = ((DependsUponMavenPlugin) sensor).getMavenPluginHandler(project);
if (handler != null) {
TimeProfiler profiler = new TimeProfiler(LOG).start("Execute maven plugin " + handler.getArtifactId());
- mavenExecutor.execute(project, handler);
+ mavenExecutor.execute(project, projectDefinition, handler);
profiler.stop();
}
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java
index 687ccbbbc6b..eecec7e0113 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/AbstractMavenPluginExecutorTest.java
@@ -19,13 +19,18 @@
*/
package org.sonar.batch;
+import org.apache.maven.project.MavenProject;
import org.junit.Test;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.maven.MavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
import org.sonar.api.resources.Project;
+import java.io.File;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
public class AbstractMavenPluginExecutorTest {
@@ -34,13 +39,36 @@ public class AbstractMavenPluginExecutorTest {
assertThat(AbstractMavenPluginExecutor.getGoal("group", "artifact", null, "goal"), is("group:artifact::goal"));
}
- static class FakeCheckstyleMavenPluginHandler implements MavenPluginHandler {
+ /**
+ * The maven plugin sometimes changes the project structure (for example mvn build-helper:add-source). These changes
+ * must be applied to the internal structure.
+ */
+ @Test
+ public void shouldUpdateProjectAfterExecution() {
+ AbstractMavenPluginExecutor executor = new AbstractMavenPluginExecutor() {
+ @Override
+ public void concreteExecute(MavenProject pom, String goal) throws Exception {
+ pom.addCompileSourceRoot("src/java");
+ }
+ };
+ MavenProject pom = new MavenProject();
+ pom.setFile(new File("target/AbstractMavenPluginExecutorTest/pom.xml"));
+ pom.getBuild().setDirectory("target");
+ Project foo = new Project("foo");
+ foo.setPom(pom);
+ ProjectDefinition definition = ProjectDefinition.create();
+ executor.execute(foo, definition, new AddSourceMavenPluginHandler());
+
+ assertThat(definition.getSourceDirs(), hasItem("src/java"));
+ }
+
+ static class AddSourceMavenPluginHandler implements MavenPluginHandler {
public String getGroupId() {
- return "org.apache.maven.plugins";
+ return "fake";
}
public String getArtifactId() {
- return "maven-checkstyle-plugin";
+ return "fake";
}
public String getVersion() {
@@ -52,7 +80,7 @@ public class AbstractMavenPluginExecutorTest {
}
public String[] getGoals() {
- return new String[] { "checkstyle" };
+ return new String[] { "fake" };
}
public void configure(Project project, MavenPlugin plugin) {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultProjectFileSystem2Test.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultProjectFileSystem2Test.java
new file mode 100644
index 00000000000..7fd4cb02745
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultProjectFileSystem2Test.java
@@ -0,0 +1,47 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * 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.commons.io.FileUtils;
+import org.hamcrest.core.Is;
+import org.junit.Test;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.resources.Project;
+
+import java.io.File;
+
+import static org.junit.Assert.assertThat;
+import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
+
+public class DefaultProjectFileSystem2Test {
+ @Test
+ public void shouldIgnoreInexistingSourceDirs() {
+ File exists = FileUtils.toFile(getClass().getResource("/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs"));
+ File notExists = new File("target/unknown");
+
+ ProjectDefinition definition = ProjectDefinition.create().addSourceDirs(exists, notExists);
+
+ DefaultProjectFileSystem2 fs = new DefaultProjectFileSystem2(new Project("foo"), new Languages(), definition);
+ assertThat(fs.getSourceDirs().size(), Is.is(1));
+ assertThat(fs.getSourceDirs(), hasItem(exists));
+
+ }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java b/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java
index 279645cb3a2..7077ddde528 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/MavenProjectConverterTest.java
@@ -45,9 +45,11 @@ public class MavenProjectConverterTest {
public void shouldConvertModules() {
MavenProject root = new MavenProject();
root.setFile(new File("/foo/pom.xml"));
+ root.getBuild().setDirectory("target");
root.getModules().add("module");
MavenProject module = new MavenProject();
module.setFile(new File("/foo/module/pom.xml"));
+ module.getBuild().setDirectory("target");
ProjectDefinition project = MavenProjectConverter.convert(Arrays.asList(root, module), root);
assertThat(project.getSubProjects().size(), is(1));
@@ -61,6 +63,8 @@ public class MavenProjectConverterTest {
pom.setVersion("1.0.1");
pom.setName("Test");
pom.setDescription("just test");
+ pom.setFile(new File("/foo/pom.xml"));
+ pom.getBuild().setDirectory("target");
ProjectDefinition project = MavenProjectConverter.convert(pom);
Properties properties = project.getProperties();
@@ -130,6 +134,7 @@ public class MavenProjectConverterTest {
Model model = new MavenXpp3Reader().read(new StringReader(FileUtils.readFileToString(pomFile)));
MavenProject pom = new MavenProject(model);
pom.setFile(pomFile);
+ pom.getBuild().setDirectory("target");
pom.setExecutionRoot(isRoot);
return pom;
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java
index dd765b0e100..ff62f837bbd 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapModuleTest.java
@@ -35,7 +35,7 @@ public class BootstrapModuleTest {
public void execute(Project project, String goal) {
}
- public MavenPluginHandler execute(Project project, MavenPluginHandler handler) {
+ public MavenPluginHandler execute(Project project, ProjectDefinition projectDef, MavenPluginHandler handler) {
return handler;
}
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java
index fc86a6d4c27..181abf73081 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/phases/DecoratorsExecutorTest.java
@@ -66,7 +66,7 @@ public class DecoratorsExecutorTest {
Decorator decorator = mock(Decorator.class);
doThrow(new SonarException()).when(decorator).decorate(any(Resource.class), any(DecoratorContext.class));
- DecoratorsExecutor executor = new DecoratorsExecutor(mock(BatchExtensionDictionnary.class), mock(SonarIndex.class), mock(EventBus.class));
+ DecoratorsExecutor executor = new DecoratorsExecutor(mock(BatchExtensionDictionnary.class), new Project("key"), mock(SonarIndex.class), mock(EventBus.class));
try {
executor.executeDecorator(decorator, mock(DefaultDecoratorContext.class), new File("org/foo/Bar.java"));
fail("Exception has not been thrown");
diff --git a/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java
index 2231bea710a..60efff96509 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/phases/PostJobsExecutorTest.java
@@ -22,6 +22,7 @@ package org.sonar.batch.phases;
import org.junit.Test;
import org.sonar.api.batch.PostJob;
import org.sonar.api.batch.SensorContext;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.resources.Project;
import org.sonar.batch.MavenPluginExecutor;
@@ -39,10 +40,11 @@ public class PostJobsExecutorTest {
PostJob job2 = mock(PostJob.class);
List<PostJob> jobs = Arrays.asList(job1, job2);
- PostJobsExecutor executor = new PostJobsExecutor(jobs, mock(MavenPluginExecutor.class));
Project project = new Project("project");
+ ProjectDefinition projectDefinition = ProjectDefinition.create();
+ PostJobsExecutor executor = new PostJobsExecutor(jobs, project, projectDefinition, mock(MavenPluginExecutor.class));
SensorContext context = mock(SensorContext.class);
- executor.execute(project, context);
+ executor.execute(context);
verify(job1).executeOn(project, context);
verify(job2).executeOn(project, context);
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs/fake.txt b/sonar-batch/src/test/resources/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs/fake.txt
new file mode 100644
index 00000000000..f0f877cedcc
--- /dev/null
+++ b/sonar-batch/src/test/resources/org/sonar/batch/DefaultProjectFileSystem2Test/shouldIgnoreInexistingSourceDirs/fake.txt
@@ -0,0 +1 @@
+fake \ No newline at end of file