mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-28 08:47:29 +00:00
Make sure the current directory is changed when processing a new maven request, fixes #397
This commit is contained in:
@@ -24,6 +24,8 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.lang.reflect.Field;
|
||||
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;
|
||||
@@ -206,7 +208,7 @@ public class DaemonMavenCli {
|
||||
try {
|
||||
CliRequest req = new CliRequest(null, null);
|
||||
req.args = arguments.toArray(new String[0]);
|
||||
req.workingDirectory = workingDirectory;
|
||||
req.workingDirectory = new File(workingDirectory).getCanonicalPath();
|
||||
req.multiModuleProjectDirectory = new File(projectDirectory);
|
||||
return doMain(req, clientEnv);
|
||||
} finally {
|
||||
@@ -684,8 +686,7 @@ public class DaemonMavenCli {
|
||||
CLibrary.setenv(key, vr);
|
||||
}
|
||||
setEnv(clientEnv);
|
||||
CLibrary.chdir(workingDir);
|
||||
System.setProperty("user.dir", workingDir);
|
||||
chDir(workingDir);
|
||||
} catch (Exception e) {
|
||||
slf4jLogger.warn("Environment mismatch ! Could not set the environment (" + e + ")");
|
||||
slf4jLogger.warn("A few environment mismatches have been detected between the client and the daemon.");
|
||||
@@ -699,6 +700,41 @@ public class DaemonMavenCli {
|
||||
}
|
||||
}
|
||||
|
||||
protected static void chDir(String workingDir) throws Exception {
|
||||
CLibrary.chdir(workingDir);
|
||||
System.setProperty("user.dir", workingDir);
|
||||
// change current dir for the java.io.File class
|
||||
Class<?> fileClass = Class.forName("java.io.File");
|
||||
Field fsField = fileClass.getDeclaredField("fs");
|
||||
fsField.setAccessible(true);
|
||||
Object fs = fsField.get(null);
|
||||
Field userDirField = fs.getClass().getDeclaredField("userDir");
|
||||
userDirField.setAccessible(true);
|
||||
userDirField.set(fs, workingDir);
|
||||
// change current dir for the java.nio.Path class
|
||||
fs = FileSystems.getDefault();
|
||||
Class<?> fsClass = fs.getClass();
|
||||
while (fsClass != Object.class) {
|
||||
if ("sun.nio.fs.UnixFileSystem".equals(fsClass.getName())) {
|
||||
Field defaultDirectoryField = fsClass.getDeclaredField("defaultDirectory");
|
||||
defaultDirectoryField.setAccessible(true);
|
||||
String encoding = System.getProperty("sun.jnu.encoding");
|
||||
Charset charset = encoding != null ? Charset.forName(encoding) : Charset.defaultCharset();
|
||||
defaultDirectoryField.set(fs, workingDir.getBytes(charset));
|
||||
} else if ("sun.nio.fs.WindowsFileSystem".equals(fsClass.getName())) {
|
||||
Field defaultDirectoryField = fsClass.getDeclaredField("defaultDirectory");
|
||||
Field defaultRootField = fsClass.getDeclaredField("defaultRoot");
|
||||
defaultDirectoryField.setAccessible(true);
|
||||
defaultRootField.setAccessible(true);
|
||||
Path wdir = Paths.get(workingDir);
|
||||
Path root = wdir.getRoot();
|
||||
defaultDirectoryField.set(fs, wdir.toString());
|
||||
defaultRootField.set(fs, root.toString());
|
||||
}
|
||||
fsClass = fsClass.getSuperclass();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected static void setEnv(Map<String, String> newenv) throws Exception {
|
||||
try {
|
||||
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
* Licensed 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.cli;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Paths;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DaemonMavenCliTest {
|
||||
|
||||
@Test
|
||||
void testChdir() throws Exception {
|
||||
File d = new File("target/tstDir").getAbsoluteFile();
|
||||
d.mkdirs();
|
||||
DaemonMavenCli.chDir(d.toString());
|
||||
assertEquals(new File(d, "test").getAbsolutePath(), new File("test").getAbsolutePath());
|
||||
assertEquals(d.toPath().resolve("test").toAbsolutePath().toString(),
|
||||
Paths.get("test").toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user