简体   繁体   English

Maven 获取依赖的相对路径

[英]Maven get relative path of dependency

I'm trying to get the relative path of the Maven dependency inside a pom.xml .我试图在pom.xml中获取 Maven 依赖项的相对路径。 Here is my project structure:这是我的项目结构:

|-Project
  |--firstproject
     |---src
     |...
     |pom.xml
  |--secondproject
     |---src
     |...
     |pom.xml

Here is the pom.xml of firstproject where I'm importing secondproject as dependency.这是我将pom.xml作为依赖项导入的secondprojectfirstproject

...
<dependency>
    <groupId>com.foo</groupId>
    <artifactId>secondproject</artifactId>
    <version>1.0.0</version>
</dependency>
...

I would like to have the system path of Maven dependency like this:我想要像这样的 Maven 依赖的系统路径:

<properties>
    <my.lib.path>${maven.dependency.com.foo.secondproject}</my.lib.path>
</properties>

Is that possible?那可能吗? Is there maybe another way to do this?有没有其他方法可以做到这一点?

I know I can use maven-antrun-plugin , but I don't want the final jar .我知道我可以使用maven-antrun-plugin ,但我不想要最终的jar I want the relative path of the dependency like: /home/user/projects/Project/secondproject .我想要依赖项的相对路径,例如: /home/user/projects/Project/secondproject Is there any way?有什么办法吗?

This is a bad idea and is poor practice when it comes to Maven dependencies.当涉及到 Maven 依赖项时,这是一个坏主意,也是不好的做法。

First and foremost, your projects will always need to be relatively locateable to one another, which means that your build environment will have to have them together.首先,您的项目将始终需要彼此相对可定位,这意味着您的构建环境必须将它们放在一起。 This translates to builds which may not be repeatable because there is absolutely no guarantee that the second project would not have changed between builds of the first project.这转化为可能不可重复的构建,因为绝对不能保证第二个项目在第一个项目的构建之间不会发生变化。

Second, the conventional way to combine multiple related projects is to have subprojects instead.其次,合并多个相关项目的常规方式是有子项目。 This way you can have your first project depend on your second project without any headaches.这样你就可以让你的第一个项目依赖于你的第二个项目,而不会让人头疼。

Failing any of that, then the best alternative is to publish the second project to your local m2 repository or your repository store, and pulling it down in your first project.如果其中任何一个都失败,那么最好的选择是将第二个项目发布到您的本地 m2 存储库或存储库存储中,然后将其拉到您的第一个项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM