【问题标题】:how to get the super pom basedir in a child module pom?如何在子模块 pom 中获取 super pom basedir?
【发布时间】:2023-03-13 03:41:01
【问题描述】:

我想在我的 maven 项目中定义一个本地存储库。

我有一个超级 pom 和几个子模块。我的文件结构是:

/root
    /repository
    /child
        pom.xml
    pom.xml

在我的超级 pom 中我定义:

<repository>
    <id>my-local-repo</id>
    <url>file://${basedir}/repository</url>
</repository>

问题是在我的子 pom 中,我的超级 pom 中定义的存储库引用了 /root/child/repository ,因此无法找到依赖项...

有没有办法定义一个总是相对于超级 pom 的路径?

如果没有,解决问题的最佳方法是什么?

【问题讨论】:

  • 所以你想为每个项目创建存储库?也许${project.parent.relativePath} 将是可解析的(另外,您必须在所有子 POM 中定义 &lt;relativePath&gt;)。

标签: maven maven-2


【解决方案1】:

在这种情况下,首先你可以尝试${project.parent.basedir}
看起来它不起作用,简单(和本机)的方法是使用完整路径(/root/...)或尝试相对路径(../)而不是使用 ${basedir} 变量。

但对我来说,一个很好的解决方案是将此配置外部化到属性文件中。
您可以使用 properties-maven-plugin (http://mojo.codehaus.org/properties-maven-plugin/plugin-info.html)。

使用这个插件,可以像在 pom.xml 中定义的属性一样读取属性文件中定义的属性。

来自插件网站:

如果您有一个名为 teams.properties 的属性文件,其中包含以下内容:

toronto=raptors
miami=heat

与在 pom.xml 中声明以下内容相同:

<properties> 
  <toronto>raptors</toronto>
  <miami>heat</miami>
</properties>

【讨论】:

    【解决方案2】:

    ${project.parent.basedir} 应该可以完成这项工作。

    或者你可以在一个属性中设置根的basedir-path,这样它就会被继承。像这样的东西在父母

    <properties>
      <rootPath>${basedir}</rootPath>
    </properties>
    

    在孩子身上

    <repository>
      <id>my-local-repo</id>
      <url>file://${rootPath}/repository</url>
    </repository>
    

    【讨论】:

    • 感谢您的回复,但是父 pom 中的 ${basedir} 没有运行,因为它被子 pom 路径替换并且 ${project.parent.basedir} 永远不会被解释
    • 是的。属性首先从父级合并,然后进行处理。
    【解决方案3】:

    我用 groovy 插件多次解决了这个问题。将一个名为“basepath_marker”的文件添加到您的超级 pom 的目录中,并将以下内容添加到您的 pom.xml 中。您可以像这样访问该属性:${base-path}。阅读this blog post了解更多详情。

    例子:

     ...
     <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>groovy-maven-plugin</artifactId>
                <executions>
                    <!-- set absolute base path from super pom -->
                    <execution>
                        <id>find-basepath</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <source>
                                <![CDATA[
                                    import java.io.File;
                                    log.info('## define projects super pom absolute path through basepath_marker')
                                    String p = "basepath_marker";
                                    File f = null;
                                    if( p != null ) {
                                        def _max_child_poms = 0
                                        while( _max_child_poms++ < 5 ) {
                                            f = new File( p );
                                            if( f.exists() ) {
                                                break;
                                            }   
                                            p = "../" + p;                                 
                                        }
                                    }
                                    if( f != null ) {
                                        String basePath = f.getCanonicalPath();
                                        basePath = basePath.substring( 0, basePath.lastIndexOf( File.separator ) ); 
                                        project.properties['base-path'] = basePath.replace( '\\' , '/');
                                        log.info(' - used base path = ' + project.properties['base-path'] );
                                    } else {
                                        log.error( 'Could not find basepath_marker marker file!' );
                                        System.stop( 0 );
                                    }
                                ]]>
                            </source>
                        </configuration>
                    </execution>                    
                </executions>
            </plugin>
        </plugins>
    </build>
     ...
    

    【讨论】:

      【解决方案4】:

      我在子 pom 中尝试 ${basedir}/../ 并且它有效。
      ${project.parent.basedir} 无法解释。
      如下解决方案也不起作用,似乎${basedir} 是动态决定的。

      1. 在你的父 pom 中定义一个属性 &lt;rootPath&gt; ${basedir} &lt;/rootPath&gt;
      2. 在您的孩子 pom 中使用 ${rootPath}

      【讨论】:

        【解决方案5】:

        在父 pom - 尝试使用相对路径(../)而不是使用${basedir}

        【讨论】:

          猜你喜欢
          • 2020-04-26
          • 2010-12-31
          • 2011-11-26
          • 2016-07-25
          • 1970-01-01
          • 2020-07-12
          • 2021-06-08
          • 2013-10-17
          • 1970-01-01
          相关资源
          最近更新 更多