Skip to content

Commit ad1076e

Browse files
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 <[email protected]>
1 parent faab936 commit ad1076e

File tree

17 files changed

+254
-7
lines changed

17 files changed

+254
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = -X generate-sources
2+
invoker.buildResult = success
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<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">
2+
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>build-helper</groupId>
6+
<artifactId>integration-test</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>@project.groupId@</groupId>
13+
<artifactId>@project.artifactId@</artifactId>
14+
<version>@project.version@</version>
15+
<executions>
16+
<execution>
17+
<id>integration-test</id>
18+
<phase>generate-sources</phase>
19+
<goals>
20+
<goal>add-resource</goal>
21+
</goals>
22+
<configuration>
23+
<resources>
24+
<resource>
25+
<directory>${project.basedir}/not-existing</directory>
26+
<targetPath>does-not-matter</targetPath>
27+
</resource>
28+
</resources>
29+
<skipAddResourceIfMissing>true</skipAddResourceIfMissing>
30+
</configuration>
31+
</execution>
32+
</executions>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
File file = new File( basedir, "build.log" );
2+
assert file.exists();
3+
4+
String text = file.getText("utf-8");
5+
6+
assert text.contains("skipAddResourceIfMissing = true");
7+
assert text.contains("Skipping directory: ");
8+
assert text.contains("not-existing, because it does not exist");
9+
10+
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = -X generate-sources
2+
invoker.buildResult = success
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<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">
2+
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>build-helper</groupId>
6+
<artifactId>integration-test</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>@project.groupId@</groupId>
13+
<artifactId>@project.artifactId@</artifactId>
14+
<version>@project.version@</version>
15+
<executions>
16+
<execution>
17+
<id>integration-test</id>
18+
<phase>generate-sources</phase>
19+
<goals>
20+
<goal>add-source</goal>
21+
</goals>
22+
<configuration>
23+
<sources>
24+
<source>${project.basedir}/not-existing</source>
25+
</sources>
26+
<skipAddSourceIfMissing>true</skipAddSourceIfMissing>
27+
</configuration>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
File file = new File( basedir, "build.log" );
2+
assert file.exists();
3+
4+
String text = file.getText("utf-8");
5+
6+
assert text.contains("skipAddSourceIfMissing = true");
7+
assert text.contains("Skipping directory: ");
8+
assert text.contains("not-existing, because it does not exist.");
9+
10+
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = -X generate-sources
2+
invoker.buildResult = success
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<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">
2+
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>build-helper</groupId>
6+
<artifactId>integration-test</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>@project.groupId@</groupId>
13+
<artifactId>@project.artifactId@</artifactId>
14+
<version>@project.version@</version>
15+
<executions>
16+
<execution>
17+
<id>integration-test</id>
18+
<phase>generate-sources</phase>
19+
<goals>
20+
<goal>add-test-resource</goal>
21+
</goals>
22+
<configuration>
23+
<resources>
24+
<resource>
25+
<directory>${project.basedir}/not-existing</directory>
26+
<targetPath>does-not-matter</targetPath>
27+
</resource>
28+
</resources>
29+
<skipAddTestResourceIfMissing>true</skipAddTestResourceIfMissing>
30+
</configuration>
31+
</execution>
32+
</executions>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
File file = new File( basedir, "build.log" );
2+
assert file.exists();
3+
4+
String text = file.getText("utf-8");
5+
6+
assert text.contains("skipAddTestResourceIfMissing = true");
7+
assert text.contains("Skipping directory: ");
8+
assert text.contains("not-existing, because it does not exist.");
9+
10+
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = -X generate-sources
2+
invoker.buildResult = success
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<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">
2+
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>build-helper</groupId>
6+
<artifactId>integration-test</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<groupId>@project.groupId@</groupId>
13+
<artifactId>@project.artifactId@</artifactId>
14+
<version>@project.version@</version>
15+
<executions>
16+
<execution>
17+
<id>integration-test</id>
18+
<phase>generate-sources</phase>
19+
<goals>
20+
<goal>add-test-source</goal>
21+
</goals>
22+
<configuration>
23+
<sources>
24+
<source>${project.basedir}/not-existing</source>
25+
</sources>
26+
<skipAddTestSourceIfMissing>true</skipAddTestSourceIfMissing>
27+
</configuration>
28+
</execution>
29+
</executions>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
File file = new File( basedir, "build.log" );
2+
assert file.exists();
3+
4+
String text = file.getText("utf-8");
5+
6+
assert text.contains("skipAddTestSourceIfMissing = true");
7+
assert text.contains("Skipping directory: ");
8+
assert text.contains("not-existing, because it does not exist.");
9+
10+
return true;

src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,18 @@ public void execute() {
6767
resource.setDirectory(resourceDir.getAbsolutePath());
6868
}
6969

70-
addResource(resource);
70+
if (isSkipIfMissing() && !resourceDir.exists()) {
71+
if (getLog().isDebugEnabled()) {
72+
getLog().debug("Skipping directory: " + resourceDir + ", because it does not exist.");
73+
}
74+
} else {
75+
addResource(resource);
76+
}
7177
}
7278
}
7379

