在多模块设置中使用maven $ {basedir}

时间:2020-12-06 00:00:25

I am using a local repository as described in Maven: add a dependency to a jar by relative path.

我正在使用Maven中描述的本地存储库:通过相对路径向jar添加依赖项。

The repository-url is defined in the topmost pom.xml as

repository-url在最顶层的pom.xml中定义为

<url>file:${basedir}/../3rdParty/maven-repository</url>

Also, the topmost pom.xml defines 2 modules

此外,最顶层的pom.xml定义了2个模块

<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.

问题是,如果一个模块(比如sub1)定义了一个应该从存储库下载的依赖项,并且maven是从最顶层的目录调用的,那么$ {basedir}不会设置到这个目录,而是设置为sub1,导致错误的存储库URL。

So, say the project with the topmost pom.xml resides in

所以,说最顶层pom.xml的项目就在于

/Development/myproject/pom.xml

And the repository is in

存储库在

/Development/3rdParty/maven-repository

Then the repository URL should be set to

然后应将存储库URL设置为

/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?

知道为什么会这样吗?

3 个解决方案

#1


21  

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.

虽然在你的情况下很烦人,但这是众所周知和有意的。 maven项目应该只知道它的执行目录,无论它在什么上下文中执行。

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.

我问了几乎相同的问题:反应堆root的Maven变量更早,唯一有意义的答案是使用$ {user.dir},虽然它很hacky,如果从模块目录构建,它将无法工作。

(There is also this very verbose solution: Maven2 property that indicates the parent directory)

(还有这个非常详细的解决方案:Maven2属性,指示父目录)

#2


3  

How about having multiple repos?

有多个回购怎么样?

<repositories>
    <repository>
        <id>ibm-jars-bundle-lv0</id>
        <url>file://${basedir}/ibm-jars-bundle/repo</url>
    </repository>
    <repository>
        <id>ibm-jars-bundle-lv1</id>
        <url>file://${basedir}/../ibm-jars-bundle/repo</url>
    </repository>
    <repository>
        <id>ibm-jars-bundle-lv2</id>
        <url>file://${basedir}/../../ibm-jars-bundle/repo</url>
    </repository>
</repositories>

#3


2  

I already asked a similar question, regarding the directory of the parent project.

关于父项目的目录,我已经问了一个类似的问题。

You can see the thread here: Maven2 property that indicates the parent directory

您可以在此处查看该主题:Maven2属性,指示父目录

#1


21  

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.

虽然在你的情况下很烦人,但这是众所周知和有意的。 maven项目应该只知道它的执行目录,无论它在什么上下文中执行。

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.

我问了几乎相同的问题:反应堆root的Maven变量更早,唯一有意义的答案是使用$ {user.dir},虽然它很hacky,如果从模块目录构建,它将无法工作。

(There is also this very verbose solution: Maven2 property that indicates the parent directory)

(还有这个非常详细的解决方案:Maven2属性,指示父目录)

#2


3  

How about having multiple repos?

有多个回购怎么样?

<repositories>
    <repository>
        <id>ibm-jars-bundle-lv0</id>
        <url>file://${basedir}/ibm-jars-bundle/repo</url>
    </repository>
    <repository>
        <id>ibm-jars-bundle-lv1</id>
        <url>file://${basedir}/../ibm-jars-bundle/repo</url>
    </repository>
    <repository>
        <id>ibm-jars-bundle-lv2</id>
        <url>file://${basedir}/../../ibm-jars-bundle/repo</url>
    </repository>
</repositories>

#3


2  

I already asked a similar question, regarding the directory of the parent project.

关于父项目的目录,我已经问了一个类似的问题。

You can see the thread here: Maven2 property that indicates the parent directory

您可以在此处查看该主题:Maven2属性,指示父目录