【发布时间】:2015-10-24 20:49:14
【问题描述】:
如何构造构建过程中使用的资源的路径(特别是verify阶段),这样会发现maven是在reactor根上执行还是直接在子模块上执行?
项目文件
UniBldResLocPrb/pom.xml # Reactor/Aggregation POM
UniBldResLocPrb/modules/parent/pom.xml
UniBldResLocPrb/modules/parent/src/main/resources/javaHeaderRegexTemplate.txt
UniBldResLocPrb/modules/parent/src/main/resources/checkstyle.xml
UniBldResLocPrb/modules/child/pom.xml
场景
我需要插入资源的路径 (javaHeaderRegexTemplate.txt) - 绝对或相对于 maven 执行 CWD - 进入其他资源 (checkstyle.xml)。这是在 parent/pom.xml 中配置的。 这条路径(相对,因为我不知道如何将其作为绝对路径)根据我是直接构建子项目还是通过反应器构建而变化。
正确的路径:
- 子版本:
../parent/src/main/resources/javaHeaderRegexTemplate.txt - 反应堆构建:
modules/parent/src/main/resources/javaHeaderRegexTemplate.txt
我无法配置我的 POM,因此这两个版本都可以正常工作。
直接构建孩子时它可以正常工作:
cd UniBldResLocPrb/modules/child/
mvn verify
[INFO] BUILD SUCCESS
但是通过 reactor POM 构建时失败:
cd UniBldResLocPrb/
mvn verify
[INFO] BUILD FAILURE
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.15:check (default) on project myproj-child: Failed during checkstyle configuration: cannot initialize module RegexpHeader - unable to load header file ../parent/src/main/resources/javaHeaderRegexTemplate.txt -> [Help 1]
如果我在子 POM 中手动设置<root.basedir>modules/parent</root.basedir>,那么只有 reactor 构建成功,而子构建失败。
这个问题与我最接近:Maven Project Aggregation and Variables 但接受的答案并不适用,因为检查文件的配置文件激活始终使用子项目作为 CWD,即使 maven 执行 CWD 是反应器项目的 CWD。p>
我正在使用 Maven 3.0.5 进行测试。
示例项目来源
我做了一个最小的例子,展示了我尝试了什么以及它是如何失败的。
GitHub 项目:https://github.com/ProblemExamples/UniBldResLocPrb
或:
git clone https://github.com/ProblemExamples/UniBldResLocPrb.git
来源的重要部分(如在 github 上)
UniBldResLocPrb/pom.xml:
<properties>
<!-- NOTE This will be overwritten by the child module. -->
<root.basedir>modules/parent</root.basedir>
</properties>
UniBldResLocPrb/modules/child/pom.xml:
<properties>
<!--
NOTE This is alwasy in effect, but it is only valid if maven is executed
on this POM directly.
-->
<root.basedir>${project.parent.relativePath}</root.basedir>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<goals>
<!-- Execute during the maven "verify" phase (`mvn verify`) -->
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
UniBldResLocPrb/modules/parent/src/main/resources/checkstyle.xml:
<!-- NOTE the used variable is supplied by the invocation in maven! -->
<module name="RegexpHeader">
<property name="headerFile" value="${java.header.regex.template.file}"/>
</module>
UniBldResLocPrb/modules/parent/pom.xml:
<properties>
<!--
NOTE This would be overwritten by the child module,
so it has no effect in this particular example project,
as we do not use this variable in this POM during the build.
-->
<!--<root.basedir>${project.basedir}</root.basedir>-->
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<configuration>
<configLocation>src/main/resources/checkstyle.xml</configLocation>
<propertyExpansion>java.header.regex.template.file=${root.basedir}/src/main/resources/javaHeaderRegexTemplate.txt</propertyExpansion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
【问题讨论】:
-
真的,谢谢!我减少了我在帖子中显示的来源。
-
参见Vic's answer 到Maven2 property that indicates the parent directory,他在其中使用Build Profile 和CheckStyle。