【问题标题】:Usage of maven ${basedir} in multi-module setup在多模块设置中使用 maven ${basedir}
【发布时间】:2011-08-29 00:37:27
【问题描述】:

我正在使用Maven: add a dependency to a jar by relative path 中所述的本地存储库。

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

那么repository URL应该设置为

/Development/myproject/../3rdParty/maven-repository

但事实证明它被设置为

/Development/myproject/sub1/../3rdParty/maven-repository

这当然不存在。

知道为什么会这样吗?

【问题讨论】:

  • 为什么不使用存储库管理器来维护您的工件?
  • 根据给定的信息,您的构建不可靠且不可重现。这对构建来说是不行的。

标签: maven maven-2


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    我之前问了几乎相同的问题:Maven variable for reactor root,唯一有意义的答案是to use ${user.dir},虽然它很老套,如果你从模块目录构建将无法工作。

    (还有这个非常冗长的解决方案:Maven2 property that indicates the parent directory

    【讨论】:

      【解决方案3】:

      拥有多个 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>
      

      【讨论】:

      • 这行得通...但我相信它可以在查找自第一个存储库定义以来的本地依赖项时在子目录中创建不必要的“ibm-jars-bundle/repo”目录(ibm-jars-bundle -lv0) 首先尝试查找当前子目录的根目录,如果该目录不存在则创建该目录。
      • 我有一个错误,因为 file://${basedir} ... 被替换为 file:///app 注意 3 /
      • @LuisRoberto 它适用于 3 个斜线:Downloading from ibm-jars-bundle-lv0: file:///home/il/build/test-jar-bundle/ibm-jars-bundle/repo/foo/test-jar-bundl2/0.1-SNAPSHOT/test-jar-bundl2-0.1-SNAPSHOT.pom
      猜你喜欢
      • 1970-01-01
      • 2015-11-03
      • 2012-03-13
      • 1970-01-01
      • 2021-12-20
      • 2014-10-15
      • 2017-09-01
      • 2016-06-01
      • 2019-05-05
      相关资源
      最近更新 更多