From ad1076e467aa818c3f7e7596d0a51958f35e70d1 Mon Sep 17 00:00:00 2001 From: Henning Schmiedehausen Date: Tue, 21 Nov 2023 12:32:47 -0800 Subject: [PATCH] Add skipIfMissing flag (#187) * Add skipIfMissing flag - build-helper:add-source - build-helper:add-test-source - build-helper:add-resource - build-helper:add-test-resource Co-authored-by: Slawomir Jaranowski --- .../invoker.properties | 2 ++ src/it/add-resource-skip-if-missing/pom.xml | 36 +++++++++++++++++++ .../verify.groovy | 10 ++++++ .../invoker.properties | 2 ++ src/it/add-source-skip-if-missing/pom.xml | 33 +++++++++++++++++ .../add-source-skip-if-missing/verify.groovy | 10 ++++++ .../invoker.properties | 2 ++ .../add-test-resource-skip-if-missing/pom.xml | 36 +++++++++++++++++++ .../verify.groovy | 10 ++++++ .../invoker.properties | 2 ++ .../add-test-source-skip-if-missing/pom.xml | 33 +++++++++++++++++ .../verify.groovy | 10 ++++++ .../buildhelper/AbstractAddResourceMojo.java | 10 +++++- .../mojo/buildhelper/AddResourceMojo.java | 12 +++++++ .../mojo/buildhelper/AddSourceMojo.java | 20 +++++++++-- .../mojo/buildhelper/AddTestResourceMojo.java | 12 +++++++ .../mojo/buildhelper/AddTestSourceMojo.java | 21 +++++++++-- 17 files changed, 254 insertions(+), 7 deletions(-) create mode 100644 src/it/add-resource-skip-if-missing/invoker.properties create mode 100644 src/it/add-resource-skip-if-missing/pom.xml create mode 100644 src/it/add-resource-skip-if-missing/verify.groovy create mode 100644 src/it/add-source-skip-if-missing/invoker.properties create mode 100644 src/it/add-source-skip-if-missing/pom.xml create mode 100644 src/it/add-source-skip-if-missing/verify.groovy create mode 100644 src/it/add-test-resource-skip-if-missing/invoker.properties create mode 100644 src/it/add-test-resource-skip-if-missing/pom.xml create mode 100644 src/it/add-test-resource-skip-if-missing/verify.groovy create mode 100644 src/it/add-test-source-skip-if-missing/invoker.properties create mode 100644 src/it/add-test-source-skip-if-missing/pom.xml create mode 100644 src/it/add-test-source-skip-if-missing/verify.groovy diff --git a/src/it/add-resource-skip-if-missing/invoker.properties b/src/it/add-resource-skip-if-missing/invoker.properties new file mode 100644 index 00000000..ff27f707 --- /dev/null +++ b/src/it/add-resource-skip-if-missing/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = -X generate-sources +invoker.buildResult = success diff --git a/src/it/add-resource-skip-if-missing/pom.xml b/src/it/add-resource-skip-if-missing/pom.xml new file mode 100644 index 00000000..545cf3f8 --- /dev/null +++ b/src/it/add-resource-skip-if-missing/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + build-helper + integration-test + 1.0-SNAPSHOT + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + integration-test + generate-sources + + add-resource + + + + + ${project.basedir}/not-existing + does-not-matter + + + true + + + + + + + diff --git a/src/it/add-resource-skip-if-missing/verify.groovy b/src/it/add-resource-skip-if-missing/verify.groovy new file mode 100644 index 00000000..a7fbc2f7 --- /dev/null +++ b/src/it/add-resource-skip-if-missing/verify.groovy @@ -0,0 +1,10 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +assert text.contains("skipAddResourceIfMissing = true"); +assert text.contains("Skipping directory: "); +assert text.contains("not-existing, because it does not exist"); + +return true; diff --git a/src/it/add-source-skip-if-missing/invoker.properties b/src/it/add-source-skip-if-missing/invoker.properties new file mode 100644 index 00000000..ff27f707 --- /dev/null +++ b/src/it/add-source-skip-if-missing/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = -X generate-sources +invoker.buildResult = success diff --git a/src/it/add-source-skip-if-missing/pom.xml b/src/it/add-source-skip-if-missing/pom.xml new file mode 100644 index 00000000..103d5960 --- /dev/null +++ b/src/it/add-source-skip-if-missing/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + build-helper + integration-test + 1.0-SNAPSHOT + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + integration-test + generate-sources + + add-source + + + + ${project.basedir}/not-existing + + true + + + + + + + diff --git a/src/it/add-source-skip-if-missing/verify.groovy b/src/it/add-source-skip-if-missing/verify.groovy new file mode 100644 index 00000000..d033daa3 --- /dev/null +++ b/src/it/add-source-skip-if-missing/verify.groovy @@ -0,0 +1,10 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +assert text.contains("skipAddSourceIfMissing = true"); +assert text.contains("Skipping directory: "); +assert text.contains("not-existing, because it does not exist."); + +return true; diff --git a/src/it/add-test-resource-skip-if-missing/invoker.properties b/src/it/add-test-resource-skip-if-missing/invoker.properties new file mode 100644 index 00000000..ff27f707 --- /dev/null +++ b/src/it/add-test-resource-skip-if-missing/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = -X generate-sources +invoker.buildResult = success diff --git a/src/it/add-test-resource-skip-if-missing/pom.xml b/src/it/add-test-resource-skip-if-missing/pom.xml new file mode 100644 index 00000000..595de347 --- /dev/null +++ b/src/it/add-test-resource-skip-if-missing/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + build-helper + integration-test + 1.0-SNAPSHOT + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + integration-test + generate-sources + + add-test-resource + + + + + ${project.basedir}/not-existing + does-not-matter + + + true + + + + + + + diff --git a/src/it/add-test-resource-skip-if-missing/verify.groovy b/src/it/add-test-resource-skip-if-missing/verify.groovy new file mode 100644 index 00000000..2c19959d --- /dev/null +++ b/src/it/add-test-resource-skip-if-missing/verify.groovy @@ -0,0 +1,10 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +assert text.contains("skipAddTestResourceIfMissing = true"); +assert text.contains("Skipping directory: "); +assert text.contains("not-existing, because it does not exist."); + +return true; diff --git a/src/it/add-test-source-skip-if-missing/invoker.properties b/src/it/add-test-source-skip-if-missing/invoker.properties new file mode 100644 index 00000000..ff27f707 --- /dev/null +++ b/src/it/add-test-source-skip-if-missing/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = -X generate-sources +invoker.buildResult = success diff --git a/src/it/add-test-source-skip-if-missing/pom.xml b/src/it/add-test-source-skip-if-missing/pom.xml new file mode 100644 index 00000000..a37e06f8 --- /dev/null +++ b/src/it/add-test-source-skip-if-missing/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + build-helper + integration-test + 1.0-SNAPSHOT + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + integration-test + generate-sources + + add-test-source + + + + ${project.basedir}/not-existing + + true + + + + + + + diff --git a/src/it/add-test-source-skip-if-missing/verify.groovy b/src/it/add-test-source-skip-if-missing/verify.groovy new file mode 100644 index 00000000..5cf6e798 --- /dev/null +++ b/src/it/add-test-source-skip-if-missing/verify.groovy @@ -0,0 +1,10 @@ +File file = new File( basedir, "build.log" ); +assert file.exists(); + +String text = file.getText("utf-8"); + +assert text.contains("skipAddTestSourceIfMissing = true"); +assert text.contains("Skipping directory: "); +assert text.contains("not-existing, because it does not exist."); + +return true; diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java index 76b17d0c..5503d604 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java @@ -67,10 +67,18 @@ public void execute() { resource.setDirectory(resourceDir.getAbsolutePath()); } - addResource(resource); + if (isSkipIfMissing() && !resourceDir.exists()) { + if (getLog().isDebugEnabled()) { + getLog().debug("Skipping directory: " + resourceDir + ", because it does not exist."); + } + } else { + addResource(resource); + } } } + protected abstract boolean isSkipIfMissing(); + protected abstract boolean isSkip(); /** diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java index f4129233..897c888c 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java @@ -46,6 +46,14 @@ public class AddResourceMojo extends AbstractAddResourceMojo { @Parameter(property = "buildhelper.addresource.skip", defaultValue = "false") private boolean skipAddResource; + /** + * If a resource directory does not exist, do not add it as a root. + * + * @since 3.5.0 + */ + @Parameter(property = "buildhelper.addresource.skipIfMissing", defaultValue = "false") + private boolean skipAddResourceIfMissing; + public void addResource(Resource resource) { getProject().addResource(resource); if (getLog().isDebugEnabled()) { @@ -53,6 +61,10 @@ public void addResource(Resource resource) { } } + protected boolean isSkipIfMissing() { + return skipAddResourceIfMissing; + } + protected boolean isSkip() { return skipAddResource; } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java index 5198b8b7..d74c4b9d 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java @@ -62,6 +62,14 @@ public class AddSourceMojo extends AbstractMojo { @Parameter(property = "buildhelper.addsource.skip", defaultValue = "false") private boolean skipAddSource; + /** + * If a directory does not exist, do not add it as a source root. + * + * @since 3.5.0 + */ + @Parameter(property = "buildhelper.addsource.skipIfMissing", defaultValue = "false") + private boolean skipAddSourceIfMissing; + public void execute() { if (skipAddSource) { if (getLog().isInfoEnabled()) { @@ -71,9 +79,15 @@ public void execute() { } for (File source : sources) { - this.project.addCompileSourceRoot(source.getAbsolutePath()); - if (getLog().isInfoEnabled()) { - getLog().info("Source directory: " + source + " added."); + if (skipAddSourceIfMissing && !source.exists()) { + if (getLog().isDebugEnabled()) { + getLog().debug("Skipping directory: " + source + ", because it does not exist."); + } + } else { + this.project.addCompileSourceRoot(source.getAbsolutePath()); + if (getLog().isInfoEnabled()) { + getLog().info("Source directory: " + source + " added."); + } } } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java index a1000caf..df107c19 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java @@ -46,6 +46,14 @@ public class AddTestResourceMojo extends AbstractAddResourceMojo { @Parameter(property = "buildhelper.addtestresource.skip", defaultValue = "false") private boolean skipAddTestResource; + /** + * If a test resource directory does not exist, do not add it as a root. + * + * @since 3.5.0 + */ + @Parameter(property = "buildhelper.addtestresource.skipIfMissing", defaultValue = "false") + private boolean skipAddTestResourceIfMissing; + /** * Add the resource to the project. * @@ -62,4 +70,8 @@ public void addResource(Resource resource) { protected boolean isSkip() { return skipAddTestResource; } + + protected boolean isSkipIfMissing() { + return skipAddTestResourceIfMissing; + } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java index f79638c2..0251a76a 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java @@ -40,6 +40,7 @@ */ @Mojo(name = "add-test-source", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES, threadSafe = true) public class AddTestSourceMojo extends AbstractMojo { + /** * Additional test source directories. * @@ -62,6 +63,14 @@ public class AddTestSourceMojo extends AbstractMojo { @Parameter(property = "buildhelper.addtestsource.skip", defaultValue = "false") private boolean skipAddTestSource; + /** + * If a directory does not exist, do not add it as a test source root. + * + * @since 3.5.0 + */ + @Parameter(property = "buildhelper.addtestsource.skipIfMissing", defaultValue = "false") + private boolean skipAddTestSourceIfMissing; + public void execute() { if (skipAddTestSource) { if (getLog().isInfoEnabled()) { @@ -71,9 +80,15 @@ public void execute() { } for (File source : sources) { - this.project.addTestCompileSourceRoot(source.getAbsolutePath()); - if (getLog().isInfoEnabled()) { - getLog().info("Test Source directory: " + source + " added."); + if (skipAddTestSourceIfMissing && !source.exists()) { + if (getLog().isDebugEnabled()) { + getLog().debug("Skipping directory: " + source + ", because it does not exist."); + } + } else { + this.project.addTestCompileSourceRoot(source.getAbsolutePath()); + if (getLog().isInfoEnabled()) { + getLog().info("Test Source directory: " + source + " added."); + } } } }