Skip to content

Commit

Permalink
Merge branch 'master' into m-api
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Mar 24, 2023
2 parents 4119137 + cd97ded commit 16bc5de
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 76 deletions.
41 changes: 10 additions & 31 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<mavenVersion>4.0.0-alpha-4</mavenVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<plexusBuildApiVersion>0.0.7</plexusBuildApiVersion>
<project.build.outputTimestamp>2022-06-11T13:41:41Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2023-03-21T10:54:20Z</project.build.outputTimestamp>
</properties>

<dependencies>
Expand Down Expand Up @@ -159,40 +159,19 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<configuration>
<excludes combine.children="append">
<exclude>src/test/units-files/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>reporting</id>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-changes-plugin</artifactId>
<configuration>
<columnNames>Type,Key,Summary,Assignee,Status,Resolution,Created</columnNames>
<maxEntries>200</maxEntries>
<onlyCurrentVersion>true</onlyCurrentVersion>
<sortColumnNames>Key</sortColumnNames>
<versionPrefix>maven-filtering-</versionPrefix>
<fixVersionIds>12335751</fixVersionIds>
<onlyCurrentVersion>true</onlyCurrentVersion>
</configuration>
<reportSets>
<reportSet>
<reports>
<report>jira-report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</profile>
</profiles>
</project>
12 changes: 2 additions & 10 deletions src/main/java/org/apache/maven/shared/filtering/BaseFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,16 +316,8 @@ public Object getValue(String expression) {
interpolator.setEscapeString(escapeString);

if (escapeWindowsPaths) {
interpolator.addPostProcessor(new InterpolationPostProcessor() {
@Override
public Object execute(String expression, Object value) {
if (value instanceof String) {
return FilteringUtils.escapeWindowsPath((String) value);
}

return value;
}
});
interpolator.addPostProcessor((expression, value) ->
(value instanceof String) ? FilteringUtils.escapeWindowsPath((String) value) : value);
}
return interpolator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,25 @@ public void filterResources(MavenResourcesExecution mavenResourcesExecution) thr

String[] includedFiles = scanner.getIncludedFiles();

LOGGER.info("Copying " + includedFiles.length + " resource" + (includedFiles.length > 1 ? "s" : "")
+ (targetPath == null ? "" : " to " + targetPath));
try {
Path basedir = mavenResourcesExecution
.getMavenProject()
.getBasedir()
.getAbsoluteFile()
.toPath();
Path destination = getDestinationFile(outputDirectory, targetPath, "", mavenResourcesExecution)
.getAbsoluteFile()
.toPath();
LOGGER.info("Copying " + includedFiles.size() + " resource" + (includedFiles.size() > 1 ? "s" : "")
+ " from "
+ basedir.relativize(resourceDirectory.getAbsoluteFile().toPath())
+ " to "
+ basedir.relativize(destination));
} catch (Exception e) {
// be foolproof: if for ANY reason throws, do not abort, just fall back to old message
LOGGER.info("Copying " + includedFiles.size() + " resource" + (includedFiles.size() > 1 ? "s" : "")
+ (targetPath == null ? "" : " to " + targetPath));
}

for (String name : includedFiles) {

Expand Down Expand Up @@ -270,9 +287,7 @@ public void filterResources(MavenResourcesExecution mavenResourcesExecution) thr

scanner.scan();

String[] deletedFiles = scanner.getIncludedFiles();

for (String name : deletedFiles) {
for (String name : scanner.getIncludedFiles()) {
Path destinationFile = getDestinationFile(outputDirectory, targetPath, name, mavenResourcesExecution);

try {
Expand Down Expand Up @@ -392,7 +407,7 @@ private Path getDestinationFile(
}

private String[] setupScanner(Resource resource, Scanner scanner, boolean addDefaultExcludes) {
String[] includes = null;
String[] includes;
if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) {
includes = resource.getIncludes().toArray(EMPTY_STRING_ARRAY);
} else {
Expand Down Expand Up @@ -429,9 +444,7 @@ private void copyDirectoryLayout(Path sourceDirectory, Path destinationDirectory
throw new IOException("Source directory doesn't exists (" + sourceDirectory.toAbsolutePath() + ").");
}

String[] includedDirectories = scanner.getIncludedDirectories();

for (String name : includedDirectories) {
for (String name : scanner.getIncludedDirectories()) {
Path source = sourceDirectory.resolve(name);

if (source.equals(sourceDirectory)) {
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static String escapeWindowsPath(String val) {
StringBuilder buf = new StringBuilder(val.length());
int start = 0, end = 0;
while ((end = val.indexOf('\\', start)) != -1) {
buf.append(val.substring(start, end)).append("\\\\");
buf.append(val, start, end).append("\\\\");
start = end + 1;

if (val.indexOf('\\', end + 1) == end + 1) {
Expand Down Expand Up @@ -321,9 +321,7 @@ public static void copyFile(Path from, Path to, String encoding, FilterWrapper[]
} else {
CharsetEncoder encoder = charset.newEncoder();

int totalBufferSize = FILE_COPY_BUFFER_SIZE;

int charBufferSize = (int) Math.floor(totalBufferSize / (2 + 2 * encoder.maxBytesPerChar()));
int charBufferSize = (int) Math.floor(FILE_COPY_BUFFER_SIZE / (2 + 2 * encoder.maxBytesPerChar()));
int byteBufferSize = (int) Math.ceil(charBufferSize * encoder.maxBytesPerChar());

CharBuffer newChars = CharBuffer.allocate(charBufferSize);
Expand All @@ -348,13 +346,17 @@ public static void copyFile(Path from, Path to, String encoding, FilterWrapper[]

if (!writing) {
existingRead = existing.read(existingBytes.array(), 0, newBytes.remaining());
((Buffer) existingBytes).position(existingRead);
((Buffer) existingBytes).flip();

if (newBytes.compareTo(existingBytes) != 0) {
writing = true;
if (existingRead > 0) {
existing.seek(existing.getFilePointer() - existingRead);
if (existingRead == -1) {
writing = true; // EOF
} else {
((Buffer) existingBytes).position(existingRead);
((Buffer) existingBytes).flip();

if (newBytes.compareTo(existingBytes) != 0) {
writing = true;
if (existingRead > 0) {
existing.seek(existing.getFilePointer() - existingRead);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public long skip(long n) throws IOException {
* @throws IOException If an I/O error occurs
*/
@Override
public int read(char cbuf[], int off, int len) throws IOException {
public int read(char[] cbuf, int off, int len) throws IOException {
for (int i = 0; i < len; i++) {
int ch = read();
if (ch == -1) {
Expand Down Expand Up @@ -301,7 +301,7 @@ public int read() throws IOException {
value = interpolator.interpolate(key.toString(), recursionInterceptor);
}
} catch (InterpolationException e) {
throw new IllegalArgumentException(e);
throw new IllegalArgumentException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public long skip(long n) throws IOException, IllegalArgumentException {
* @throws IOException If an I/O error occurs
*/
@Override
public int read(char cbuf[], int off, int len) throws IOException {
public int read(char[] cbuf, int off, int len) throws IOException {
for (int i = 0; i < len; i++) {
int ch = read();
if (ch == -1) {
Expand Down Expand Up @@ -220,7 +220,7 @@ public int read() throws IOException {
for (int i = 0; i < getEscapeString().length(); i++) {
key.append((char) ch);

if (ch != getEscapeString().charAt(i) || ch == -1 || (ch == '\n' && !supportMultiLineFiltering)) {
if (ch != getEscapeString().charAt(i) || ch == '\n' && !supportMultiLineFiltering) {
// mismatch, EOF or EOL, no escape string here
in.reset();
inEscape = false;
Expand All @@ -243,7 +243,7 @@ public int read() throws IOException {
}

for (int i = 0; i < begin.length(); i++) {
if (ch != begin.charAt(i) || ch == -1 || (ch == '\n' && !supportMultiLineFiltering)) {
if (ch != begin.charAt(i) || ch == '\n' && !supportMultiLineFiltering) {
// mismatch, EOF or EOL, no match
break;
}
Expand Down Expand Up @@ -326,7 +326,7 @@ public int read() throws IOException {
endToken = null;

// found endtoken? interpolate our key resolved above
String value = null;
String value;
if (end == 0) {
try {
if (interpolateWithPrefixPattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ private static String getPropertyValue(String k, Properties p, Logger logger) {

String v = p.getProperty(k);
String defaultValue = v;
String ret = "";
StringBuilder ret = new StringBuilder();
int idx, idx2;

while ((idx = v.indexOf("${")) >= 0) {
// append prefix to result
ret += v.substring(0, idx);
ret.append(v, 0, idx);

// strip prefix from original
v = v.substring(idx + 2);
Expand Down Expand Up @@ -219,7 +219,7 @@ private static String getPropertyValue(String k, Properties p, Logger logger) {
// resolved property ( so it can be parsed further )
// taking recursion into account.
if (nv == null || nv.equals(k) || k.equals(nk)) {
ret += "${" + nk + "}";
ret.append("${").append(nk).append("}");
} else {
v = nv + v;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class DefaultMavenResourcesFilteringTest {
PlexusContainer container;

private Path outputDirectory = Paths.get(getBasedir(), "target/DefaultMavenResourcesFilteringTest");
private Path baseDir = Paths.get("c:\\foo\\bar");
private Path baseDir = Paths.get(getBasedir());
private StubProject mavenProject = new StubProject(baseDir);
private MavenResourcesFiltering mavenResourcesFiltering;

Expand Down Expand Up @@ -444,7 +444,7 @@ public void testIncludeOneFileAndDirectory() throws Exception {

@Test
public void testFlattenDirectoryStructure() throws Exception {
Path baseDir = Paths.get("c:\\foo\\bar");
Path baseDir = Paths.get(getBasedir());
StubProject mavenProject = new StubProject(baseDir);
mavenProject.setVersion("1.0");
mavenProject.setGroupId("org.apache");
Expand Down Expand Up @@ -490,7 +490,7 @@ public void testFlattenDirectoryStructure() throws Exception {

@Test
public void testFlattenDirectoryStructureWithoutOverride() throws Exception {
Path baseDir = Paths.get("c:\\foo\\bar");
Path baseDir = Paths.get(getBasedir());
StubProject mavenProject = new StubProject(baseDir);
mavenProject.setVersion("1.0");
mavenProject.setGroupId("org.apache");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected void setUp() throws Exception {

@Test
public void testEscape() throws Exception {
Path baseDir = Paths.get("c:\\foo\\bar");
Path baseDir = Paths.get(getBasedir());
StubProject mavenProject = new StubProject(baseDir);
mavenProject.setVersion("1.0");
mavenProject.setGroupId("org.apache");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
Expand All @@ -28,7 +35,78 @@
* @since 1.0
*
*/
public class FilteringUtilsTest {
public class FilteringUtilsTest extends TestSupport {
private static File testDirectory = new File(getBasedir(), "target/test-classes/");

@Test
public void testMSHARED1213CopyWithTargetAlreadyExisting0ByteFile() throws IOException {
File fromFile = new File(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
File toFile = new File(testDirectory, "MSHARED-1213-enunciate.xml");
Files.write(toFile.toPath(), "".getBytes(StandardCharsets.UTF_8));
FilteringUtils.copyFile(
fromFile,
toFile,
"UTF-8",
new FilterWrapper[] {
new FilterWrapper() {
@Override
public Reader getReader(Reader fileReader) {
return fileReader;
}
}
},
false);
Assert.assertEquals(
Files.readAllLines(fromFile.toPath(), StandardCharsets.UTF_8),
Files.readAllLines(toFile.toPath(), StandardCharsets.UTF_8));
}

@Test
public void testMSHARED1213CopyWithTargetAlreadyExistingJunkFile() throws IOException {
File fromFile = new File(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
File toFile = new File(testDirectory, "MSHARED-1213-enunciate.xml");
Files.write(toFile.toPath(), "junk".getBytes(StandardCharsets.UTF_8));
FilteringUtils.copyFile(
fromFile,
toFile,
"UTF-8",
new FilterWrapper[] {
new FilterWrapper() {
@Override
public Reader getReader(Reader fileReader) {
return fileReader;
}
}
},
false);
Assert.assertEquals(
Files.readAllLines(fromFile.toPath(), StandardCharsets.UTF_8),
Files.readAllLines(toFile.toPath(), StandardCharsets.UTF_8));
}

@Test
public void testMSHARED1213CopyWithTargetAlreadyExistingSameFile() throws IOException {
File fromFile = new File(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
File toFile = new File(testDirectory, "MSHARED-1213-enunciate.xml");
Files.copy(fromFile.toPath(), toFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
FilteringUtils.copyFile(
fromFile,
toFile,
"UTF-8",
new FilterWrapper[] {
new FilterWrapper() {
@Override
public Reader getReader(Reader fileReader) {
return fileReader;
}
}
},
false);
Assert.assertEquals(
Files.readAllLines(fromFile.toPath(), StandardCharsets.UTF_8),
Files.readAllLines(toFile.toPath(), StandardCharsets.UTF_8));
}

@Test
public void testEscapeWindowsPathStartingWithDrive() {
assertEquals("C:\\\\Users\\\\Administrator", FilteringUtils.escapeWindowsPath("C:\\Users\\Administrator"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void setUp() throws Exception {
*/
@Test
public void testFilteringTokenOnce() throws Exception {
Path baseDir = Paths.get("c:\\foo\\bar");
Path baseDir = Paths.get(getBasedir());
StubProject mavenProject = new StubProject(baseDir);
mavenProject.setVersion("1.0");
mavenProject.setGroupId("org.apache");
Expand Down
Loading

0 comments on commit 16bc5de

Please sign in to comment.