有没有一种简单的方法可以找到多模块Maven项目的根,比如Gradle的rootDir
背景:
我想使用maven-dependency-plugin将工件从我的多模块项目的所有子模块复制到一个相对于整个项目根目录的目录。
也就是说,我的布局类似于此,名称已更改:
to-deploy/
my-project/
module-a/
module-b/
more-modules-1/
module-c/
module-d/
more-modules-2/
module-e/
module-f/
...我希望将所有工件从它们各自模块的目标目录复制到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>但是,当我尝试在模块POM中使用${myproject.root}时,${basedir}将解析为模块的basedir。
此外,我发现了http://labs.consol.de/lang/de/blog/maven/project-root-path-in-a-maven-multi-module-project/,其中建议每个开发人员和持续集成服务器应该在profiles.xml文件中配置根目录,但我不认为这是一种解决方案。
那么,有没有一种简单的方法来找到多模块项目的根呢?
发布于 2011-10-06 23:30:03
使用${session.executionRootDirectory}
需要指出的是,在Maven3.0.3中,${session.executionRootDirectory}在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>发布于 2018-03-28 15:11:25
从Maven3.3.1开始,您可以使用${maven.multiModuleProjectDirectory}来实现此目的。(感谢https://stackoverflow.com/a/48879554/302789)
编辑:这似乎只有当你的项目根目录下有一个.mvn文件夹时才能正常工作。
发布于 2012-01-13 17:07:22
我在我的项目中使用的一些东西是重写子模块pom中的属性。
root: <myproject.root>${basedir}</myproject.root>
moduleA: <myproject.root>${basedir}/..</myproject.root>
other/moduleX: <myproject.root>${basedir}/../..</myproject.root>
这样,您仍然拥有相对路径,但是您可以在根模块中定义一次插件,您的模块将通过正确的myproject.root替换来继承它。
https://stackoverflow.com/questions/3084629
复制相似问题