6

I want to make testing for different folders in a maven project and I need to change the maven's project.build.testSourceDirectory property.

I'm using a maven profile for this problem.

My profile looks like this:

<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>

The project.build.testSourceDirectory isn't changed, only remains the default /home/username/workspace/projectName/core/src/test/java (I've tested this with maven-antrun-plugin and gives that path).
I have multiple pom.xml in the projects, so in the path I have the ../core/.. folder (this is the project's core pom.xml).


The 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>


Executing mvn help:active-profiles -f core/pom.xml -Dtest=JavaClientTest -o -e test -DsahiTesting=true with <maven.test.skip>true</maven.test.skip> in maven-antrun-plugin this gives me:
[INFO] Executing tasks
[echo] ** Displaying value of property **
[echo] /home/username/workspace/projectName/core/src/test/java
[echo] true

and with <maven.test.skip>false</maven.test.skip> in maven-antrun-plugin this gives me:
[INFO] Executing tasks
[echo] ** Displaying value of property **
[echo] /home/username/workspace/projectName/core/src/test/java
[echo] false

So, we can see that the another variable was changed.

I know that the profile was activated because I used maven-help-plugin to identify this.
The maven-help-plugin gives me this result:
The following profiles are active:

  • sahi_UI_testing (source: pom)

I've tried without maven's profile to change the project.build.testSourceDirectory property only in the <build> tag.

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

There the property was changed (but I need to assign more than one value to that property). I've tried the maven-surefire-plugin and doesn't work too.

The question is that why the project.build.testSourceDirectory isn't changed when using the profile?

4

2 回答 2

5

我发现这个问题的解决方案不是很好,但它是一个解决方案。对我来说它正在工作并且它改变了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属性更改为所需的值。

于 2013-10-03T08:20:02.173 回答
-1

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

于 2013-10-02T17:10:18.657 回答