【问题标题】:Resource path during reactor build and child build (Maven)反应堆构建和子构建期间的资源路径(Maven)
【发布时间】: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>

【问题讨论】:

标签: xml bash maven


【解决方案1】:

我最终只在父 POM 中设置了这个单一属性。

<properties>
    <!-- NOTE This works for reactor/aggregation & child module builds. -->
    <root.basedir>${project.basedir}/../parent</root.basedir>
</properties>

这是有效的,因为${project.basedir} 似乎扩展为绝对路径, 并且始终与正在构建的实际项目相关,即使在反应堆 POM 上执行 maven 也是如此。就我而言,要构建的项目(将使用${root.basedir} 属性)将始终是子项目。它们都位于“modules/”下,因此从它们的路径中转到“../parent”可以正常工作。

${root.basedir} 将包含如下内容:

"/home/user/projects/UniBldResLocPrb/modules/child/../parent"

感谢用户 Gerold BroserVic 引导我回答这个问题。

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 2021-05-04
    • 2018-03-09
    • 1970-01-01
    • 2023-03-12
    • 2011-10-07
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多