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

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

问题描述

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



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

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

我想让所有的EAR文件从各自模块的目标目录复制到 my-project /../ to-deployment 所以我最终得到

 部署/ 
ear-module-a.ear
ear-module-b.ear
ear-module-c.ear
ear-module-d.ear
ear -module-e.ear
ear-module-f.ear
my-project /
...

我可以做到这一点每个耳朵模块中的相对路径,如下所示:

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
<执行>
< 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> ear< / type>
< outputDirectory> ../../ to-deploy< / outputDirectory>
< / artifactItem>
< / artifactItems>
< / configuration>
< / execution>
< / executions>
< / plugin>

但我宁愿不在< outputDirectory> ; 元素。
我喜欢像 $ {reactor.root.directory} /../ to-deploy ,但我找不到这样的东西。 / p>

此外,我喜欢如果有一些方法继承这个maven依赖插件配置,所以我不必为每个EAR模块指定它。 / p>

我还尝试从根目录继承自定义属性:

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

但是当我尝试使用 $ {myproject.root} 在耳机模块POM中, $ {basedir} 将解决耳机模块的基础。



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



那么有一个找到多模块项目的根源的简单方法?

解决方案

使用 $ {session.executionRootDirectory }



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



我将插件配置在父pom中使用此属性,以便继承它。我在一个配置文件中使用它,只有当我知道我要在父项目上运行Maven时才选择。这样一来,当我在一个子项目上运行Maven(因为那个变量不会是父节点的路径)时,我不大可能以不理想的方式使用这个变量。



例如

 < plugin> 
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
<执行>
< 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>


I want to use the maven-dependency-plugin to copy EAR-files 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/
    ear-module-a/
    ear-module-b/
    more-modules-1/
        ear-module-c/
        ear-module-d/
    more-modules-2/
        ear-module-e/
        ear-module-f/
    ...

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

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

I could do it with a relative path in each ear-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>ear</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 EAR-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 ear-module POM's, the ${basedir} would resolve to the basedir of the ear-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反应器项目的根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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