Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions apache-maven/src/bin/mvn
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fi
# first directory with .mvn subdirectory is considered project base directory
find_maven_basedir() {
(
basedir="`pwd`"
basedir=`find_file_argument_basedir "$@"`
wdir="`pwd`"
while [ "$wdir" != '/' ] ; do
if [ -d "$wdir"/.mvn ] ; then
Expand All @@ -134,14 +134,41 @@ find_maven_basedir() {
)
}

find_file_argument_basedir() {
(
basedir="`pwd`"

found_file_switch=0
for arg in "$@"; do
if [ ${found_file_switch} -eq 1 ]; then
if [ -f ${arg} ]; then
basedir=$(dirname $(readlink -f "${arg}"))
if [ ! -d ${basedir} ]; then
echo "Directory ${basedir} extracted from the -f/--file command-line argument ${arg} does not exist" >&2
exit 1
fi
else
echo "POM file ${arg} specified with the -f/--file command line argument does not exist" >&2
exit 1
fi
break
fi
if [ "$arg" == "-f" -o "$arg" == "--file" ]; then
found_file_switch=1
fi
done
echo "${basedir}"
)
}

# concatenates all lines of a file
concat_lines() {
if [ -f "$1" ]; then
echo "`tr -s '\n' ' ' < "$1"`"
fi
}

MAVEN_PROJECTBASEDIR="${MAVEN_BASEDIR:-`find_maven_basedir`}"
MAVEN_PROJECTBASEDIR="${MAVEN_BASEDIR:-`find_maven_basedir "$@"`}"
MAVEN_OPTS="`concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config"` $MAVEN_OPTS"

# For Cygwin, switch project base directory path to Windows format before
Expand Down
58 changes: 54 additions & 4 deletions apache-maven/src/bin/mvn.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,69 @@ set MAVEN_CMD_LINE_ARGS=%*
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
if not "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir

set "EXEC_DIR=%CD%"
set "WDIR=%EXEC_DIR%"
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
)
call :get_directory_from_file %FILE_ARG%
if not exist "%POM_DIR%" (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check not present in the shell script?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like it or not, sh and cmd are different and have different capabilities. In the sh script, we know that the directory exists because of the check for existence of the POM file. I easily can add the POM file existence test to the cmd script and print the same errors if the pom file does not exist.

In the cmd script, I need to check for the existence of the extracted directory because if, for some reason, the directory extracted doesn't exist (e.g., a bug in the extraction code), the user gets a cryptic error. The same is not true for the sh script.

I will add the redundant check to the sh script too just for symmetry purposes...

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 findBaseDir
goto findBaseDirLoop

:baseDirFound
set "MAVEN_PROJECTBASEDIR=%WDIR%"
cd "%EXEC_DIR%"
cd /d "%EXEC_DIR%"
goto endDetectBaseDir

:baseDirNotFound
Expand Down