Skip to content

Commit

Permalink
New configuration that allow to build changed projects first
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Jul 17, 2019
1 parent a397fc4 commit 8836b59
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/it/projects/build-changed-first/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.name = TEST build changed first
invoker.goals = -e clean install -Dpartial.buildAll=true -Dpartial.buildChangedFirst=true
39 changes: 39 additions & 0 deletions src/it/projects/build-changed-first/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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>

<modules>
<module>child1</module>
<module>child2</module>
<module>child3</module>
<module>child4</module>
<module>child5</module>
<module>child6</module>
<module>child7</module>
</modules>

<groupId>parent</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>com.lesfurets</groupId>
<artifactId>partial-build-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
<configuration>
<enabled>true</enabled>
<uncommited>false</uncommited>
<referenceBranch>refs/heads/develop</referenceBranch>
<baseBranch>refs/heads/feature/1</baseBranch>
</configuration>
</plugin>
</plugins>
</build>

</project>
8 changes: 8 additions & 0 deletions src/it/projects/build-changed-first/setup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import com.lesfurets.maven.partial.mocks.ITHelper

def testProjectBasedir = basedir as File
def pbpBaseDir = sourceDir as String
def pbpVersion = projectVersion as String
new ITHelper(testProjectBasedir, pbpBaseDir, pbpVersion).setupTest()

return true
24 changes: 24 additions & 0 deletions src/it/projects/build-changed-first/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import org.codehaus.plexus.util.FileUtils

def file = new File(basedir, "build.log")
String buildLog = FileUtils.fileRead(file)

boolean verified = true
verified &= buildLog.contains("Sorting reactor so changed project are built first.")
verified &= buildLog.contains(" subchild2")
verified &= buildLog.contains(" child3")
verified &= buildLog.contains(" child4")
verified &= buildLog.contains(" subchild41")

verified &= buildLog.contains(" child1")
verified &= buildLog.contains(" child2")
verified &= buildLog.contains(" child5")
verified &= buildLog.contains(" child6")
verified &= buildLog.contains(" child7")
verified &= buildLog.contains(" subchild1")
verified &= buildLog.contains(" subchild42")
verified &= buildLog.contains(" parent")

verified &= buildLog.contains(" BUILD SUCCESS")

return verified;
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Configuration {
public final boolean makeUpstream;
public final boolean skipTestsForNotImpactedModules;
public final boolean buildAll;
public final boolean buildChangedFirst;
public final boolean compareToMergeBase;
public final boolean fetchBaseBranch;
public final boolean fetchReferenceBranch;
Expand Down Expand Up @@ -70,6 +71,7 @@ public Configuration(MavenSession session) throws IOException {
untracked = Boolean.valueOf(Property.untracked.getValue());
skipTestsForNotImpactedModules = Boolean.valueOf(Property.skipTestsForNotImpactedModules.getValue());
buildAll = Boolean.valueOf(Property.buildAll.getValue());
buildChangedFirst = Boolean.valueOf(Property.buildChangedFirst.getValue());
compareToMergeBase = Boolean.valueOf(Property.compareToMergeBase.getValue());
fetchReferenceBranch = Boolean.valueOf(Property.fetchReferenceBranch.getValue());
fetchBaseBranch = Boolean.valueOf(Property.fetchBaseBranch.getValue());
Expand Down Expand Up @@ -166,6 +168,7 @@ public String toString() {
.append("makeUpstream", makeUpstream)
.append("skipTestsForNotImpactedModules", skipTestsForNotImpactedModules)
.append("buildAll", buildAll)
.append("buildChangedFirst", buildChangedFirst)
.append("compareToMergeBase", compareToMergeBase)
.append("fetchBaseBranch", fetchBaseBranch)
.append("fetchReferenceBranch", fetchReferenceBranch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum Property {
untracked("false"),
skipTestsForNotImpactedModules("false"),
buildAll("false"),
buildChangedFirst("false"),
compareToMergeBase("true"),
fetchBaseBranch("false"),
fetchReferenceBranch("false"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -34,6 +35,30 @@ public void setUpSession(Collection<MavenProject> changedProjects) {
final Collection<MavenProject> changed = new ArrayList<>(changedProjects);
changed.addAll(configuration.ignoredProjects);
changed.addAll(configuration.buildAnywaysProjects);

Comparator<MavenProject> comparator;
if (configuration.buildChangedFirst)
{
logger.info("Sorting reactor so changed project are built first.");

comparator = (project1, project2) -> {
if (changedProjects.contains(project1)
&& changedProjects.contains(project2))
// both projects are changed, keep order
return 0;

if (changedProjects.contains(project1))
return -1;
if (changedProjects.contains(project2))
return 1;

return 0;
};
} else {
// do not reshuffle projects
comparator = (project1, project2) -> 0;
}

if (!configuration.buildAll) {
Collection<MavenProject> rebuildProjects = changed;
if (configuration.makeUpstream) {
Expand All @@ -47,6 +72,7 @@ public void setUpSession(Collection<MavenProject> changedProjects) {
} else {
mavenSession.setProjects(mavenSession.getProjects().stream()
.filter(rebuildProjects::contains)
.sorted(comparator)
.collect(Collectors.toList()));
}
} else {
Expand All @@ -56,6 +82,10 @@ public void setUpSession(Collection<MavenProject> changedProjects) {
this.ifSkipDependenciesTest(p);
this.ifSkipDependenciesSonar(p);
});

mavenSession.setProjects(mavenSession.getProjects().stream()
.sorted(comparator)
.collect(Collectors.toList()));
}
}

Expand Down

0 comments on commit 8836b59

Please sign in to comment.