How to skip maven phase pre-integration-test if skipITs is set?
Asked Answered
G

2

18

I have some integration tests that depend on test data. This test data is created in phase pre-integration-test and removed in phase post-integration-test.

My problem is that these phases are still executed if I use -DskipITs on the Maven commandline.

Is there any way to make -DskipITs also skip the pre-integration-test and post-integration-test phases?

This is the plugin definition in the pom:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies>

<configuration>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>${database.url}</url>
    <username>${database.user}</username>
    <password>${database.pw}</password>
</configuration>

<executions>
    <execution>
        <id>create-integration-test-data</id>
        <phase>pre-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <orderFile>descending</orderFile>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                    <include>AdministrationTestSetup.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>

    <execution>
        <id>remove-data-after-test</id>
        <phase>post-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>
</executions>
</plugin>
Gertrudis answered 9/12, 2014 at 14:22 Comment(2)
What plugin(s) are bound to the pre-integration-test and post-integration-test phases?Himself
Please show your pom file!Provincetown
S
7

You can use a profile activated by the presence of skipITs and the <skip> option of the Maven plugin to skip the execution of the plugin entirely. There are several ways of doing this.


Set a property to check later

You can set a Maven property inside the profile, then use that property to skip actions.

<profiles>
    <profile>
        <id>skip-integration-test-data-creation</id>
        <activation>
            <property>
                <name>skipITs</name>
            </property>
        </activation>
        <properties>
            <skip-integration-test-data-creation>true</skip-integration-test-data-creation>
        </properties>
    </profile>
</profiles>

-

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    ...
    <skip>${skip-integration-test-data-creation}</skip>
    ...
</plugin>

Put the plugins into their own profile

Another solution is to put all <plugin> sections that should not run into a seperate profile, and disable that profile when -DskipITs is active.

<profiles>
    <profile>
        <id>skip-integration-test-data-creation</id>
        <activation>
            <property>
                <!-- Disable profile if skipITs is set -->
                <name>!skipITs</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
<!-- Here: All plugins that should not run if skipITs is set -->
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Santanasantayana answered 15/7, 2015 at 13:18 Comment(3)
Hi, good answer! You were faster than me, so I took the liberty of adding my solution to your answer. That way it reads nicer than a second answer.Livi
Fine by me, but I thought the point of multiple answers was to provide different solutions (from which the OP may choose the one that best fits his needs). Anyway, you have way more experience than I do. Funny, that we both "raced" to provide an answer for a question from 2014 ;-)Santanasantayana
Yes, you have a point about multiple answers. I guess a second answer would have been ok as well - it's just that the answers are very similar, so I thought I'd combine them.Livi
C
19

Better way is to use the skipITs property to skip the execution of both pre-integration & post-integration phases.

In your case, it will look like :

<executions>
    <execution>
        <id>create-integration-test-data</id>
        <phase>pre-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <skip>${skipITs}</skip>
            <orderFile>descending</orderFile>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                    <include>AdministrationTestSetup.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>

    <execution>
        <id>remove-data-after-test</id>
        <phase>post-integration-test</phase>
        <goals>
            <goal>execute</goal>
        </goals>
        <configuration>
            <skip>${skipITs}</skip>
            <fileset>
                <basedir>${basedir}/src/test/resources/sql</basedir>
                <includes>
                    <include>AdministrationTestTeardown.sql</include>
                </includes>
            </fileset>
        </configuration>
    </execution>
</executions>

So, whenever you run mvn command with -DskipITs, it will skip the integration test as well the execution of this plugin.

Caret answered 27/9, 2015 at 6:48 Comment(3)
Is it a default property for this plugin? Your solution worked but my IDE says it cannot resolve the symbol skipITs. IDE - IntelliJ IDEA 2016.2Manville
You can ignore the IDE, it's just not aware of the implementation details of the plugin. You can put <!--suppress UnresolvedMavenProperty --> right above that line so IntelliJ ignores it too.Charmainecharmane
Great solution, but it can be improved. If we use the generic property to not execute tests (skipTests), when we indicate that we do not want tests following the standard, the plugins that we configure with: <skip>${skipTests}</skip> will not be executed either.Betel
S
7

You can use a profile activated by the presence of skipITs and the <skip> option of the Maven plugin to skip the execution of the plugin entirely. There are several ways of doing this.


Set a property to check later

You can set a Maven property inside the profile, then use that property to skip actions.

<profiles>
    <profile>
        <id>skip-integration-test-data-creation</id>
        <activation>
            <property>
                <name>skipITs</name>
            </property>
        </activation>
        <properties>
            <skip-integration-test-data-creation>true</skip-integration-test-data-creation>
        </properties>
    </profile>
</profiles>

-

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    ...
    <skip>${skip-integration-test-data-creation}</skip>
    ...
</plugin>

Put the plugins into their own profile

Another solution is to put all <plugin> sections that should not run into a seperate profile, and disable that profile when -DskipITs is active.

<profiles>
    <profile>
        <id>skip-integration-test-data-creation</id>
        <activation>
            <property>
                <!-- Disable profile if skipITs is set -->
                <name>!skipITs</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
<!-- Here: All plugins that should not run if skipITs is set -->
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Santanasantayana answered 15/7, 2015 at 13:18 Comment(3)
Hi, good answer! You were faster than me, so I took the liberty of adding my solution to your answer. That way it reads nicer than a second answer.Livi
Fine by me, but I thought the point of multiple answers was to provide different solutions (from which the OP may choose the one that best fits his needs). Anyway, you have way more experience than I do. Funny, that we both "raced" to provide an answer for a question from 2014 ;-)Santanasantayana
Yes, you have a point about multiple answers. I guess a second answer would have been ok as well - it's just that the answers are very similar, so I thought I'd combine them.Livi

© 2022 - 2024 — McMap. All rights reserved.