Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@
*/
public abstract class AbstractGroovySourcesMojo extends AbstractGroovyMojo {

/**
* Main source directory name.
*/
protected static final String MAIN = "main";
protected enum SourceRootScope {
MAIN("removeCompileSourceRoot"),
TEST("removeTestCompileSourceRoot");

/**
* Test source directory name.
*/
protected static final String TEST = "test";
private final String removalMethod;

SourceRootScope(String removalMethod) {
this.removalMethod = removalMethod;
}

String getDirectoryName() {
return name().toLowerCase(Locale.ROOT);
}

String getRemovalMethod() {
return removalMethod;
}
}

/**
* Gets the set of included files from the specified source files or source directory (if sources are null).
Expand Down Expand Up @@ -81,7 +90,8 @@ protected FileSet[] getFilesets(final FileSet[] fromSources, final boolean inclu
groovyFileSets = fromSources;
} else {
FileSet groovyFileSet = new FileSet();
String groovyDirectory = "src" + File.separator + MAIN + File.separator + "groovy";
String groovyDirectory = "src" + File.separator + SourceRootScope.MAIN.getDirectoryName()
+ File.separator + "groovy";
groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
groovyFileSets = new FileSet[]{groovyFileSet};
Expand Down Expand Up @@ -120,7 +130,8 @@ protected FileSet[] getTestFilesets(final FileSet[] fromSources, final boolean i
groovyFileSets = fromSources;
} else {
FileSet groovyFileSet = new FileSet();
String groovyDirectory = "src" + File.separator + TEST + File.separator + "groovy";
String groovyDirectory = "src" + File.separator + SourceRootScope.TEST.getDirectoryName()
+ File.separator + "groovy";
groovyFileSet.setDirectory(project.getBasedir() + File.separator + groovyDirectory);
groovyFileSet.setIncludes(singletonList(GROOVY_SOURCES_PATTERN));
groovyFileSets = new FileSet[]{groovyFileSet};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,30 @@ public abstract class AbstractGroovyStubSourcesMojo extends AbstractGroovySource
* Removes the source roots from the project, using reflection to avoid breaking changes in Maven 4.
*
* @param project the Maven project
* @param scopeToRemove the scope to remove (main or test)
* @param sourceRootScope the source root scope to remove
* @param sourceDirectory the source directory to remove
* @throws ClassNotFoundException when a class needed cannot be found
* @throws NoSuchFieldException when a field needed cannot be found
* @throws NoSuchMethodException when a method needed cannot be found
* @throws IllegalAccessException when a method needed cannot be accessed
*/
protected static void removeSourceRoot(MavenProject project, String scopeToRemove, File sourceDirectory)
protected static void removeSourceRoot(MavenProject project, SourceRootScope sourceRootScope, File sourceDirectory)
throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException {
try {
project.getClass().getMethod(sourceRootScope.getRemovalMethod(), String.class)
.invoke(project, sourceDirectory.getAbsolutePath());
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
} catch (NoSuchMethodException e) {
try {
removeMaven4SourceRoot(project, sourceRootScope, sourceDirectory);
} catch (ClassNotFoundException e2) {
removeMaven3SourceRoot(project, sourceRootScope, sourceDirectory);
}
}
}

protected static void removeMaven4SourceRoot(MavenProject project, SourceRootScope sourceRootScope, File sourceDirectory)
throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException {
Class<?> sourceRoot = project.getClass().getClassLoader().loadClass("org.apache.maven.api.SourceRoot");
Path path = project.getBasedir().toPath().resolve(sourceDirectory.getAbsolutePath()).normalize();
Expand All @@ -48,7 +64,7 @@ protected static void removeSourceRoot(MavenProject project, String scopeToRemov
Collection<?> sources = (Collection<?>) field.get(project);
sources.removeIf(source -> {
try {
return Objects.equals(id.invoke(scope.invoke(source)), scopeToRemove)
return Objects.equals(id.invoke(scope.invoke(source)), sourceRootScope.getDirectoryName())
&& Objects.equals(id.invoke(language.invoke(source)), "java")
&& Objects.equals(directory.invoke(source), path);
} catch (IllegalAccessException | InvocationTargetException ex) {
Expand All @@ -57,6 +73,14 @@ protected static void removeSourceRoot(MavenProject project, String scopeToRemov
});
}

protected static void removeMaven3SourceRoot(MavenProject project, SourceRootScope sourceRootScope, File sourceDirectory) {
if (sourceRootScope == SourceRootScope.MAIN) {
project.getCompileSourceRoots().remove(sourceDirectory.getAbsolutePath());
} else {
project.getTestCompileSourceRoots().remove(sourceDirectory.getAbsolutePath());
}
}

/**
* Gets the set of stub files in specified directory.
*
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/codehaus/gmavenplus/mojo/RemoveStubsMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,9 @@ public class RemoveStubsMojo extends AbstractGroovyStubSourcesMojo {
@Override
public void execute() {
try {
project.getCompileSourceRoots().remove(stubsOutputDirectory.getAbsolutePath());
} catch (UnsupportedOperationException e) {
try {
removeSourceRoot(project, "main", stubsOutputDirectory);
} catch (Throwable e2) {
e.addSuppressed(e2);
throw e;
}
removeSourceRoot(project, SourceRootScope.MAIN, stubsOutputDirectory);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Unable to remove Groovy stub source root", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ public class RemoveTestStubsMojo extends AbstractGroovyStubSourcesMojo {
public void execute() {
if (!skipTests) {
try {
project.getTestCompileSourceRoots().remove(testStubsOutputDirectory.getAbsolutePath());
} catch (UnsupportedOperationException e) {
try {
removeSourceRoot(project, "test", testStubsOutputDirectory);
} catch (Throwable e2) {
e.addSuppressed(e2);
throw e;
}
removeSourceRoot(project, SourceRootScope.TEST, testStubsOutputDirectory);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Unable to remove Groovy test stub source root", e);
}
}
}
Expand Down
Loading