查找多模块Maven reactor项目的根目录 [英] Finding the root directory of a multi module Maven reactor project

查看:59
本文介绍了查找多模块Maven reactor项目的根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以找到多模块 Maven 项目的根目录,例如 Gradle 的 rootDir?


背景:

我想使用 maven-dependency-plugin 将工件从我的多模块项目的所有子模块复制到相对于整个项目根目录的目录.

也就是说,我的布局看起来与此类似,名称已更改:

部署/我的项目/模块-a/模块-b/更多模块-1/模块-c/模块-d/更多模块-2/模块-e/模块-f/...

并且我希望将所有工件从各自模块的目标目录复制到 my-project/../to-deploy 所以我最终得到

部署/模块-a.jar模块-b.jar模块-c.jar模块-d.jar模块-e.jar模块-f.jar我的项目/...

我可以在每个模块中使用相对路径来完成,如下所示:

 <插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><执行><执行><id>复制</id><阶段>安装</阶段><目标><目标>复制</目标></目标><配置><artifactItems><artifactItem><groupId>${project.groupId}</groupId><artifactId>${project.artifactId}</artifactId><version>${project.version}</version><type>jar</type><outputDirectory>../../to-deploy</outputDirectory></artifactItem></artifactItems></配置></执行></执行></插件>

但我宁愿不在 元素中指定相对路径.我更喜欢 ${reactor.root.directory}/../to-deploy 之类的东西,但我找不到这样的东西.

另外,我更喜欢是否有某种方法可以继承这个 maven-dependency-plugin 配置,这样我就不必为每个模块指定它.

我也尝试从根 pom 继承自定义属性:

<myproject.root>${basedir}</myproject.root></属性>

但是当我尝试在模块 POM 中使用 ${myproject.root} 时,${basedir} 将解析为模块的 basedir.>

另外,我发现 http://labs.consol.de/lang/de/blog/maven/project-root-path-in-a-maven-multi-module-project/ 建议每个开发人员和大概的持续集成服务器应该在 profile.xml 文件中配置根目录,但我不认为这是一个解决方案.

那么有没有一种简单的方法可以找到多模块项目的根目录?

解决方案

use ${session.executionRootDirectory}

作为记录,${session.executionRootDirectory} 在 Maven 3.0.3 的 pom 文件中对我有用.该属性将是您运行的目录,因此运行父项目,每个模块都可以获得该根目录的路径.

我将使用此属性的插件配置放在父 pom 中,以便它被继承.我在一个配置文件中使用它,只有当我知道我将在父项目上运行 Maven 时才选择该配置文件.这样,当我在子项目上运行 Maven 时,我不太可能以不希望的方式使用这个变量(因为这样变量就不会是父项目的路径).

例如

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><执行><执行><id>copy-artifact</id><phase>包</phase><目标><目标>复制</目标></目标><配置><artifactItems><artifactItem><groupId>${project.groupId}</groupId><artifactId>${project.artifactId}</artifactId><version>${project.version}</version><type>${project.packaging}</type></artifactItem></artifactItems><outputDirectory>${session.executionRootDirectory}/target/</outputDirectory></配置></执行></执行></插件>

Is there an easy way to find the root of a multi-module Maven project, like Gradle's rootDir?


Background:

I want to use the maven-dependency-plugin to copy artifacts from all sub-modules of my multi-module project to a directory that is relative to the root directory of the entire project.

That is, my layout looks similar to this, names changed:

to-deploy/
my-project/
    module-a/
    module-b/
    more-modules-1/
        module-c/
        module-d/
    more-modules-2/
        module-e/
        module-f/
    ...

And i want all the artifacts to be copied from the target-directories of their respective modules to my-project/../to-deploy so i end up with

to-deploy/
    module-a.jar
    module-b.jar
    module-c.jar
    module-d.jar
    module-e.jar
    module-f.jar
my-project/
    ...

I could do it with a relative path in each module, like so:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>install</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>${project.groupId}</groupId>
                                <artifactId>${project.artifactId}</artifactId>
                                <version>${project.version}</version>
                                <type>jar</type>
                                <outputDirectory>../../to-deploy</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>

But i'd rather not specify a relative path in the <outputDirectory> element. I'd prefer something like ${reactor.root.directory}/../to-deploy, but i can't find anything like this.

Also, i'd prefer if there was some way to inherit this maven-dependency-plugin configuration so i don't have to specify it for each module.

I also tried inheriting a custom property from the root pom:

<properties>
    <myproject.root>${basedir}</myproject.root>
</properties>

But when i tried to use ${myproject.root} in the module POM's, the ${basedir} would resolve to the basedir of the module.

Also, i found http://labs.consol.de/lang/de/blog/maven/project-root-path-in-a-maven-multi-module-project/ where it's suggested that each developer and presumably the continuous integration server should configure the root directory in a profiles.xml file, but i don't consider it a solution.

So is there an easy way to find the root of a multi-module project?

解决方案

use ${session.executionRootDirectory}

For the record, ${session.executionRootDirectory} works for me in pom files in Maven 3.0.3. That property will be the directory you're running in, so run the parent project and each module can get the path to that root directory.

I put the plugin configuration that uses this property in the parent pom so that it's inherited. I use it in a profile that I only select when I know that I'm going to run Maven on the parent project. This way, it's less likely that I'll use this variable in an undesired way when I run Maven on a child project (because then the variable would not be the path to the parent).

For example,

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-artifact</id>
            <phase>package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <type>${project.packaging}</type>
                    </artifactItem>
                </artifactItems>
                <outputDirectory>${session.executionRootDirectory}/target/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

这篇关于查找多模块Maven reactor项目的根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