Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<properties>
<mavenVersion>3.9.12</mavenVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<plexusBuildApiVersion>0.0.7</plexusBuildApiVersion>
<version.plexus-utils>3.6.0</version.plexus-utils>
<project.build.outputTimestamp>2024-08-28T15:27:52Z</project.build.outputTimestamp>
<!-- don't fail check for some rules that are too hard to enforce (could even be told broken for some)
Expand All @@ -72,20 +71,31 @@
</properties>

<dependencies>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.sonatype.plexus</groupId>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-build-api</artifactId>
<version>${plexusBuildApiVersion}</version>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interpolation</artifactId>
<version>1.29</version>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand All @@ -105,25 +115,6 @@
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interpolation</artifactId>
<version>1.29</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.20.0</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
Expand Down Expand Up @@ -164,7 +155,12 @@
<artifactId>org.eclipse.sisu.inject</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.21.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.interpolation.Interpolator;

import static java.util.Objects.requireNonNull;

/**
* @since 1.0-beta-3
*/
Expand Down Expand Up @@ -83,6 +85,13 @@ public class AbstractMavenFilteringRequest {

private Consumer<Interpolator> interpolatorCustomizer;

/**
* Change detection strategy to determine whether an existing file should be overwritten.
*
* @since 3.5.0
*/
private ChangeDetection changeDetection = ChangeDetection.CONTENT;

/**
* Create instance.
*/
Expand Down Expand Up @@ -360,4 +369,23 @@ public Consumer<Interpolator> getInterpolatorCustomizer() {
public void setInterpolatorCustomizer(Consumer<Interpolator> interpolatorCustomizer) {
this.interpolatorCustomizer = interpolatorCustomizer;
}

/**
* Change detection strategy to determine whether an existing file should be overwritten.
*
* @since 3.5.0
*/
public ChangeDetection getChangeDetection() {
return changeDetection;
}

/**
* Sets the change detection strategy to determine whether an existing file should be overwritten.
*
* @param changeDetection the change detection strategy to use, must not be {@code null}.
* @since 3.5.0
*/
public void setChangeDetection(ChangeDetection changeDetection) {
this.changeDetection = requireNonNull(changeDetection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.shared.filtering;

/**
* Change detection strategies: to decide whether an <strong>existing target file</strong> needs to be overwritten or not.
*
* @since 3.5.0
*/
public enum ChangeDetection {
/**
* Only consider the file timestamp to determine is overwrite of existing file needed. This was default before 3.4.0.
*/
TIMESTAMP,
/**
* Only consider the content of the file to determine is overwrite of existing file needed. This is the default since 3.4.0.
*/
CONTENT,
/**
* Combine timestamp and content change detection for existing file.
*/
TIMESTAMP_AND_CONTENT,
/**
* Disable change detection; always overwrite existing file.
*/
ALWAYS,
/**
* Disable change detection; never overwrite existing file.
*/
NEVER;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public DefaultMavenFileFilter(BuildContext buildContext) {
this.buildContext = requireNonNull(buildContext);
}

@Deprecated
@Override
public void copyFile(
File from,
Expand All @@ -56,15 +57,40 @@ public void copyFile(
String encoding,
MavenSession mavenSession)
throws MavenFilteringException {
copyFile(
from,
to,
filtering,
mavenProject,
filters,
escapedBackslashesInFilePath,
encoding,
mavenSession,
ChangeDetection.CONTENT);
}

@Override
public void copyFile(
File from,
File to,
boolean filtering,
MavenProject mavenProject,
List<String> filters,
boolean escapedBackslashesInFilePath,
String encoding,
MavenSession mavenSession,
ChangeDetection changeDetection)
throws MavenFilteringException {
MavenResourcesExecution mre = new MavenResourcesExecution();
mre.setMavenProject(mavenProject);
mre.setFileFilters(filters);
mre.setEscapeWindowsPaths(escapedBackslashesInFilePath);
mre.setMavenSession(mavenSession);
mre.setInjectProjectBuildFilters(true);
mre.setChangeDetection(changeDetection);

List<FilterWrapper> filterWrappers = getDefaultFilterWrappers(mre);
copyFile(from, to, filtering, filterWrappers, encoding);
copyFile(from, to, filtering, filterWrappers, encoding, changeDetection);
}

@Override
Expand All @@ -76,24 +102,57 @@ public void copyFile(MavenFileFilterRequest mavenFileFilterRequest) throws Maven
mavenFileFilterRequest.getTo(),
mavenFileFilterRequest.isFiltering(),
filterWrappers,
mavenFileFilterRequest.getEncoding());
mavenFileFilterRequest.getEncoding(),
mavenFileFilterRequest.getChangeDetection());
}

@Deprecated
@Override
public void copyFile(File from, File to, boolean filtering, List<FilterWrapper> filterWrappers, String encoding)
throws MavenFilteringException {
copyFile(from, to, filtering, filterWrappers, encoding, ChangeDetection.CONTENT);
}

@Override
@Deprecated
public void copyFile(
File from,
File to,
boolean filtering,
List<FilterWrapper> filterWrappers,
String encoding,
boolean overwrite)
throws MavenFilteringException {
copyFile(
from,
to,
filtering,
filterWrappers,
encoding,
overwrite ? ChangeDetection.ALWAYS : ChangeDetection.CONTENT);
}

@Override
public void copyFile(
File from,
File to,
boolean filtering,
List<FilterWrapper> filterWrappers,
String encoding,
ChangeDetection changeDetection)
throws MavenFilteringException {
try {
if (filtering) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("filtering " + from.getPath() + " to " + to.getPath());
}
FilterWrapper[] array = filterWrappers.toArray(new FilterWrapper[0]);
FilteringUtils.copyFile(from, to, encoding, array, false);
FilteringUtils.copyFile(from, to, encoding, array, changeDetection);
} else {
if (getLogger().isDebugEnabled()) {
getLogger().debug("copy " + from.getPath() + " to " + to.getPath());
}
FilteringUtils.copyFile(from, to, encoding, new FilterWrapper[0], false);
FilteringUtils.copyFile(from, to, encoding, new FilterWrapper[0], changeDetection);
}

buildContext.refresh(to);
Expand All @@ -104,18 +163,4 @@ public void copyFile(File from, File to, boolean filtering, List<FilterWrapper>
e);
}
}

@Override
@Deprecated
public void copyFile(
File from,
File to,
boolean filtering,
List<FilterWrapper> filterWrappers,
String encoding,
boolean overwrite)
throws MavenFilteringException {
// overwrite forced to false to preserve backward comp
copyFile(from, to, filtering, filterWrappers, encoding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
import java.util.List;
import java.util.Locale;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Resource;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.Scanner;
import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.plexus.build.incremental.BuildContext;
Expand Down Expand Up @@ -93,7 +93,7 @@ public boolean filteredFileExtension(String fileName, List<String> userNonFilter
}

private static String getExtension(String fileName) {
String rawExt = FilenameUtils.getExtension(fileName);
String rawExt = FileUtils.getExtension(fileName);
return rawExt == null ? null : rawExt.toLowerCase(Locale.ROOT);
}

Expand Down Expand Up @@ -280,7 +280,7 @@ private File getTargetFile(File file) throws MavenFilteringException {
File destinationFile = getDestinationFile(outputDirectory, targetPath, name, mavenResourcesExecution);

if (mavenResourcesExecution.isFlatten() && destinationFile.exists()) {
if (mavenResourcesExecution.isOverwrite()) {
if (mavenResourcesExecution.getChangeDetection() == ChangeDetection.ALWAYS) {
LOGGER.warn("existing file " + destinationFile.getName() + " will be overwritten by " + name);
} else {
throw new MavenFilteringException("existing file " + destinationFile.getName()
Expand All @@ -303,7 +303,7 @@ private File getTargetFile(File file) throws MavenFilteringException {
resource.isFiltering() && filteredExt,
mavenResourcesExecution.getFilterWrappers(),
encoding,
mavenResourcesExecution.isOverwrite());
mavenResourcesExecution.getChangeDetection());
}

// deal with deleted source files
Expand Down Expand Up @@ -507,7 +507,7 @@ private String filterFileName(String name, List<FilterWrapper> wrappers) throws
}

try (StringWriter writer = new StringWriter()) {
IOUtils.copy(reader, writer);
IOUtil.copy(reader, writer);
String filteredFilename = writer.toString();

if (LOGGER.isDebugEnabled()) {
Expand Down
Loading