Maven help:evaluate 在批处理文件中抛出 MissingProjectException

Maven help:evaluate throwing MissingProjectException in Batch file

我想在批处理脚本中将maven的本地仓库路径读入一个变量。

尝试通过控制台检索路径时,它工作正常:

C:\Users\me>mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout
C:\Users\me\.m2\repository

但是当 运行 在批处理文件的 for-in 循环中执行相同操作时,它会抛出 MissingProjectException。

C:\Users\me>FOR /F "tokens=*" %%g IN ('mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout') do (SET LOCAL_REPO=%%g)
C:\Users\me>(SET LOCAL_REPO=[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\Users\me). Please verify you invoked Maven from the correct directory. -> [Help 1] )
C:\Users\me>(SET LOCAL_REPO=[ERROR]  )
C:\Users\me>(SET LOCAL_REPO=[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. )
C:\Users\me>(SET LOCAL_REPO=[ERROR] Re-run Maven using the -X switch to enable full debug logging. )
C:\Users\me>(SET LOCAL_REPO=[ERROR]  )
C:\Users\me>(SET LOCAL_REPO=[ERROR] For more information about the errors and possible solutions, please read the following articles: )
C:\Users\me>(SET LOCAL_REPO=[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException )

我假设它没有正确解释这里的命令(可能是因为冒号?),但我不知道我做错了什么。非常感谢任何帮助:)

FOR /F "tokens=*" %%g IN ('mvn help:evaluate "-Dexpression=settings.localRepository" -q -DforceStdout') do (SET LOCAL_REPO=%%g)

-Dexpression标志需要放在引号内,否则被拆分,'settings.localRepository'被视为生命周期阶段。