java maven ${user.home} 没有被正确解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1528837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:56:07  来源:igfitidea点击:

maven ${user.home} not being resolved correctly

javamaven-2properties

提问by schubySteve

I've got the following line in my pom.xml:

我的 pom.xml 中有以下行:

<updateRepositoryURL>file:/${user.home}/.m2/repository/repository.xml</updateRepositoryURL>

and when I am attempting to use it within my program the resultant string is:

当我试图在我的程序中使用它时,结果字符串是:

file:/c:Documents and Settings<myusername>/.m2/repository/repository.xml

where <myusername>is my user name funnily enough.

<myusername>有趣的是,我的用户名在哪里。

however it should be

然而它应该是

file:/c:/Documents and Settings/<myusername>/.m2/repository/repository.xml

Does anyone out there have any ideas as to why it is not resolving properly?

有没有人知道为什么它没有正确解决?

Thanks in advance

提前致谢

回答by Pascal Thivent

This might be a bug in maven. I've bookmarked this workaroundsome time ago:

这可能是 Maven 中的一个错误。前段时间我已将此解决方法添加为书签:

I found a handy way to reference the Java system property ${user.home}within a Maven build that supports Windows' ridiculous path name to home/profile directories:

c:\Documents and Settings\foobar.

The problem is, when using Maven, this parameterized property doesn't get passed through as one property value, but as three, because somewhere in the build Maven chokes on the spaces or back-slashes and interprets it as either three arguments:

"c:\Documents", "and", "Settings\foobar"

or treats the windows back-slash as an escape character and removes them so my parameterized user.home becomes:

"c:Documents and Settingsfoobar"

[...]

However, on Windows XP, unless I set the user.home on the build path every time, the back-slash escaping or space issues cause the files to not be found.

To fix it, add this profile to the $M2_HOME/conf/settings.xmlfile:

<profile>
<id>laptop-xp</id>
<properties>
<user.home>C:/Documents and Settings/${user.name}</user.home>
</properties>
</profile>

Then add an appropriate entry to the activeProfiles:

<activeProfile>laptop-xp</activeProfile>

Now every user will be able to use the user.homeproperty to reference their home path correctly on the Windows box.

我找到了一种${user.home}在 Maven 构建中引用 Java 系统属性的简便方法,该构建支持 Windows 到 home/profile 目录的荒谬路径名:

c:\Documents and Settings\foobar.

问题是,当使用 Maven 时,这个参数化属性不会作为一个属性值传递,而是作为三个属性值传递,因为构建 Maven 中的某个地方会阻塞空格或反斜杠并将其解释为三个参数:

"c:\Documents", "and", "Settings\foobar"

或将 Windows 反斜杠视为转义字符并删除它们,因此我的参数化 user.home 变为:

"c:Documents and Settingsfoobar"

[...]

但是,在 Windows XP 上,除非我每次都在构建路径上设置 user.home,否则反斜杠转义或空间问题会导致找不到文件。

要修复它,请将此配置$M2_HOME/conf/settings.xml文件添加到 文件中:

<profile>
<id>laptop-xp</id>
<properties>
<user.home>C:/Documents and Settings/${user.name}</user.home>
</properties>
</profile>

然后向 activeProfiles 添加适当的条目:

<activeProfile>laptop-xp</activeProfile>

现在每个用户都可以使用该 user.home属性在 Windows 框中正确引用他们的主路径。

Or, you're just another victim of this bug: http://bugs.sun.com/view_bug.do?bug_id=4787931. It's a very old bug (more than 6 years old) that affects all versions up till Java 1.6.x.

或者,您只是此错误的另一个受害者:http: //bugs.sun.com/view_bug.do?bug_id=4787931。这是一个非常古老的错误(超过 6 年),它影响到 Java 1.6.x 之前的所有版本。

回答by Andreas Dietrich

Another workaround could be to correct or produce the path via the Groovy GMaven plugin, similar to this groovy code:

另一种解决方法可能是通过Groovy GMaven 插件更正或生成路径,类似于以下 groovy 代码:

System.setProperty(  
  'user.home.fixed',  
  System.getProperty('user.home').replaceAll( '\\', '/' )  )

Maybe one could even override user.home, but I am not sure if it would work. So using ${user.home.fixed}everywhere instead of ${user.home}should work then if executed in an early phase.

也许人们甚至可以覆盖user.home,但我不确定它是否有效。因此${user.home.fixed}${user.home}如果在早期阶段执行,则使用无处不在而不是应该可以工作。