Skip to content

Commit 01f36a7

Browse files
committed
[MSHARED-1351] - Fix console message when origin is baseDir
Print alternative message replacing the relative origin path by the literal 'base directory'.
1 parent ed9d2df commit 01f36a7

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import org.apache.commons.io.FilenameUtils;
3838
import org.apache.commons.io.IOUtils;
39+
import org.apache.commons.lang3.StringUtils;
3940
import org.apache.maven.model.Resource;
4041
import org.codehaus.plexus.util.DirectoryScanner;
4142
import org.codehaus.plexus.util.Scanner;
@@ -254,9 +255,15 @@ private File getTargetFile(File file) throws MavenFilteringException {
254255
Path destination = getDestinationFile(outputDirectory, targetPath, "", mavenResourcesExecution)
255256
.getAbsoluteFile()
256257
.toPath();
258+
String origin = basedir.relativize(
259+
resourceDirectory.getAbsoluteFile().toPath())
260+
.toString();
261+
if (StringUtils.isEmpty(origin)) {
262+
origin = ".";
263+
}
257264
LOGGER.info("Copying " + includedFiles.size() + " resource" + (includedFiles.size() > 1 ? "s" : "")
258265
+ " from "
259-
+ basedir.relativize(resourceDirectory.getAbsoluteFile().toPath())
266+
+ origin
260267
+ " to "
261268
+ basedir.relativize(destination));
262269
} catch (Exception e) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.shared.filtering;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.OutputStream;
24+
import java.io.PrintStream;
25+
26+
/**
27+
* Helping class to capture console input and output for tests.
28+
*
29+
* @author abelsromero
30+
* @since 3.3.2
31+
*/
32+
class ConsoleHolder {
33+
34+
private PrintStream originalOut;
35+
private PrintStream originalErr;
36+
37+
private ByteArrayOutputStream newOut;
38+
private ByteArrayOutputStream newErr;
39+
40+
private ConsoleHolder() {}
41+
42+
static ConsoleHolder start() {
43+
final ConsoleHolder holder = new ConsoleHolder();
44+
45+
holder.originalOut = System.out;
46+
holder.originalErr = System.err;
47+
48+
holder.newOut = new DoubleOutputStream(holder.originalOut);
49+
holder.newErr = new DoubleOutputStream(holder.originalErr);
50+
51+
System.setOut(new PrintStream(holder.newOut));
52+
System.setErr(new PrintStream(holder.newErr));
53+
54+
return holder;
55+
}
56+
57+
void release() {
58+
System.setOut(originalOut);
59+
System.setOut(originalErr);
60+
}
61+
62+
String getOutput() {
63+
return new String(newOut.toByteArray());
64+
}
65+
66+
String getError() {
67+
return new String(newErr.toByteArray());
68+
}
69+
70+
static class DoubleOutputStream extends ByteArrayOutputStream {
71+
72+
final OutputStream other;
73+
74+
DoubleOutputStream(final OutputStream os) {
75+
other = os;
76+
}
77+
78+
@Override
79+
public synchronized void write(final byte[] b, final int off, final int len) {
80+
try {
81+
other.write(b, off, len);
82+
} catch (IOException e) {
83+
throw new RuntimeException(e);
84+
}
85+
super.write(b, off, len);
86+
}
87+
}
88+
}

src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Properties;
3232

3333
import org.apache.commons.io.FileUtils;
34+
import org.apache.commons.lang3.StringUtils;
3435
import org.apache.maven.execution.MavenSession;
3536
import org.apache.maven.model.Resource;
3637
import org.apache.maven.settings.Settings;
@@ -366,6 +367,57 @@ void noFiltering() throws Exception {
366367
assertTrue(filesAreIdentical(initialImageFile, imageFile));
367368
}
368369

370+
@Test
371+
void messageWhenCopyingFromSubDirectory() throws Exception {
372+
373+
String subDirectory = "src/test/units-files/maven-resources-filtering";
374+
String unitFilesDir = String.format("%s/%s", getBasedir(), subDirectory);
375+
376+
assertMessage(
377+
unitFilesDir,
378+
"Copying (\\d)+ resources from " + subDirectory + " to target/DefaultMavenResourcesFilteringTest");
379+
}
380+
381+
@Test
382+
void messageWhenCopyingFromBaseDir() throws Exception {
383+
384+
String unitFilesDir = getBasedir();
385+
386+
assertMessage(unitFilesDir, "Copying (\\d)+ resources from . to target/DefaultMavenResourcesFilteringTest");
387+
}
388+
389+
private void assertMessage(String directory, String expectedMessagePattern) throws Exception {
390+
Resource resource = new Resource();
391+
List<Resource> resources = new ArrayList<>();
392+
resources.add(resource);
393+
394+
resource.setDirectory(directory);
395+
resource.setFiltering(false);
396+
397+
MavenResourcesExecution mre = new MavenResourcesExecution();
398+
mre.setResources(resources);
399+
mre.setOutputDirectory(outputDirectory);
400+
mre.setEncoding("UTF-8");
401+
mre.setMavenProject(mavenProject);
402+
mre.setFilters(null);
403+
mre.setNonFilteredFileExtensions(Collections.emptyList());
404+
mre.setMavenSession(new StubMavenSession());
405+
406+
ConsoleHolder console = ConsoleHolder.start();
407+
408+
mavenResourcesFiltering.filterResources(mre);
409+
410+
String output = console.getError();
411+
String marker = DefaultMavenResourcesFiltering.class.getSimpleName();
412+
String message = output.substring(output.indexOf(marker) + marker.length() + 3)
413+
.trim()
414+
.replaceAll("\\\\", "/");
415+
416+
boolean matches = message.matches(expectedMessagePattern);
417+
assertTrue(matches, "expected: '" + expectedMessagePattern + "' does not match actual: '" + message + "'");
418+
console.release();
419+
}
420+
369421
private static boolean filesAreIdentical(File expected, File current) throws IOException {
370422
if (expected.length() != current.length()) {
371423
return false;

0 commit comments

Comments
 (0)