Usage of maven ${basedir} in multi-module setup

孤者浪人 提交于 2019-12-20 10:29:34

问题


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?


回答1:


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)




回答2:


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:


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



来源:https://stackoverflow.com/questions/6061537/usage-of-maven-basedir-in-multi-module-setup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!