This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-wagon.git
The following commit(s) were added to refs/heads/master by this push:
new f10f1797 Use JDK file APIs in SCM wagon and shared test utilities
f10f1797 is described below
commit f10f179711203f715eb6094cf94d38bb4e89e421
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Fri Nov 20 23:05:44 2026 +0100
Use JDK file APIs in SCM wagon and shared test utilities
Keep the read-only retry when deleting checkout directories: Git writes
loose
object files read-only, and Windows refuses to delete them until the flag is
cleared. Without it the SCM tests fail on the Windows JDK 8 job.
---
.../java/org/apache/maven/wagon/FileTestUtils.java | 111 ++++++++++++++++++++-
wagon-providers/wagon-scm/pom.xml | 4 -
.../apache/maven/wagon/providers/scm/ScmWagon.java | 108 +++++++++++++++++---
.../providers/scm/AbstractScmGitWagonTest.java | 8 +-
.../providers/scm/AbstractScmSvnWagonTest.java | 8 +-
.../wagon/providers/scm/AbstractScmWagonTest.java | 9 +-
.../ssh/AbstractEmbeddedScpWagonWithKeyTest.java | 23 +++--
7 files changed, 231 insertions(+), 40 deletions(-)
diff --git
a/wagon-provider-test/src/main/java/org/apache/maven/wagon/FileTestUtils.java
b/wagon-provider-test/src/main/java/org/apache/maven/wagon/FileTestUtils.java
index 80bcc69c..82837be3 100644
---
a/wagon-provider-test/src/main/java/org/apache/maven/wagon/FileTestUtils.java
+++
b/wagon-provider-test/src/main/java/org/apache/maven/wagon/FileTestUtils.java
@@ -22,8 +22,12 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
-
-import org.codehaus.plexus.util.FileUtils;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.attribute.BasicFileAttributes;
/**
* @author <a href="[email protected]>Michal Maczka</a>
@@ -54,7 +58,7 @@ public static File createDir(final String name) throws
IOException {
final File retValue = new File(baseDirectory, name);
if (retValue.exists()) {
- FileUtils.cleanDirectory(retValue);
+ cleanDirectory(retValue);
return retValue;
}
@@ -98,4 +102,105 @@ public static File generateFile(String file, String
content) throws IOException
return f;
}
+
+ /**
+ * Removes all files and sub-directories inside {@code directory}, leaving
the directory itself in place.
+ */
+ private static void cleanDirectory(File directory) throws IOException {
+ File[] contents = directory.listFiles();
+ if (contents == null) {
+ throw new IOException("Unable to list directory: " + directory);
+ }
+ for (File file : contents) {
+ if (file.isDirectory()) {
+ cleanDirectory(file);
+ }
+ if (!file.delete() && file.exists()) {
+ throw new IOException("Unable to delete: " + file);
+ }
+ }
+ }
+
+ /**
+ * Recursively deletes {@code directory}, including the directory itself.
Does nothing if it does not exist.
+ *
+ * @param directory the directory to delete
+ * @throws IOException if the directory cannot be listed or a file cannot
be deleted
+ */
+ public static void deleteDirectory(Path directory) throws IOException {
+ if (!Files.exists(directory)) {
+ return;
+ }
+ Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes
attrs) throws IOException {
+ delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException
exc) throws IOException {
+ if (exc != null) {
+ throw exc;
+ }
+ delete(dir);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+
+ private static void delete(Path path) throws IOException {
+ try {
+ Files.delete(path);
+ } catch (java.nio.file.AccessDeniedException e) {
+ // Git stores object files read-only on Windows; clear the flag
and retry once.
+ path.toFile().setWritable(true);
+ Files.delete(path);
+ }
+ }
+
+ /**
+ * Copies the contents of {@code source} into {@code target}, recreating
its directory structure and preserving
+ * file attributes. Existing files are overwritten.
+ *
+ * @param source the directory to copy
+ * @param target the destination directory
+ * @throws IOException if the source cannot be read or the target cannot
be written
+ */
+ public static void copyDirectory(Path source, Path target) throws
IOException {
+ Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
+
Files.createDirectories(target.resolve(source.relativize(dir)));
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes
attrs) throws IOException {
+ Files.copy(
+ file,
+ target.resolve(source.relativize(file)),
+ StandardCopyOption.REPLACE_EXISTING,
+ StandardCopyOption.COPY_ATTRIBUTES);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+
+ /**
+ * Creates {@code targetDirectory} if necessary and copies {@code source}
into it, preserving file attributes.
+ * An existing file with the same name is overwritten.
+ *
+ * @param source the file to copy
+ * @param targetDirectory the directory to copy the file into
+ * @throws IOException if the file cannot be copied
+ */
+ public static void copyFileToDirectory(Path source, Path targetDirectory)
throws IOException {
+ Files.createDirectories(targetDirectory);
+ Files.copy(
+ source,
+ targetDirectory.resolve(source.getFileName()),
+ StandardCopyOption.REPLACE_EXISTING,
+ StandardCopyOption.COPY_ATTRIBUTES);
+ }
}
diff --git a/wagon-providers/wagon-scm/pom.xml
b/wagon-providers/wagon-scm/pom.xml
index 43005dfb..4dabaccc 100644
--- a/wagon-providers/wagon-scm/pom.xml
+++ b/wagon-providers/wagon-scm/pom.xml
@@ -47,10 +47,6 @@ under the License.
<version>${mavenScmVersion}</version>
<scope>runtime</scope>
</dependency>
- <dependency>
- <groupId>org.codehaus.plexus</groupId>
- <artifactId>plexus-utils</artifactId>
- </dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
diff --git
a/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
b/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
index af6467fb..d39b6473 100644
---
a/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
+++
b/wagon-providers/wagon-scm/src/main/java/org/apache/maven/wagon/providers/scm/ScmWagon.java
@@ -23,6 +23,12 @@
import java.io.File;
import java.io.IOException;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.attribute.BasicFileAttributes;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
@@ -56,8 +62,6 @@
import org.apache.maven.wagon.authorization.AuthorizationException;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.resource.Resource;
-import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.StringUtils;
/**
* Wagon provider to get and put files from and to SCM systems, using
Maven-SCM as underlying transport.
@@ -238,7 +242,7 @@ private void removeCheckoutDirectory() throws
ConnectionException {
* take effect.</p>
*/
private ScmVersion makeScmVersion() {
- if (StringUtils.isBlank(scmVersion)) {
+ if (scmVersion == null || scmVersion.trim().isEmpty()) {
return null;
}
if (scmVersion.length() > 0) {
@@ -348,15 +352,15 @@ private void putInternal(File source, String targetName)
throws TransferFailedEx
File newCheckoutDirectory = new File(checkoutDirectory, relPath);
- File scmFile = new File(newCheckoutDirectory, isDirectory ? "" :
FileUtils.removePath(targetName, '/'));
+ File scmFile = new File(newCheckoutDirectory, isDirectory ? "" :
removePath(targetName, '/'));
boolean fileAlreadyInScm = scmFile.exists();
if (!scmFile.equals(source)) {
if (isDirectory) {
- FileUtils.copyDirectoryStructure(source, scmFile);
+ copyDirectoryStructure(source, scmFile);
} else {
- FileUtils.copyFile(source, scmFile);
+ copyFile(source, scmFile);
}
}
@@ -452,7 +456,7 @@ private String ensureDirs(
boolean added = false;
try {
mkdirsThrow(deepDir, createdDirs);
- if (createdDirs.size() != 0) {
+ if (!createdDirs.isEmpty()) {
File topNewDir = createdDirs.get(0);
String relTopNewDir = topNewDir
.getPath()
@@ -467,8 +471,8 @@ private String ensureDirs(
throw new TransferFailedException("Failed to add directory " +
createdDirs.get(0) + " to working copy", e);
} finally {
- if (!added && createdDirs.size() != 0) {
- FileUtils.deleteDirectory(createdDirs.get(0));
+ if (!added && !createdDirs.isEmpty()) {
+ deleteDirectory(createdDirs.get(0));
}
}
@@ -643,7 +647,7 @@ public void get(String resourceName, File destination)
}
if (!scmFile.equals(destination)) {
- FileUtils.copyFile(scmFile, destination);
+ copyFile(scmFile, destination);
}
} catch (ScmException e) {
fireTransferError(resource, e, TransferEvent.REQUEST_GET);
@@ -698,7 +702,7 @@ private ScmResult tryPartialCheckout(String subdir, boolean
recursiveArg) throws
private void deleteCheckoutDirectory() throws IOException {
haveRecursiveCO = false;
- FileUtils.deleteDirectory(checkoutDirectory);
+ deleteDirectory(checkoutDirectory);
}
private boolean checkoutDirExists(ScmProvider scmProvider) {
@@ -748,6 +752,86 @@ public boolean resourceExists(String resourceName) throws
TransferFailedExceptio
}
private String getDirname(String resourceName) {
- return FileUtils.getPath(resourceName, '/');
+ return getPath(resourceName, '/');
+ }
+
+ /** Returns everything after the last {@code separator}, or the whole
string when there is none. */
+ private static String removePath(String path, char separator) {
+ int index = path.lastIndexOf(separator);
+ return index < 0 ? path : path.substring(index + 1);
+ }
+
+ /** Returns everything before the last {@code separator} (without it), or
the empty string. */
+ private static String getPath(String path, char separator) {
+ int index = path.lastIndexOf(separator);
+ return index < 0 ? "" : path.substring(0, index);
+ }
+
+ /** JDK replacement for {@code FileUtils.copyFile}: creates parent dirs
and copies file attributes. */
+ private static void copyFile(File source, File destination) throws
IOException {
+ File parent = destination.getParentFile();
+ if (parent != null) {
+ Files.createDirectories(parent.toPath());
+ }
+ Files.copy(
+ source.toPath(),
+ destination.toPath(),
+ StandardCopyOption.REPLACE_EXISTING,
+ StandardCopyOption.COPY_ATTRIBUTES);
+ }
+
+ /** JDK replacement for {@code FileUtils.copyDirectoryStructure}. */
+ private static void copyDirectoryStructure(final File source, final File
target) throws IOException {
+ final Path sourcePath = source.toPath();
+ Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
+
Files.createDirectories(target.toPath().resolve(sourcePath.relativize(dir)));
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes
attrs) throws IOException {
+ Files.copy(
+ file,
+ target.toPath().resolve(sourcePath.relativize(file)),
+ StandardCopyOption.REPLACE_EXISTING,
+ StandardCopyOption.COPY_ATTRIBUTES);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+
+ /** JDK replacement for {@code FileUtils.deleteDirectory}. */
+ private static void deleteDirectory(File directory) throws IOException {
+ if (!directory.exists()) {
+ return;
+ }
+ Files.walkFileTree(directory.toPath(), new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes
attrs) throws IOException {
+ delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException
exc) throws IOException {
+ if (exc != null) {
+ throw exc;
+ }
+ delete(dir);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+
+ private static void delete(Path path) throws IOException {
+ try {
+ Files.delete(path);
+ } catch (java.nio.file.AccessDeniedException e) {
+ // Git stores object files read-only on Windows; clear the flag
and retry once.
+ path.toFile().setWritable(true);
+ Files.delete(path);
+ }
}
}
diff --git
a/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmGitWagonTest.java
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmGitWagonTest.java
index d8be4429..8a5eafb0 100644
---
a/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmGitWagonTest.java
+++
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmGitWagonTest.java
@@ -20,9 +20,11 @@
import java.io.File;
-import org.codehaus.plexus.util.FileUtils;
import org.junit.jupiter.api.BeforeEach;
+import static org.apache.maven.wagon.FileTestUtils.copyDirectory;
+import static org.apache.maven.wagon.FileTestUtils.deleteDirectory;
+
/**
* Test for ScmWagon using Git as underlying SCM
*/
@@ -39,9 +41,9 @@ public void setUp() throws Exception {
File testRepo = getTestFile("target/test-classes/test-repo-git-test");
- FileUtils.deleteDirectory(testRepo);
+ deleteDirectory(testRepo.toPath());
- FileUtils.copyDirectoryStructure(origRepo, testRepo);
+ copyDirectory(origRepo.toPath(), testRepo.toPath());
repository = "scm:git:" +
testRepo.getAbsoluteFile().toPath().toUri().toASCIIString();
}
diff --git
a/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmSvnWagonTest.java
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmSvnWagonTest.java
index 37946ba4..04694783 100644
---
a/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmSvnWagonTest.java
+++
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmSvnWagonTest.java
@@ -20,9 +20,11 @@
import java.io.File;
-import org.codehaus.plexus.util.FileUtils;
import org.junit.jupiter.api.BeforeEach;
+import static org.apache.maven.wagon.FileTestUtils.copyDirectory;
+import static org.apache.maven.wagon.FileTestUtils.deleteDirectory;
+
/**
* Test for ScmWagon using SVN as underlying SCM
*
@@ -42,9 +44,9 @@ public void setUp() throws Exception {
File testRepo = getTestFile("target/test-classes/test-repo-svn-test");
- FileUtils.deleteDirectory(testRepo);
+ deleteDirectory(testRepo.toPath());
- FileUtils.copyDirectoryStructure(origRepo, testRepo);
+ copyDirectory(origRepo.toPath(), testRepo.toPath());
repository = "scm:svn:" +
testRepo.getAbsoluteFile().toPath().toUri().toASCIIString();
}
diff --git
a/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmWagonTest.java
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmWagonTest.java
index ebfe4395..afe72af9 100644
---
a/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmWagonTest.java
+++
b/wagon-providers/wagon-scm/src/test/java/org/apache/maven/wagon/providers/scm/AbstractScmWagonTest.java
@@ -37,10 +37,11 @@
import org.apache.maven.wagon.authorization.AuthorizationException;
import org.apache.maven.wagon.repository.Repository;
import org.apache.maven.wagon.resource.Resource;
-import org.codehaus.plexus.util.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import static org.apache.maven.wagon.FileTestUtils.deleteDirectory;
+
/**
* Test for {@link ScmWagon}. You need a subclass for each SCM provider you
want to test.
*
@@ -66,7 +67,7 @@ public void testWagonPutDirectory() throws Exception {
public void setUp() throws Exception {
super.setUp();
- FileUtils.deleteDirectory(getCheckoutDirectory());
+ deleteDirectory(getCheckoutDirectory().toPath());
if (wagon == null) {
wagon = (ScmWagon) super.getWagon();
@@ -147,12 +148,12 @@ protected String getProtocol() {
protected void createDirectory(Wagon wagon, String resourceToCreate,
String dirName) throws Exception {
super.createDirectory(wagon, resourceToCreate, dirName);
- FileUtils.deleteDirectory(getCheckoutDirectory());
+ deleteDirectory(getCheckoutDirectory().toPath());
}
protected void assertResourcesAreInRemoteSide(Wagon wagon, List<String>
resourceNames)
throws IOException, TransferFailedException,
ResourceDoesNotExistException, AuthorizationException {
- FileUtils.deleteDirectory(getCheckoutDirectory());
+ deleteDirectory(getCheckoutDirectory().toPath());
super.assertResourcesAreInRemoteSide(wagon, resourceNames);
}
}
diff --git
a/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/AbstractEmbeddedScpWagonWithKeyTest.java
b/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/AbstractEmbeddedScpWagonWithKeyTest.java
index 48e331d4..3cdf3e20 100644
---
a/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/AbstractEmbeddedScpWagonWithKeyTest.java
+++
b/wagon-providers/wagon-ssh-common-test/src/main/java/org/apache/maven/wagon/providers/ssh/AbstractEmbeddedScpWagonWithKeyTest.java
@@ -19,6 +19,7 @@
package org.apache.maven.wagon.providers.ssh;
import java.io.File;
+import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
@@ -27,10 +28,13 @@
import org.apache.maven.wagon.authentication.AuthenticationInfo;
import org.apache.maven.wagon.repository.Repository;
import org.apache.maven.wagon.resource.Resource;
-import org.codehaus.plexus.util.FileUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import static org.apache.maven.wagon.FileTestUtils.copyDirectory;
+import static org.apache.maven.wagon.FileTestUtils.copyFileToDirectory;
+import static org.apache.maven.wagon.FileTestUtils.deleteDirectory;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -101,23 +105,23 @@ protected boolean supportsGetIfNewer() {
}
@Test
- public void testWithSpaces() throws Exception {
+ public void testWithSpaces(@TempDir Path tempDir) throws Exception {
String dir = "foo test";
File spaceDirectory = new File(TestData.getRepoPath(), dir);
if (spaceDirectory.exists()) {
- FileUtils.deleteDirectory(spaceDirectory);
+ deleteDirectory(spaceDirectory.toPath());
}
spaceDirectory.mkdirs();
String subDir = "foo bar";
File sub = new File(spaceDirectory, subDir);
if (sub.exists()) {
- FileUtils.deleteDirectory(sub);
+ deleteDirectory(sub.toPath());
}
sub.mkdirs();
File dummy = new File("src/test/resources/dummy.txt");
- FileUtils.copyFileToDirectory(dummy, sub);
+ copyFileToDirectory(dummy.toPath(), sub.toPath());
String url = getTestRepositoryUrl() + "/" + dir;
Repository repo = new Repository("foo", url);
@@ -136,15 +140,12 @@ public void testWithSpaces() throws Exception {
assertTrue(files.contains("dummy.txt"));
assertTrue(files.contains("newdummy.txt"));
- File sourceWithSpace = new File("target/directory with spaces");
- if (sourceWithSpace.exists()) {
- FileUtils.deleteDirectory(sourceWithSpace);
- }
+ Path sourceWithSpace = tempDir.resolve("directory with spaces");
File resources = new File("src/test/resources");
- FileUtils.copyDirectory(resources, sourceWithSpace);
+ copyDirectory(resources.toPath(), sourceWithSpace);
- wagon.putDirectory(sourceWithSpace, "target with spaces");
+ wagon.putDirectory(sourceWithSpace.toFile(), "target with spaces");
files = wagon.getFileList("target with spaces");