Skip to content

Commit caefcde

Browse files
authored
Bug: use change detecton strategies (#462)
Use the change detection strategy from maven-filtering Depends on apache/maven-filtering#323 Fixes #453
1 parent 38534e3 commit caefcde

4 files changed

Lines changed: 59 additions & 14 deletions

File tree

.mvn/maven.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-Dapache.snapshots
2+

pom.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ under the License.
7676
<project.build.outputTimestamp>2025-11-22T21:31:20Z</project.build.outputTimestamp>
7777

7878
<version.slf4j>1.7.36</version.slf4j>
79+
<version.plexus-utils>3.6.0</version.plexus-utils>
7980
<version.maven-invoker-plugin>3.9.1</version.maven-invoker-plugin>
8081
</properties>
8182

@@ -84,12 +85,11 @@ under the License.
8485
<dependency>
8586
<groupId>org.apache.maven.shared</groupId>
8687
<artifactId>maven-filtering</artifactId>
87-
<version>3.4.0</version>
88+
<version>3.4.1-SNAPSHOT</version>
8889
</dependency>
8990
<dependency>
90-
<groupId>org.apache.commons</groupId>
91-
<artifactId>commons-lang3</artifactId>
92-
<version>3.20.0</version>
91+
<groupId>org.codehaus.plexus</groupId>
92+
<artifactId>plexus-utils</artifactId>
9393
</dependency>
9494

9595
<!-- Maven (provided) -->
@@ -148,11 +148,6 @@ under the License.
148148
<version>${version.slf4j}</version>
149149
<scope>test</scope>
150150
</dependency>
151-
<dependency>
152-
<groupId>org.codehaus.plexus</groupId>
153-
<artifactId>plexus-utils</artifactId>
154-
<scope>test</scope>
155-
</dependency>
156151
<dependency>
157152
<groupId>commons-io</groupId>
158153
<artifactId>commons-io</artifactId>

src/main/java/org/apache/maven/plugins/resources/ResourcesMojo.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Map;
3131
import java.util.Properties;
3232

33-
import org.apache.commons.lang3.StringUtils;
3433
import org.apache.maven.execution.MavenSession;
3534
import org.apache.maven.model.Resource;
3635
import org.apache.maven.plugin.AbstractMojo;
@@ -39,9 +38,11 @@
3938
import org.apache.maven.plugins.annotations.Mojo;
4039
import org.apache.maven.plugins.annotations.Parameter;
4140
import org.apache.maven.project.MavenProject;
41+
import org.apache.maven.shared.filtering.ChangeDetection;
4242
import org.apache.maven.shared.filtering.MavenFilteringException;
4343
import org.apache.maven.shared.filtering.MavenResourcesExecution;
4444
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
45+
import org.codehaus.plexus.util.StringUtils;
4546

4647
/**
4748
* Copy resources for the main source code to the main output directory. Always uses the project.build.resources element
@@ -156,10 +157,32 @@ public class ResourcesMojo extends AbstractMojo {
156157
* Overwrite existing files even if the destination files are newer.
157158
*
158159
* @since 2.3
160+
* @deprecated Use {@link #changeDetection} instead.
159161
*/
162+
@Deprecated
160163
@Parameter(defaultValue = "false")
161164
private boolean overwrite;
162165

166+
/**
167+
* The strategy to use for change detection. Supported values are listed below. If this parameter is configured,
168+
* it will override the value of {@link #overwrite}.
169+
*
170+
* Strategies and their behavior are as follows:
171+
* <ul>
172+
* <li><strong>content</strong>: This is the default strategy since version 3.4.0. Overwrites existing target file only if content differs.</li>
173+
* <li><strong>timestamp</strong>: This was the default strategy before version 3.4.0. Overwrites existing target file only if target timestamp is older than source timestamp.</li>
174+
* <li><strong>timestamp+content</strong>: Combines the two strategies above; if timestamp is older and if content differs, existing target file will be overwritten.</li>
175+
* <li><strong>always</strong>: Always overwrites existing target file. Equivalent of {@code overwrite=true}.</li>
176+
* <li><strong>never</strong>: Never overwrites existing target file.</li>
177+
* </ul>
178+
*
179+
* Note: default value of this parameter is handled programmatically (as "content") for programmatic detection reasons.
180+
*
181+
* @since 3.5.0
182+
*/
183+
@Parameter
184+
private String changeDetection;
185+
163186
/**
164187
* Copy any empty directories included in the Resources.
165188
*
@@ -323,7 +346,7 @@ public void execute() throws MojoExecutionException {
323346
mavenResourcesExecution.setInjectProjectBuildFilters(false);
324347

325348
mavenResourcesExecution.setEscapeString(escapeString);
326-
mavenResourcesExecution.setOverwrite(overwrite);
349+
mavenResourcesExecution.setChangeDetection(getChangeDetection());
327350
mavenResourcesExecution.setIncludeEmptyDirs(includeEmptyDirs);
328351
mavenResourcesExecution.setSupportMultiLineFiltering(supportMultiLineFiltering);
329352
mavenResourcesExecution.setFilterFilenames(fileNameFiltering);
@@ -350,6 +373,29 @@ public void execute() throws MojoExecutionException {
350373
}
351374
}
352375

376+
private ChangeDetection getChangeDetection() {
377+
if (changeDetection != null) {
378+
switch (changeDetection) {
379+
case "content":
380+
return ChangeDetection.CONTENT;
381+
case "timestamp":
382+
return ChangeDetection.TIMESTAMP;
383+
case "timestamp+content":
384+
return ChangeDetection.TIMESTAMP_AND_CONTENT;
385+
case "always":
386+
return ChangeDetection.ALWAYS;
387+
case "never":
388+
return ChangeDetection.NEVER;
389+
default:
390+
throw new IllegalArgumentException("Invalid value for changeDetection: " + changeDetection);
391+
}
392+
} else if (overwrite) {
393+
return ChangeDetection.ALWAYS;
394+
} else {
395+
return ChangeDetection.CONTENT;
396+
}
397+
}
398+
353399
/**
354400
* This solves https://issues.apache.org/jira/browse/MRESOURCES-99.<br/>
355401
* BUT:<br/>

src/test/java/org/apache/maven/plugins/resources/filters/ItFilter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.io.File;
2525
import java.io.IOException;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
2628
import java.util.ArrayList;
2729
import java.util.Collections;
2830
import java.util.List;
@@ -31,8 +33,6 @@
3133
import org.apache.maven.shared.filtering.MavenResourcesExecution;
3234
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
3335

34-
import static org.apache.commons.io.FileUtils.writeLines;
35-
3636
/**
3737
* @author Olivier Lamy
3838
* @since 2.5
@@ -73,7 +73,9 @@ public void filterResources(MavenResourcesExecution mavenResourcesExecution) thr
7373
.getMavenSession()
7474
.getSystemProperties()
7575
.getProperty("toto"));
76-
writeLines(f, lines);
76+
Path target = f.toPath();
77+
Files.createDirectories(target.getParent());
78+
Files.write(target, lines);
7779
} catch (IOException e) {
7880
throw new MavenFilteringException(e.getMessage(), e);
7981
}

0 commit comments

Comments
 (0)