Skip to content

Commit

Permalink
[Issue mojohaus#48] Fix rootlocation goal.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcsantos committed Dec 9, 2019
1 parent 9dd13af commit 74101fd
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/it/rootlocation-in-submodule/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals = validate
invoker.buildResult = success
invoker.project = submodule
36 changes: 36 additions & 0 deletions src/it/rootlocation-in-submodule/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<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.codehaus.mojo</groupId>
<artifactId>build-helper-rootlocation-parent</artifactId>
<version>1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>rootlocation search top root</name>
<description>
Tests that rootlocation finds the highest basedir.
</description>

<modules>
<module>submodule</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>rootlocation</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
16 changes: 16 additions & 0 deletions src/it/rootlocation-in-submodule/submodule/pom.xml
Original file line number Diff line number Diff line change
@@ -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.codehaus.mojo</groupId>
<artifactId>build-helper-rootlocation-parent</artifactId>
<version>1-SNAPSHOT</version>
</parent>

<artifactId>project</artifactId>
<packaging>jar</packaging>

<name>rootlocation sub project</name>

</project>
8 changes: 8 additions & 0 deletions src/it/rootlocation-in-submodule/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
File file = new File( basedir, "build.log" )
assert file.exists()

String text = file.getText("utf-8")

assert text.matches('(?ms)(.*?)define property rootlocation = "(.*)/rootlocation-in-submodule"(.*?)')

return true
2 changes: 2 additions & 0 deletions src/it/rootlocation-run-only-at-exec-root/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = validate
invoker.buildResult = success
39 changes: 39 additions & 0 deletions src/it/rootlocation-run-only-at-exec-root/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<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.codehaus.mojo</groupId>
<artifactId>build-helper-rootlocation-parent</artifactId>
<version>1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>rootlocation run only at execution root</name>
<description>
Tests that rootlocation is executed only at execution top level.
</description>

<modules>
<module>submodule</module>
</modules>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>rootlocation</goal>
</goals>
<configuration>
<runOnlyAtExecutionRoot>true</runOnlyAtExecutionRoot>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
16 changes: 16 additions & 0 deletions src/it/rootlocation-run-only-at-exec-root/submodule/pom.xml
Original file line number Diff line number Diff line change
@@ -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.codehaus.mojo</groupId>
<artifactId>build-helper-rootlocation-parent</artifactId>
<version>1-SNAPSHOT</version>
</parent>

<artifactId>project</artifactId>
<packaging>jar</packaging>

<name>rootlocation sub project</name>

</project>
9 changes: 9 additions & 0 deletions src/it/rootlocation-run-only-at-exec-root/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
File file = new File( basedir, "build.log" )
assert file.exists()

String text = file.getText("utf-8")

assert text.matches('(?ms)(.*?)define property rootlocation = "(.*)/rootlocation-run-only-at-exec-root"(.*?)')
assert text.contains("Skip getting the rootlocation in this project because it's not the Execution Root")

return true
2 changes: 2 additions & 0 deletions src/it/rootlocation/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = validate
invoker.buildResult = success
32 changes: 32 additions & 0 deletions src/it/rootlocation/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<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.codehaus.mojo</groupId>
<artifactId>build-helper-rootlocation</artifactId>
<version>1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>rootlocation</name>
<description>
Tests that rootlocation is set.
</description>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>rootlocation</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
8 changes: 8 additions & 0 deletions src/it/rootlocation/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
File file = new File( basedir, "build.log" )
assert file.exists()

String text = file.getText("utf-8")

assert text.matches('(?ms)(.*?)define property rootlocation = "(.*)/rootlocation"(.*?)')

return true
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import javax.annotation.Nonnull;

/**
* This goal will get the location of the root folder within a multi module build as a property {@code rootlocation}.
Expand Down Expand Up @@ -42,8 +45,25 @@ public void execute()
}
else
{
defineProperty( rootLocationProperty, session.getTopLevelProject().getBasedir().getAbsolutePath() );
MavenProject topLevelProject = findHighestParentProject(session.getTopLevelProject());
defineProperty( rootLocationProperty, topLevelProject.getBasedir().getAbsolutePath() );
}
}

private MavenProject findHighestParentProject(@Nonnull MavenProject project)
{
// search up model hierarchy to find the highest basedir location
MavenProject parent = project;
while (parent.getParent() != null)
{
if (parent.getParent().getBasedir() == null)
{
// we've hit a parent that was resolved. Stop going higher up in the hierarchy
break;
}
parent = parent.getParent();
}
return parent;
}

}

0 comments on commit 74101fd

Please sign in to comment.