Skip to content

Commit

Permalink
Add skipIfMissing flag (#187)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
hgschmie and slawekjaranowski authored Nov 21, 2023
1 parent faab936 commit ad1076e
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/it/add-resource-skip-if-missing/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = -X generate-sources
invoker.buildResult = success
36 changes: 36 additions & 0 deletions src/it/add-resource-skip-if-missing/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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>build-helper</groupId>
<artifactId>integration-test</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>integration-test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/not-existing</directory>
<targetPath>does-not-matter</targetPath>
</resource>
</resources>
<skipAddResourceIfMissing>true</skipAddResourceIfMissing>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
10 changes: 10 additions & 0 deletions src/it/add-resource-skip-if-missing/verify.groovy
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/it/add-source-skip-if-missing/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = -X generate-sources
invoker.buildResult = success
33 changes: 33 additions & 0 deletions src/it/add-source-skip-if-missing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>build-helper</groupId>
<artifactId>integration-test</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>integration-test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/not-existing</source>
</sources>
<skipAddSourceIfMissing>true</skipAddSourceIfMissing>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
10 changes: 10 additions & 0 deletions src/it/add-source-skip-if-missing/verify.groovy
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/it/add-test-resource-skip-if-missing/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = -X generate-sources
invoker.buildResult = success
36 changes: 36 additions & 0 deletions src/it/add-test-resource-skip-if-missing/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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>build-helper</groupId>
<artifactId>integration-test</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>integration-test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/not-existing</directory>
<targetPath>does-not-matter</targetPath>
</resource>
</resources>
<skipAddTestResourceIfMissing>true</skipAddTestResourceIfMissing>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
10 changes: 10 additions & 0 deletions src/it/add-test-resource-skip-if-missing/verify.groovy
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/it/add-test-source-skip-if-missing/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = -X generate-sources
invoker.buildResult = success
33 changes: 33 additions & 0 deletions src/it/add-test-source-skip-if-missing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>build-helper</groupId>
<artifactId>integration-test</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>integration-test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/not-existing</source>
</sources>
<skipAddTestSourceIfMissing>true</skipAddTestSourceIfMissing>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
10 changes: 10 additions & 0 deletions src/it/add-test-source-skip-if-missing/verify.groovy
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,25 @@ 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()) {
getLog().debug("Added resource: " + resource.getDirectory());
}
}

protected boolean isSkipIfMissing() {
return skipAddResourceIfMissing;
}

protected boolean isSkip() {
return skipAddResource;
}
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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.");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -62,4 +70,8 @@ public void addResource(Resource resource) {
protected boolean isSkip() {
return skipAddTestResource;
}

protected boolean isSkipIfMissing() {
return skipAddTestResourceIfMissing;
}
}
21 changes: 18 additions & 3 deletions src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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()) {
Expand All @@ -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.");
}
}
}
}
Expand Down

0 comments on commit ad1076e

Please sign in to comment.