如何在子模块pom中获取超级pom basedir?

发布于 2024-12-24 22:32:59 字数 468 浏览 3 评论 0原文

我想在我的 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 的路径?

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

I want to define a local repository in my maven project.

I've got a super pom and several child modules. My file structure is :

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

in my super pom I define :

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

The problem is that in my child pom, the repository defined in my super pom refers to /root/child/repository and so, dependencies cannot be found...

Is there a way to define a path always relative to the super pom ?

If not, what's the best way to solve the problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

惟欲睡 2024-12-31 22:32:59

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

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

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

从插件站点:

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

toronto=raptors
miami=heat

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

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

In this case, at first you could try ${project.parent.basedir}.
As it seems it doesn't work, the simple(and native) way is use complete path (/root/...) or try relative path (../) instead of using ${basedir} variable.

But for me, a great solution would be externalize this configuration into a properties file.
You can use properties-maven-plugin ( http://mojo.codehaus.org/properties-maven-plugin/plugin-info.html ).

With this plugin, properties defined on the properties file can be read just like properties defined inside pom.xml.

From the plugin site:

If you have a properties file called teams.properties with this content:

toronto=raptors
miami=heat

Would be the same as declaring the following in your pom.xml:

<properties> 
  <toronto>raptors</toronto>
  <miami>heat</miami>
</properties>
娇柔作态 2024-12-31 22:32:59

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

或者您可以在属性中设置根的 basedir-path,这样它将被继承。父母

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

和孩子身上都有类似的事情

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

${project.parent.basedir} should do the job.

Or you can set the basedir-path of the root in a property, so it will be inherited. Something like this in the Parent

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

And in the Child

<repository>
  <id>my-local-repo</id>
  <url>file://${rootPath}/repository</url>
</repository>
安人多梦 2024-12-31 22:32:59

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

例子:

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

I solved this many times with groovy plugin. Add a file called "basepath_marker" to your super pom's directory and add the following to your pom. You can access the property like this: ${base-path}. Read this blog post for more details.

Example:

 ...
 <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>
 ...
故人如初 2024-12-31 22:32:59

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

  1. 定义一个属性 ${basedir} int 你的父 pom
  2. 在你的子 pom 中使用 ${rootPath}

I try ${basedir}/../ in child pom and it works.
${project.parent.basedir} can not be interpreted.
Also the solution as follow not work, seems ${basedir} is dynamic decided.

  1. define a properties <rootPath> ${basedir} </rootPath> int your parent pom
  2. use ${rootPath} in your child pom
时间海 2024-12-31 22:32:59

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

In Parent pom -
Try to use relative path (../) instead of using ${basedir}

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