在多模块设置中使用 maven ${basedir}
我正在使用本地存储库,如 Maven: add通过相对路径对 jar 的依赖。
存储库 URL 在最顶层的 pom.xml 中定义为
<url>file:${basedir}/../3rdParty/maven-repository</url>
另外,最顶层的 pom.xml 定义了 2 个模块
<modules>
<module>sub1</module>
<module>sub2</module>
</modules>
问题是,如果一个模块(比如 sub1
) 定义了一个需要从仓库下载的依赖,maven是从最顶层目录调用的,${basedir}
没有设置到这个目录,而是设置到sub1
,导致错误的存储库 URL。
因此,假设最顶层 pom.xml
的项目位于
/Development/myproject/pom.xml
并且存储库位于
/Development/3rdParty/maven-repository
那么存储库 URL 应该设置为,
/Development/myproject/../3rdParty/maven-repository
但事实证明它设置为
/Development/myproject/sub1/../3rdParty/maven-repository
当然不存在。
知道为什么会这样吗?
I am using a local repository as described in Maven: add a dependency to a jar by relative path.
The repository-url is defined in the topmost pom.xml
as
<url>file:${basedir}/../3rdParty/maven-repository</url>
Also, the topmost pom.xml
defines 2 modules
<modules>
<module>sub1</module>
<module>sub2</module>
</modules>
The problem is, that if a module (say sub1
) defines a dependency that should be downloaded from the repository, and maven is called from the topmost directory, the ${basedir}
is not set to this directory, but to sub1
, resulting in a wrong repository-URL.
So, say the project with the topmost pom.xml
resides in
/Development/myproject/pom.xml
And the repository is in
/Development/3rdParty/maven-repository
Then the repository URL should be set to
/Development/myproject/../3rdParty/maven-repository
but it turns out it is set to
/Development/myproject/sub1/../3rdParty/maven-repository
which of course does not exist.
Any idea why that is the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管在你的情况下这很烦人,但这是众所周知的并且是故意的。一个 Maven 项目应该只知道它的执行目录,无论它在什么上下文中执行。
我问了几乎同样的问题: Maven variable forreactor root 早些时候,唯一的答案有意义的是使用
${user.dir}
,尽管它很hacky并且如果您从模块目录构建则无法工作。(还有一个非常详细的解决方案: Maven2 属性指示父目录)
Although it is annoying in your case, this is well-known and intentional. A maven project should know about its execution directory only, no matter in what context it is executed.
I asked almost the same question: Maven variable for reactor root earlier, and the only answer that made sense was to use
${user.dir}
, although it's hacky and will not work if you build from a module directory.(There is also this very verbose solution: Maven2 property that indicates the parent directory)
您可以使用这个不错的插件确定模块的根目录:
使用如下目录:
${maven-parent.basedir}
You can determine the root directory of a module with this nice plugin:
Use the directory like this:
${maven-parent.basedir}
拥有多个存储库怎么样?
How about having multiple repos?