Maven project.build.testSourceDirectory property n

2020-04-09 23:28发布

问题:

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?

回答1:

I found a not very nice solution to this question but it's a solution. For me it's working and it changes the testSourceDirectory variable.

In the pom.xml file I've declared an own variable in the properties tag initialized with the testSourceDirectory's default value:

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

My profile is:

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

At the start of build tag I've set the testSourceDirectory to

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

So, when we don't use the profile we have a default value to the myTestSourceDirectory variable and when we use a the profile the variable will be changed to the requested testing directory. The variable always exists, and in the build tag we change the testSourceDirectory property to the desired value.



回答2:

You can't overwrite the project model with properties, Maven doesn't allow that. The only option you have is by specifying the testSourceDirectory in the plugin you want to use (assuming it's available).



标签: maven pom.xml