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
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -476,26 +478,38 @@ private String getRelativeOutputDirectory(MavenResourcesExecution execution) {
*/
private String filterFileName(String name, List<FilterWrapper> wrappers) throws MavenFilteringException {

Reader reader = new StringReader(name);
for (FilterWrapper wrapper : wrappers) {
reader = wrapper.getReader(reader);
}

try (StringWriter writer = new StringWriter()) {
char[] buffer = new char[BUFFER_LENGTH];
int nRead;
while ((nRead = reader.read(buffer, 0, buffer.length)) >= 0) {
writer.write(buffer, 0, nRead);
char[] buffer = new char[BUFFER_LENGTH];
StringBuilder sb = new StringBuilder();
Path path = Path.of(name);
Iterator<Path> iterator = path.iterator();
while (iterator.hasNext()) {
String component = iterator.next().toString();
Reader reader = new StringReader(component);
for (FilterWrapper wrapper : wrappers) {
reader = wrapper.getReader(reader);
}

String filteredFilename = writer.toString();
try (StringWriter writer = new StringWriter()) {
int nRead;
while ((nRead = reader.read(buffer, 0, buffer.length)) >= 0) {
writer.write(buffer, 0, nRead);
}
String filteredComponent = writer.toString();
sb.append(filteredComponent);
if (iterator.hasNext()) {
sb.append(FileSystems.getDefault().getSeparator());
}

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("renaming filename " + name + " to " + filteredFilename);
} catch (IOException e) {
throw new MavenFilteringException("Failed filtering filename" + name, e);
}
return filteredFilename;
} catch (IOException e) {
throw new MavenFilteringException("Failed filtering filename" + name, e);
}

String filteredFilename = sb.toString();

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("renaming filename " + name + " to " + filteredFilename);
}
return filteredFilename;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -925,7 +926,7 @@ public void testFilterFileName() throws Exception {
Resource resource = new Resource();
resource.setDirectory(unitFilesDir);
resource.setFiltering(true);
resource.addInclude("${pom.version}*");
resource.addInclude("**/${pom.version}*");
resource.setTargetPath("testTargetPath");

MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(
Expand All @@ -943,6 +944,49 @@ public void testFilterFileName() throws Exception {

List<Path> files = list(targetPathFile);
assertEquals(1, files.size());
assertEquals("subfolder", filename(files.get(0)));
assertTrue(Files.isDirectory(files.get(0)));

files = list(files.get(0));
assertEquals(1, files.size());
assertEquals("1.0.txt", filename(files.get(0)));
}

@Test
public void testFilterFileNameWithFileSeparatorAsEscape() throws Exception {

String unitFilesDir = getBasedir() + "/src/test/units-files/maven-filename-filtering";

Resource resource = new Resource();
resource.setDirectory(unitFilesDir);
resource.setFiltering(true);
resource.addInclude("**/${pom.version}*");
resource.setTargetPath("testTargetPath");

MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(
Collections.singletonList(resource),
outputDirectory,
mavenProject,
"UTF-8",
Collections.<String>emptyList(),
Collections.<String>emptyList(),
new StubSession());
mavenResourcesExecution.setFilterFilenames(true);

// more likely to occur on windows, where the file
// separator is the same as the common escape string "\"
mavenResourcesExecution.setEscapeString(FileSystems.getDefault().getSeparator());
mavenResourcesFiltering.filterResources(mavenResourcesExecution);

Path targetPathFile = outputDirectory.resolve("testTargetPath");

List<Path> files = list(targetPathFile);
assertEquals(1, files.size());
assertEquals("subfolder", filename(files.get(0)));
assertTrue(Files.isDirectory(files.get(0)));

files = list(files.get(0));
assertEquals(1, files.size());
assertEquals("1.0.txt", filename(files.get(0)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# 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.
# 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.
Loading