80+
protected abstract boolean isSkipIfMissing();
81+
7482
protected abstract boolean isSkip();
7583

7684
/**

src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java

+12
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,25 @@ public class AddResourceMojo extends AbstractAddResourceMojo {
4646
@Parameter(property = "buildhelper.addresource.skip", defaultValue = "false")
4747
private boolean skipAddResource;
4848

49+
/**
50+
* If a resource directory does not exist, do not add it as a root.
51+
*
52+
* @since 3.5.0
53+
*/
54+
@Parameter(property = "buildhelper.addresource.skipIfMissing", defaultValue = "false")
55+
private boolean skipAddResourceIfMissing;
56+
4957
public void addResource(Resource resource) {
5058
getProject().addResource(resource);
5159
if (getLog().isDebugEnabled()) {
5260
getLog().debug("Added resource: " + resource.getDirectory());
5361
}
5462
}
5563

64+
protected boolean isSkipIfMissing() {
65+
return skipAddResourceIfMissing;
66+
}
67+
5668
protected boolean isSkip() {
5769
return skipAddResource;
5870
}

src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public class AddSourceMojo extends AbstractMojo {
6262
@Parameter(property = "buildhelper.addsource.skip", defaultValue = "false")
6363
private boolean skipAddSource;
6464

65+
/**
66+
* If a directory does not exist, do not add it as a source root.
67+
*
68+
* @since 3.5.0
69+
*/
70+
@Parameter(property = "buildhelper.addsource.skipIfMissing", defaultValue = "false")
71+
private boolean skipAddSourceIfMissing;
72+
6573
public void execute() {
6674
if (skipAddSource) {
6775
if (getLog().isInfoEnabled()) {
@@ -71,9 +79,15 @@ public void execute() {
7179
}
7280

7381
for (File source : sources) {
74-
this.project.addCompileSourceRoot(source.getAbsolutePath());
75-
if (getLog().isInfoEnabled()) {
76-
getLog().info("Source directory: " + source + " added.");
82+
if (skipAddSourceIfMissing && !source.exists()) {
83+
if (getLog().isDebugEnabled()) {
84+
getLog().debug("Skipping directory: " + source + ", because it does not exist.");
85+
}
86+
} else {
87+
this.project.addCompileSourceRoot(source.getAbsolutePath());
88+
if (getLog().isInfoEnabled()) {
89+
getLog().info("Source directory: " + source + " added.");
90+
}
7791
}
7892
}
7993
}

src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public class AddTestResourceMojo extends AbstractAddResourceMojo {
4646
@Parameter(property = "buildhelper.addtestresource.skip", defaultValue = "false")
4747
private boolean skipAddTestResource;
4848

49+
/**
50+
* If a test resource directory does not exist, do not add it as a root.
51+
*
52+
* @since 3.5.0
53+
*/
54+
@Parameter(property = "buildhelper.addtestresource.skipIfMissing", defaultValue = "false")
55+
private boolean skipAddTestResourceIfMissing;
56+
4957
/**
5058
* Add the resource to the project.
5159
*
@@ -62,4 +70,8 @@ public void addResource(Resource resource) {
6270
protected boolean isSkip() {
6371
return skipAddTestResource;
6472
}
73+
74+
protected boolean isSkipIfMissing() {
75+
return skipAddTestResourceIfMissing;
76+
}
6577
}

src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
@Mojo(name = "add-test-source", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES, threadSafe = true)
4242
public class AddTestSourceMojo extends AbstractMojo {
43+
4344
/**
4445
* Additional test source directories.
4546
*
@@ -62,6 +63,14 @@ public class AddTestSourceMojo extends AbstractMojo {
6263
@Parameter(property = "buildhelper.addtestsource.skip", defaultValue = "false")
6364
private boolean skipAddTestSource;
6465

66+
/**
67+
* If a directory does not exist, do not add it as a test source root.
68+
*
69+
* @since 3.5.0
70+
*/
71+
@Parameter(property = "buildhelper.addtestsource.skipIfMissing", defaultValue = "false")
72+
private boolean skipAddTestSourceIfMissing;
73+
6574
public void execute() {
6675
if (skipAddTestSource) {
6776
if (getLog().isInfoEnabled()) {
@@ -71,9 +80,15 @@ public void execute() {
7180
}
7281

7382
for (File source : sources) {
74-
this.project.addTestCompileSourceRoot(source.getAbsolutePath());
75-
if (getLog().isInfoEnabled()) {
76-
getLog().info("Test Source directory: " + source + " added.");
83+
if (skipAddTestSourceIfMissing && !source.exists()) {
84+
if (getLog().isDebugEnabled()) {
85+
getLog().debug("Skipping directory: " + source + ", because it does not exist.");
86+
}
87+
} else {
88+
this.project.addTestCompileSourceRoot(source.getAbsolutePath());
89+
if (getLog().isInfoEnabled()) {
90+
getLog().info("Test Source directory: " + source + " added.");
91+
}
7792
}
7893
}
7994
}

0 commit comments

Comments
 (0)