有没有一种简单的方法可以找到多模块 Maven 项目的根目录,例如 Gradle 的 rootDir
?
背景:
我想使用 maven-dependency-plugin 将多模块项目的所有子模块中的 Artifact 复制到相对于整个项目根目录的目录。
也就是说,我的布局看起来与此类似,名称已更改:
to-deploy/
my-project/
module-a/
module-b/
more-modules-1/
module-c/
module-d/
more-modules-2/
module-e/
module-f/
...
我希望将所有 Artifact 从各自模块的目标目录复制到 my-project/../to-deploy
所以我最终得到
to-deploy/
module-a.jar
module-b.jar
module-c.jar
module-d.jar
module-e.jar
module-f.jar
my-project/
...
我可以使用每个模块中的相对路径来完成此操作,如下所示:
<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>
但我不想在 <outputDirectory>
中指定相对路径元素。
我更喜欢类似 ${reactor.root.directory}/../to-deploy
的东西,但我找不到这样的东西。
此外,我希望有某种方法可以继承此 maven-dependency-plugin 配置,这样我就不必为每个模块指定它。
我还尝试从根 pom 继承自定义属性:
<properties>
<myproject.root>${basedir}</myproject.root>
</properties>
但是当我尝试使用${myproject.root}
时在模块 POM 中,${basedir}
将解析为模块的 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>
<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 - 查找多模块 Maven Reactor 项目的根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3084629/