Skip to content

Commit

Permalink
MSONAR-166 Fix Mojo so it can correctly determine if it runs on the l…
Browse files Browse the repository at this point in the history
…ast project

* the counting mode used previously is very fragile depending on the
  number of calls made to the plugin.

  An example where it failed is if the plugin is only configured to run
  in the aggregator pom (so the counter never gets past 1).

  As the dependency graph is already computed by maven we can just
  take the last project in order and compare it to the current one.
  • Loading branch information
henryju committed Jun 18, 2018
1 parent 6ca2caf commit 5444fb6
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 13 deletions.
@@ -0,0 +1,16 @@
<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.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>module1</artifactId>
<name>Module1</name>


</project>

@@ -0,0 +1,16 @@
<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.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>module2</artifactId>
<name>Module2</name>


</project>

@@ -0,0 +1,10 @@
<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>
<groupId>org.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

</project>

@@ -0,0 +1,40 @@
<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.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>parent/pom.xml</relativePath>
</parent>
<artifactId>aggregator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>parent</module>
<module>module1</module>
<module>module2</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.maven.it.mojoVersion}</version>
<executions>
<execution>
<id>run-sonar</id>
<goals>
<goal>sonar</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

16 changes: 16 additions & 0 deletions its/projects/maven/aggregator-inherit-parent/module1/pom.xml
@@ -0,0 +1,16 @@
<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.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>module1</artifactId>
<name>Module1</name>


</project>

16 changes: 16 additions & 0 deletions its/projects/maven/aggregator-inherit-parent/module2/pom.xml
@@ -0,0 +1,16 @@
<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.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>module2</artifactId>
<name>Module2</name>


</project>

10 changes: 10 additions & 0 deletions its/projects/maven/aggregator-inherit-parent/parent/pom.xml
@@ -0,0 +1,10 @@
<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>
<groupId>org.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

</project>

22 changes: 22 additions & 0 deletions its/projects/maven/aggregator-inherit-parent/pom.xml
@@ -0,0 +1,22 @@
<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.sonarsource.maven.its</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>parent/pom.xml</relativePath>
</parent>
<artifactId>aggregator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>parent</module>
<module>module1</module>
<module>module2</module>
</modules>


</project>

19 changes: 18 additions & 1 deletion its/src/test/java/com/sonar/maven/it/suite/MavenTest.java
Expand Up @@ -116,16 +116,33 @@ public void structureWithRelativePaths() {
orchestrator.executeBuild(build);
}

@Test
/**
* See MSONAR-164
*/
@Test
public void flatStructure() {
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-flat-layout/parent"))
.setGoals(cleanSonarGoal());
orchestrator.executeBuild(build);
}

@Test
public void aggregatorInheritParent() {
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/aggregator-inherit-parent"))
.setGoals(cleanSonarGoal());
orchestrator.executeBuild(build);
assertThat(getMeasureAsInteger("org.sonarsource.maven.its:aggregator", "files")).isEqualTo(4); // 4 x pom.xml
}

@Test
public void aggregatorInheritParentAndSonarAttachedToPhase() {
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/aggregator-inherit-parent-and-bind-to-verify"))
.setGoals("clean verify")
.setProperty("sonar.maven.it.mojoVersion", mojoVersion().toString());
orchestrator.executeBuild(build);
assertThat(getMeasureAsInteger("org.sonarsource.maven.its:aggregator", "files")).isEqualTo(4); // 4 x pom.xml
}

@Test
public void shouldSupportJarWithoutSources() {
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/project-with-module-without-sources"))
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/org/sonarsource/scanner/maven/SonarQubeMojo.java
Expand Up @@ -19,9 +19,9 @@
*/
package org.sonarsource.scanner.maven;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -32,6 +32,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.ScanProperties;
Expand Down Expand Up @@ -74,11 +75,6 @@ public class SonarQubeMojo extends AbstractMojo {
@Parameter(defaultValue = "${mojoExecution}", required = true, readonly = true)
private MojoExecution mojoExecution;

/**
* Wait until reaching the last project before executing sonar when attached to phase
*/
static final AtomicInteger readyProjectsCounter = new AtomicInteger();

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (getLog().isDebugEnabled()) {
Expand Down Expand Up @@ -113,7 +109,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
* @return true if goal is attached to phase and not last in a multi-module project
*/
private boolean shouldDelayExecution() {
return !isDetachedGoal() && isLastProjectInReactor();
return !isDetachedGoal() && !isLastProjectInReactor();
}

/**
Expand All @@ -132,13 +128,21 @@ private boolean isDetachedGoal() {
/**
* Is this project the last project in the reactor?
*
* See <a href="http://svn.apache.org/viewvc/maven/plugins/tags/maven-install-plugin-2.5.2/src/main/java/org/apache/maven/plugin/install/InstallMojo.java?view=markup">
install plugin</a> for another example of using this technique.
*
* @return true if last project (including only project)
*/
private boolean isLastProjectInReactor() {
return readyProjectsCounter.incrementAndGet() != session.getProjects().size();
List<MavenProject> sortedProjects = session.getProjectDependencyGraph().getSortedProjects();

MavenProject lastProject = sortedProjects.isEmpty()
? session.getCurrentProject()
: sortedProjects.get( sortedProjects.size() - 1 );

if ( getLog().isDebugEnabled() ) {
getLog().debug( "Current project: '" + session.getCurrentProject().getName() +
"', Last project to execute based on dependency graph: '" + lastProject.getName() + "'" );
}

return session.getCurrentProject().equals( lastProject );
}

private boolean isSkip(Map<String, String> properties) {
Expand Down
Expand Up @@ -25,6 +25,9 @@
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.maven.execution.ProjectDependencyGraph;
import org.apache.maven.graph.GraphBuilder;
import org.apache.maven.model.building.Result;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.testing.MojoRule;
import org.assertj.core.data.MapEntry;
Expand All @@ -50,7 +53,6 @@ public class SonarQubeMojoTest {
private Log mockedLogger;

private SonarQubeMojo getMojo(File baseDir) throws Exception {
SonarQubeMojo.readyProjectsCounter.getAndSet(0);
return (SonarQubeMojo) mojoRule.lookupConfiguredMojo(baseDir, "sonar");
}

Expand Down Expand Up @@ -156,6 +158,10 @@ private File executeProject(String projectName, String... properties) throws Exc
File baseDir = new File("src/test/resources/org/sonarsource/scanner/maven/SonarQubeMojoTest/" + projectName);
SonarQubeMojo mojo = getMojo(baseDir);
mojo.getSession().getProjects().get(0).setExecutionRoot(true);
mojo.getSession().setAllProjects(mojo.getSession().getProjects());

Result<? extends ProjectDependencyGraph> result = mojoRule.lookup( GraphBuilder.class ).build( mojo.getSession() );
mojo.getSession().setProjectDependencyGraph( result.get() ); // done by maven in a normal execution

mojo.setLog(mockedLogger);

Expand Down

0 comments on commit 5444fb6

Please sign in to comment.