Skip to content

Commit 45a49aa

Browse files
committed
Annotate set-rootdir mojo as threadsafe and update code to make it so. Although I'm guessing concurrent set calls for the property would be ok (since they would set it to the same thing), I went ahead and actually made the code thread safe. My motivation for this is to remove "The following goals are not marked as thread-safe" warnings from my multi-thread scijava maven builds.
1 parent 9879a47 commit 45a49aa

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/main/java/org/scijava/maven/plugin/SetRootDirPropertyMojo.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*
4646
* @author Johannes Schindelin
4747
*/
48-
@Mojo(name = "set-rootdir", defaultPhase = LifecyclePhase.VALIDATE)
48+
@Mojo(name = "set-rootdir", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
4949
public class SetRootDirPropertyMojo extends AbstractMojo {
5050

5151
/**
@@ -67,25 +67,40 @@ public class SetRootDirPropertyMojo extends AbstractMojo {
6767

6868
@Override
6969
public void execute() throws MojoExecutionException {
70-
if (currentProject.getProperties().getProperty(rootdirPropertyName) != null)
71-
{
72-
getLog().debug("Using previously defined rootdir");
73-
return;
70+
if (isRootdirPropertyNameDefined()) {
71+
logUsingPreviouslyDefinedRootdir();
72+
} else {
73+
setRootdirPropertyName();
7474
}
75+
}
7576

76-
if (!isLocalProject(currentProject)) return;
77+
private synchronized void setRootdirPropertyName() {
78+
if (isRootdirPropertyNameDefined()) {
79+
logUsingPreviouslyDefinedRootdir();
80+
} else if (isLocalProject(currentProject)) {
81+
MavenProject project = currentProject;
82+
for (;;) {
83+
final MavenProject parent = project.getParent();
84+
if (parent == null || ! isLocalProject(parent)) {
85+
break;
86+
}
87+
project = parent;
88+
}
7789

78-
MavenProject project = currentProject;
79-
for (;;) {
80-
final MavenProject parent = project.getParent();
81-
if (parent == null || !isLocalProject(parent)) break;
82-
project = parent;
90+
final String rootdir = project.getBasedir().getAbsolutePath();
91+
getLog().info("Setting rootdir: " + rootdir);
92+
for (final MavenProject reactorProject : reactorProjects) {
93+
reactorProject.getProperties().setProperty(rootdirPropertyName, rootdir);
94+
}
8395
}
96+
}
97+
98+
private boolean isRootdirPropertyNameDefined() {
99+
return currentProject.getProperties().getProperty(rootdirPropertyName) != null;
100+
}
84101

85-
final String rootdir = project.getBasedir().getAbsolutePath();
86-
getLog().info("Setting rootdir: " + rootdir);
87-
for (final MavenProject reactorProject : reactorProjects)
88-
reactorProject.getProperties().setProperty(rootdirPropertyName, rootdir);
102+
private void logUsingPreviouslyDefinedRootdir() {
103+
getLog().debug("Using previously defined rootdir");
89104
}
90105

91106
/**

0 commit comments

Comments
 (0)