【问题标题】:Maven project.build.testSourceDirectory property not working from profileMaven project.build.testSourceDirectory 属性在配置文件中不起作用
【发布时间】:2013-10-08 23:45:04
【问题描述】:

我想对 maven 项目中的不同文件夹进行测试,我需要更改 maven 的 project.build.testSourceDirectory 属性。

我正在使用 maven 配置文件来解决这个问题。

我的个人资料如下所示:

<profiles>
    <profile>
        <id>sahi_UI_testing</id>
        <activation>
            <property>
                <name>sahiTesting</name>
                <value>true</value>
            </property>
        </activation>
        <properties>
            <maven.test.skip>false</maven.test.skip>
            <project.build.testSourceDirectory>src/test/java/org/package1/package2/sahi</project.build.testSourceDirectory>
        </properties>
    </profile>
</profiles>

project.build.testSourceDirectory 没有改变,只保留默认的/home/username/workspace/projectName/core/src/test/java(我已经用maven-antrun-plugin 对此进行了测试并给出了该路径)。
我在项目中有多个 pom.xml,所以在路径中我有 ../core/.. 文件夹(这是项目的核心 pom.xml)。


maven-antrun-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.1</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <tasks>
                    <echo>******** Displaying value of property ********</echo>
                    <echo>${project.build.testSourceDirectory}</echo>
                    <echo>${maven.test.skip}</echo>
                </tasks>
            </configuration>
        </execution>
    </executions>
</plugin>


maven-antrun-plugin 中执行mvn help:active-profiles -f core/pom.xml -Dtest=JavaClientTest -o -e test -DsahiTesting=true&lt;maven.test.skip&gt;true&lt;/maven.test.skip&gt; 这给了我:
[INFO] 执行任务
[echo] ** 显示属性值 **
[echo] /home/username/workspace/projectName/core/src/test/java
[回声] 真

&lt;maven.test.skip&gt;false&lt;/maven.test.skip&gt;maven-antrun-plugin 这给了我:
[INFO] 执行任务
[echo] ** 显示属性值 **
[echo] /home/username/workspace/projectName/core/src/test/java
[回声] 假

因此,我们可以看到另一个变量已更改。

我知道配置文件已激活,因为我使用了maven-help-plugin 来识别它。
maven-help-plugin 给了我这个结果:
以下配置文件处于活动状态:

  • sahi_UI_testing(来源:pom)

我尝试在没有 maven 配置文件的情况下仅更改 &lt;build&gt; 标记中的 project.build.testSourceDirectory 属性。

...
<build>
    <testSourceDirectory>src/test/java/org/package/package2/sahi</testSourceDirectory>
    ...
</build>

该属性已更改(但我需要为该属性分配多个值)。 我试过maven-surefire-plugin 也没用。

问题是为什么project.build.testSourceDirectory在使用配置文件时没有改变?

【问题讨论】:

    标签: maven pom.xml


    【解决方案1】:

    您不能用属性覆盖project 模型,Maven 不允许这样做。您唯一的选择是在您要使用的插件中指定 testSourceDirectory(假设它可用)。

    【讨论】:

    • 我现在明白我无法覆盖属性中的project 模型。我已经尝试过那个插件 (maven-surefire-plugin) 但它不起作用。我在互联网上了解到该插件的 testSourceDirectory 也不适用于其他开发人员。
    【解决方案2】:

    我发现这个问题的解决方案不是很好,但它是一个解决方案。对我来说它正在工作,它改变了testSourceDirectory 变量。

    pom.xml 文件中,我在properties 标记中声明了一个自己的变量,并使用testSourceDirectory 的默认值初始化:

    <project ...>
        ...
        <properties>
            <myTestSourceDirectory>${project.basedir}/src/test/java</myTestSourceDirectory>
            ...
        </properties>
        ...
    </project>
    

    我的个人资料是:

    <profiles>
        <profile>
            <id>sahi_UI_testing</id>
            <activation>
                <property>
                    <name>sahiTesting</name>
                    <value>true</value>
                </property>
            </activation>
            <properties>
                <maven.test.skip>false</maven.test.skip>
                <myTestSourceDirectory>src/test/java/org/<package1>/<package2>/sahi</myTestSourceDirectory>
            </properties>
        </profile>
    </profiles>
    

    build 标签的开头,我将testSourceDirectory 设置为

    <project ...>
        <build>
            <testSourceDirectory>${myTestSourceDirectory}</testSourceDirectory>
            ...
        <build>
        ...
    </project>
    

    因此,当我们不使用配置文件时,我们对myTestSourceDirectory 变量有一个默认值,当我们使用配置文件时,该变量将更改为请求的测试目录。该变量始终存在,并且在build 标记中我们将testSourceDirectory 属性更改为所需的值。

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 2013-05-17
      • 2018-05-18
      • 2012-03-07
      • 1970-01-01
      • 2012-08-17
      • 2022-01-24
      • 2014-05-30
      • 1970-01-01
      相关资源
      最近更新 更多