break;
}
}
- return new ProjectReactor(mavenProjectConverter.configure(sortedProjects, topLevelProject));
+ if (topLevelProject != null && sortedProjects != null) {
+ return new ProjectReactor(mavenProjectConverter.configure(sortedProjects, topLevelProject));
+ }
+ else {
+ throw new IllegalStateException("Maven session is not in a good state. No top level project or empty reactor.");
+ }
}
}
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
import org.sonar.java.api.JavaUtils;
+import javax.annotation.Nullable;
+
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
Map<MavenProject, ProjectDefinition> defs = Maps.newHashMap();
try {
- for (MavenProject pom : poms) {
- paths.put(pom.getFile().getCanonicalPath(), pom);
- ProjectDefinition def = ProjectDefinition.create();
- merge(pom, def);
- defs.put(pom, def);
- }
+ configureModules(poms, paths, defs);
- 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);
- }
- }
+ rebuildModuleHierarchy(paths, defs);
} catch (IOException e) {
throw new SonarException(e);
}
return rootProject;
}
+ private void rebuildModuleHierarchy(Map<String, MavenProject> paths, Map<MavenProject, ProjectDefinition> defs) throws IOException {
+ 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);
+ }
+ }
+ }
+
+ private void configureModules(List<MavenProject> poms, Map<String, MavenProject> paths, Map<MavenProject, ProjectDefinition> defs) throws IOException {
+ for (MavenProject pom : poms) {
+ paths.put(pom.getFile().getCanonicalPath(), pom);
+ ProjectDefinition def = ProjectDefinition.create();
+ merge(pom, def);
+ defs.put(pom, def);
+ }
+ }
+
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()) {
getBuildDir(pom),
resolvePaths((List<String>) pom.getCompileSourceRoots(), pom.getBasedir()),
resolvePaths((List<String>) pom.getTestCompileSourceRoots(), pom.getBasedir()),
- Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir()))
- );
+ Arrays.asList(resolvePath(pom.getBuild().getOutputDirectory(), pom.getBasedir())));
}
- static File resolvePath(String path, File basedir) {
+ static File resolvePath(@Nullable String path, File basedir) {
if (path != null) {
File file = new File(path);
if (!file.isAbsolute()) {
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
import org.sonar.batch.scan.maven.MavenPluginExecutor;
+import javax.annotation.Nullable;
+
import java.lang.reflect.Method;
import java.util.Arrays;
@Override
public final MavenPluginHandler execute(Project project, DefaultModuleFileSystem fs, MavenPluginHandler handler) {
for (String goal : handler.getGoals()) {
+ if (goal == null) {
+ throw new IllegalStateException("Maven goal can't be null");
+ }
MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId());
execute(project,
fs,
}
}
- static String getGoal(String groupId, String artifactId, String version, String goal) {
+ static String getGoal(String groupId, String artifactId, @Nullable String version, String goal) {
String defaultVersion = (version == null ? "" : version);
return new StringBuilder()
.append(groupId).append(":")
.toString();
}
- public void concreteExecute(MavenProject pom, String goal) throws Exception {
+ public void concreteExecute(MavenProject pom, String goal) throws SecurityException {
Method executeMethod = null;
for (Method m : lifecycleExecutor.getClass().getMethods()) {
- if (m.getName().equals("execute")) {
+ if ("execute".equals(m.getName())) {
executeMethod = m;
break;
}
}
}
- public void concreteExecuteMaven2(Method executeMethod, MavenProject pom, String goal) throws Exception {
- ReactorManager reactor = new ReactorManager(Arrays.asList(pom));
- MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
- mavenSession.getSettings(),
- mavenSession.getLocalRepository(),
- mavenSession.getEventDispatcher(),
- reactor,
- Arrays.asList(goal),
- mavenSession.getExecutionRootDirectory(),
- mavenSession.getExecutionProperties(),
- mavenSession.getStartTime());
- executeMethod.invoke(lifecycleExecutor, clonedSession, reactor, clonedSession.getEventDispatcher());
+ public void concreteExecuteMaven2(Method executeMethod, MavenProject pom, String goal) {
+ try {
+ ReactorManager reactor = new ReactorManager(Arrays.asList(pom));
+ MavenSession clonedSession = new MavenSession(mavenSession.getContainer(),
+ mavenSession.getSettings(),
+ mavenSession.getLocalRepository(),
+ mavenSession.getEventDispatcher(),
+ reactor,
+ Arrays.asList(goal),
+ mavenSession.getExecutionRootDirectory(),
+ mavenSession.getExecutionProperties(),
+ mavenSession.getStartTime());
+ executeMethod.invoke(lifecycleExecutor, clonedSession, reactor, clonedSession.getEventDispatcher());
+ } catch (Exception e) {
+ throw new SonarException("Unable to execute Maven 2 plugin", e);
+ }
}
}
public class PhasesSumUpTimeProfiler implements ProjectAnalysisHandler, SensorExecutionHandler, DecoratorExecutionHandler, PostJobExecutionHandler, DecoratorsPhaseHandler,
SensorsPhaseHandler, PostJobsPhaseHandler, MavenPhaseHandler, InitializersPhaseHandler, InitializerExecutionHandler, BatchStepHandler {
- static Logger LOG = LoggerFactory.getLogger(PhasesSumUpTimeProfiler.class);
+ final static Logger LOG = LoggerFactory.getLogger(PhasesSumUpTimeProfiler.class);
private static final int TEXT_RIGHT_PAD = 60;
private static final int TIME_LEFT_PAD = 10;
import org.sonar.api.batch.events.SensorsPhaseHandler.SensorsPhaseEvent;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
-import org.sonar.batch.events.BatchStepHandler;
+import org.sonar.batch.events.BatchStepEvent;
import org.sonar.batch.phases.Phases.Phase;
import java.util.Arrays;
private void batchStep(PhasesSumUpTimeProfiler profiler) throws InterruptedException {
// Start of batch step
- profiler.onBatchStep(batchStepEvent(true, "Free memory"));
+ profiler.onBatchStep(new BatchStepEvent("Free memory", true));
clock.sleep(9);
// End of batch step
- profiler.onBatchStep(batchStepEvent(false, "Free memory"));
+ profiler.onBatchStep(new BatchStepEvent("Free memory", false));
}
private void mavenPhase(PhasesSumUpTimeProfiler profiler) throws InterruptedException {
};
}
- private BatchStepHandler.BatchStepEvent batchStepEvent(final boolean start, final String stepName) {
- return new BatchStepHandler.BatchStepEvent() {
-
- @Override
- public boolean isStart() {
- return start;
- }
-
- @Override
- public boolean isEnd() {
- return !start;
- }
-
- @Override
- public String stepName() {
- return stepName;
- }
- };
- }
-
private MavenPhaseHandler.MavenPhaseEvent mavenEvent(final boolean start) {
return new MavenPhaseHandler.MavenPhaseEvent() {
try {
file = new File(basedir, path).getCanonicalFile();
} catch (IOException e) {
- throw new RuntimeException("Unable to resolve path '" + path + "'", e);
+ throw new IllegalStateException("Unable to resolve path '" + path + "'", e);
}
}
return file;
import org.sonar.api.BatchExtension;
import org.sonar.api.resources.Project;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
/**
* @since 1.10
*/
*
* @return the group id
*/
+ @Nonnull
String getGroupId();
/**
*
* @return artifact id
*/
+ @Nonnull
String getArtifactId();
/**
*
* @return the plugin version
*/
+ @Nullable
String getVersion();
/**
*
* @return an array of goals
*/
+ @Nonnull
String[] getGoals();
/**