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

发布于 2024-11-08 15:24:13 字数 1016 浏览 0 评论 0原文

我正在使用本地存储库,如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天白云 2024-11-15 15:24:13

尽管在你的情况下这很烦人,但这是众所周知的并且是故意的。一个 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)

黑凤梨 2024-11-15 15:24:13

您可以使用这个不错的插件确定模块的根目录:

    <plugin>
        <groupId>org.commonjava.maven.plugins</groupId>
        <artifactId>directory-maven-plugin</artifactId>
        <version>0.3.1</version>
        <executions>
            <execution>
                <id>directories</id>
                <goals>
                    <goal>directory-of</goal>
                </goals>
                <phase>initialize</phase>
                <configuration>
                    <property>maven-parent.basedir</property>
                    <project>
                        <groupId>my.group</groupId>
                        <artifactId>maven-parent</artifactId>
                    </project>
                </configuration>
            </execution>
        </executions>
    </plugin>

使用如下目录:${maven-parent.basedir}

You can determine the root directory of a module with this nice plugin:

    <plugin>
        <groupId>org.commonjava.maven.plugins</groupId>
        <artifactId>directory-maven-plugin</artifactId>
        <version>0.3.1</version>
        <executions>
            <execution>
                <id>directories</id>
                <goals>
                    <goal>directory-of</goal>
                </goals>
                <phase>initialize</phase>
                <configuration>
                    <property>maven-parent.basedir</property>
                    <project>
                        <groupId>my.group</groupId>
                        <artifactId>maven-parent</artifactId>
                    </project>
                </configuration>
            </execution>
        </executions>
    </plugin>

Use the directory like this: ${maven-parent.basedir}

紧拥背影 2024-11-15 15:24:13

拥有多个存储库怎么样?

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文