gnodet commented on code in PR #406:
URL: https://github.com/apache/maven-filtering/pull/406#discussion_r4106018189
##########
src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java:
##########
@@ -252,13 +252,17 @@ public void filterResources(MavenResourcesExecution
mavenResourcesExecution) thr
for (String name : includedFiles) {
- LOGGER.debug("Copying file " + name);
Path source = resourceDirectory.resolve(name);
Path destinationFile = getDestinationFile(outputDirectory,
targetPath, name, mavenResourcesExecution);
+ // Per-resource changeDetection overrides the request-level
setting
+ ChangeDetection changeDetection =
resource.getChangeDetection() != null
+ ? resource.getChangeDetection()
+ : mavenResourcesExecution.getChangeDetection();
+
if (mavenResourcesExecution.isFlatten() &&
Files.exists(destinationFile)) {
- if (mavenResourcesExecution.isOverwrite()) {
+ if (changeDetection == ChangeDetection.ALWAYS) {
Review Comment:
Fixed in 3ac738c. Error message now says "changeDetection is not set to
ALWAYS".
##########
src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java:
##########
@@ -292,25 +292,115 @@ static boolean isEmpty(final String string) {
*
* @param from the file to copy
* @param to the destination file
- * @param encoding the file output encoding (only if wrappers is not empty)
+ * @param encoding the encoding used for both reading and writing (only if
wrappers is not empty)
* @param wrappers array of {@link FilterWrapper}
+ * @param overwrite if {@code true}, uses {@link ChangeDetection#ALWAYS};
if {@code false}, uses {@link ChangeDetection#CONTENT}
* @throws IOException if an IO error occurs during copying or filtering
+ * @deprecated use {@link #copyFile(Path, Path, String, FilterWrapper[],
ChangeDetection)} instead
*/
+ @Deprecated
public static void copyFile(Path from, Path to, String encoding,
FilterWrapper[] wrappers, boolean overwrite)
throws IOException {
+ copyFile(from, to, encoding, wrappers, overwrite ?
ChangeDetection.ALWAYS : ChangeDetection.CONTENT);
+ }
+
+ /**
+ * Copies a file from the source to the destination, applying the
specified filters if provided.
+ *
+ * @param from the file to copy
+ * @param to the destination file
+ * @param encoding the file output encoding (only if wrappers is not empty)
+ * @param wrappers array of {@link FilterWrapper}
+ * @param changeDetection the strategy to apply if to is existing file
+ * @return {@code true} if the file was copied.
+ * @throws IOException if an IO error occurs during copying or filtering
+ * @since 4.0.0-beta-2
+ */
+ public static boolean copyFile(
+ Path from, Path to, String encoding, FilterWrapper[] wrappers,
ChangeDetection changeDetection)
+ throws IOException {
+ return copyFile(from, to, encoding, encoding, wrappers,
changeDetection);
+ }
+
+ /**
+ * Copy and optionally filter a file using separate input and output
encodings. This allows
+ * encoding conversion during resource filtering (e.g. reading ISO-8859-1
and writing UTF-8).
+ * <b>If wrappers is null or empty, the file is copied as raw bytes —
encoding parameters are
+ * ignored in that case.</b>
+ *
+ * @param from the file to copy
+ * @param to the destination file
+ * @param inputEncoding the charset used to read {@code from} (only when
wrappers is not empty)
+ * @param outputEncoding the charset used to write {@code to} (only when
wrappers is not empty)
+ * @param wrappers array of {@link FilterWrapper}
+ * @param changeDetection the strategy to apply if to is existing file
+ * @return {@code true} if the file was copied.
+ * @throws IOException if an IO error occurs during copying or filtering
+ * @since 4.0.0-beta-3
+ */
+ public static boolean copyFile(
+ Path from,
+ Path to,
+ String inputEncoding,
+ String outputEncoding,
+ FilterWrapper[] wrappers,
+ ChangeDetection changeDetection)
+ throws IOException {
+ boolean needsCopy = false;
+ boolean unconditionally = false;
+ switch (changeDetection) {
+ case NEVER:
+ needsCopy = !Files.isRegularFile(to);
+ unconditionally = true;
+ break;
+ case ALWAYS:
+ needsCopy = true;
+ unconditionally = true;
+ break;
+ case TIMESTAMP:
+ needsCopy = !Files.exists(to)
+ || Files.getLastModifiedTime(to).toMillis()
+ < Files.getLastModifiedTime(from).toMillis();
+ unconditionally = true;
+ break;
+ case CONTENT:
+ needsCopy = true;
+ break;
+ case TIMESTAMP_AND_CONTENT:
+ needsCopy = !Files.exists(to)
+ || Files.getLastModifiedTime(to).toMillis()
+ < Files.getLastModifiedTime(from).toMillis();
+ break;
+ default:
+ throw new IllegalArgumentException("Unsupported change
detection mode: " + changeDetection);
+ }
+ boolean copied = false;
+ if (needsCopy) {
+ if (unconditionally) {
+ copied = copyUnconditionally(from, to, inputEncoding,
outputEncoding, wrappers);
+ } else {
+ copied = copyIfContentsChanged(from, to, inputEncoding,
outputEncoding, wrappers);
+ }
+ }
+ return copied;
+ }
Review Comment:
Fixed in 3ac738c. Added 14 ChangeDetection tests in FilteringUtilsTest
covering all 5 strategies (ALWAYS, NEVER, TIMESTAMP, CONTENT,
TIMESTAMP_AND_CONTENT) with both existing and non-existing destination files.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]