Handle the multimodule project directory as early as possible in the client (fixes #694) (#697)

This commit is contained in:
Guillaume Nodet
2022-10-03 22:46:09 +02:00
committed by GitHub
parent 48ca793014
commit aacd3eb4cc
8 changed files with 52 additions and 149 deletions

View File

@@ -116,6 +116,19 @@ public class DefaultClient implements Client {
System.setProperty(Environment.MVND_HOME.getProperty(), parameters.mvndHome().toString());
Path dir;
if (Environment.MAVEN_FILE.hasCommandLineOption(args)) {
dir = parameters.userDir().resolve(Environment.MAVEN_FILE.getCommandLineOption(args));
if (Files.isRegularFile(dir)) {
dir = dir.getParent();
}
dir = dir.normalize();
} else {
dir = parameters.userDir();
}
System.setProperty(Environment.MAVEN_MULTIMODULE_PROJECT_DIRECTORY.getProperty(),
parameters.multiModuleProjectDirectory(dir).toString());
// .mvn/jvm.config
if (Files.isRegularFile(parameters.jvmConfigPath())) {
try (Stream<String> jvmArgs = Files.lines(parameters.jvmConfigPath())) {
@@ -293,17 +306,6 @@ public class DefaultClient implements Client {
});
Environment.MVND_TERMINAL_WIDTH.addCommandLineOption(args, width);
Path dir;
if (Environment.MAVEN_FILE.hasCommandLineOption(args)) {
dir = parameters.userDir().resolve(Environment.MAVEN_FILE.getCommandLineOption(args));
if (Files.isRegularFile(dir)) {
dir = dir.getParent();
}
dir = dir.normalize();
} else {
dir = parameters.userDir();
}
final DaemonConnector connector = new DaemonConnector(parameters, registry);
try (DaemonClientConnection daemon = connector.connect(output)) {
output.setDaemonId(daemon.getDaemon().getId());
@@ -313,7 +315,7 @@ public class DefaultClient implements Client {
daemon.dispatch(new Message.BuildRequest(
args,
parameters.userDir().toString(),
parameters.multiModuleProjectDirectory(dir).toString(),
parameters.multiModuleProjectDirectory().toString(),
System.getenv()));
output.accept(Message

View File

@@ -78,88 +78,6 @@ goto error
set MAVEN_CMD_LINE_ARGS=%*
@REM Find the project basedir, i.e., the directory that contains the folder ".mvn".
@REM Fallback to current working directory if not found.
set "MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%"
if not "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
set "EXEC_DIR=%CD%"
set "WDIR=%EXEC_DIR%"
@REM Look for the --file switch and start the search for the .mvn directory from the specified
@REM POM location, if supplied.
set FILE_ARG=
:arg_loop
if "%~1" == "-f" (
set "FILE_ARG=%~2"
shift
goto process_file_arg
)
if "%~1" == "--file" (
set "FILE_ARG=%~2"
shift
goto process_file_arg
)
@REM If none of the above, skip the argument
shift
if not "%~1" == "" (
goto arg_loop
) else (
goto findBaseDir
)
:process_file_arg
if "%FILE_ARG%" == "" (
goto findBaseDir
)
if not exist "%FILE_ARG%" (
echo POM file "%FILE_ARG%" specified the -f/--file command-line argument does not exist >&2
goto error
)
if exist "%FILE_ARG%\*" (
set "POM_DIR=%FILE_ARG%"
) else (
call :get_directory_from_file "%FILE_ARG%"
)
if not exist "%POM_DIR%" (
echo Directory "%POM_DIR%" extracted from the -f/--file command-line argument "%FILE_ARG%" does not exist >&2
goto error
)
set "WDIR=%POM_DIR%"
goto findBaseDir
:get_directory_from_file
set "POM_DIR=%~dp1"
:stripPomDir
if not "_%POM_DIR:~-1%"=="_\" goto pomDirStripped
set "POM_DIR=%POM_DIR:~0,-1%"
goto stripPomDir
:pomDirStripped
exit /b
:findBaseDir
cd /d "%WDIR%"
:findBaseDirLoop
if exist "%WDIR%\.mvn" goto baseDirFound
cd ..
IF "%WDIR%"=="%CD%" goto baseDirNotFound
set "WDIR=%CD%"
goto findBaseDirLoop
:baseDirFound
set "MAVEN_PROJECTBASEDIR=%WDIR%"
cd /d "%EXEC_DIR%"
goto endDetectBaseDir
:baseDirNotFound
if "_%EXEC_DIR:~-1%"=="_\" set "EXEC_DIR=%EXEC_DIR:~0,-1%"
set "MAVEN_PROJECTBASEDIR=%EXEC_DIR%"
cd "%EXEC_DIR%"
:endDetectBaseDir
@setlocal EnableExtensions EnableDelayedExpansion
for %%i in ("%MVND_HOME%"\mvn\boot\*.jar "%MVND_HOME%"\mvn\lib\ext\*.jar "%MVND_HOME%"\mvn\lib\*.jar) do set DAEMON_JAR=!DAEMON_JAR!;%%i
@endlocal & set DAEMON_JAR="%DAEMON_JAR%"

View File

@@ -118,23 +118,6 @@ if $cygwin ; then
DAEMON_JAR=`cygpath --path --windows "$DAEMON_JAR"`
fi
# traverses directory structure from process work directory to filesystem root
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
(
basedir=`find_file_argument_basedir "$@"`
wdir="${basedir}"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
basedir=$wdir
break
fi
wdir=`cd "$wdir/.."; pwd`
done
echo "${basedir}"
)
}
find_file_argument_basedir() {
(
basedir=`pwd`
@@ -172,17 +155,6 @@ concat_lines() {
fi
}
MAVEN_PROJECTBASEDIR="${MAVEN_BASEDIR:-`find_maven_basedir "$@"`}"
# For Cygwin, switch project base directory path to Windows format before
# executing Maven otherwise this will cause Maven not to consider it.
if $cygwin ; then
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
fi
export MAVEN_PROJECTBASEDIR
# Provide a "standardized" way to retrieve the CLI args that will
# work with both Windows and non-Windows executions.
MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
@@ -196,5 +168,4 @@ exec "$JAVACMD" \
"-Dmvnd.home=${MVND_HOME}" \
"-Dmaven.home=${MVND_HOME}/mvn" \
"-Dlibrary.jansi.path=${MVND_HOME}/mvn/lib/jansi-native" \
"-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
${DAEMON_LAUNCHER} "$@"

View File

@@ -27,6 +27,13 @@ import org.mvndaemon.mvnd.junit.TestParameters;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* NOTE: there is no related JVM test because the support for the maven
* multiModuleProjectDiscovery is done very early in the client in order
* to be able to load the JVM parameters from the .mvn/jvm.config file.
* Thus, there's point in setting because it would have to be done
* in the test iself.
*/
@MvndNativeTest(projectDir = "src/test/projects/specific-file")
public class SpecificFileNativeIT {
@@ -49,6 +56,12 @@ public class SpecificFileNativeIT {
"-Dexpression=maven.multiModuleProjectDirectory", "-f", "../prj2/pom.xml", "-e").assertSuccess();
assertTrue(output.getMessages().stream()
.anyMatch(m -> m.toString().contains(prj2.toString())), "Output should contain " + prj2);
output.getMessages().clear();
cl.execute(output, "org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate",
"-Dexpression=foo-bar", "-f", "../prj2/pom.xml", "-e").assertSuccess();
assertTrue(output.getMessages().stream()
.anyMatch(m -> m.toString().contains("xx-prj2-xx")), "Output should contain " + prj2);
}
protected boolean isNative() {

View File

@@ -1,26 +0,0 @@
/*
* 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.mvndaemon.mvnd.it;
import org.mvndaemon.mvnd.junit.MvndTest;
@MvndTest(projectDir = "src/test/projects/specific-file")
public class SpecificFileTest extends SpecificFileNativeIT {
protected boolean isNative() {
return false;
}
}

View File

@@ -15,21 +15,28 @@
*/
package org.mvndaemon.mvnd.junit;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.mvndaemon.mvnd.assertj.TestClientOutput;
import org.mvndaemon.mvnd.client.DaemonParameters;
import org.mvndaemon.mvnd.client.DefaultClient;
import org.mvndaemon.mvnd.client.ExecutionResult;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.logging.ClientOutput;
public class JvmTestClient extends DefaultClient {
private DaemonParameters parameters;
public JvmTestClient(DaemonParameters parameters) {
super(parameters);
this.parameters = parameters;
}
@Override
public ExecutionResult execute(ClientOutput output, List<String> argv) {
setMultiModuleProjectDirectory(argv);
setSystemPropertiesFromCommandLine(argv);
final ExecutionResult delegate = super.execute(output, argv);
if (output instanceof TestClientOutput) {
@@ -38,6 +45,22 @@ public class JvmTestClient extends DefaultClient {
return delegate;
}
private void setMultiModuleProjectDirectory(List<String> args) {
// Specific parameters
Path dir;
if (Environment.MAVEN_FILE.hasCommandLineOption(args)) {
dir = parameters.userDir().resolve(Environment.MAVEN_FILE.getCommandLineOption(args));
if (Files.isRegularFile(dir)) {
dir = dir.getParent();
}
dir = dir.normalize();
} else {
dir = parameters.userDir();
}
System.setProperty(Environment.MAVEN_MULTIMODULE_PROJECT_DIRECTORY.getProperty(),
parameters.multiModuleProjectDirectory(dir).toString());
}
public static class JvmTestResult implements ExecutionResult {
private final ExecutionResult delegate;

View File

@@ -0,0 +1 @@
-Dfoo-bar=xx-prj1-xx

View File

@@ -0,0 +1 @@
-Dfoo-bar=xx-prj2-xx