How do I skip a Maven plugin execution if "-DskipTests" or "-Dmaven.test.skip=true" is specified?

automated-tests, maven, plugins, testing

Solution

You could use profiles that get activated when using one of the unit test skip properties to set a new property (e.g. `skipLiquibaseRun`) that holds the flag if liquibase should run or not

<profiles>
    <profile>
      <id>default</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <skipLiquibaseRun>false</skipLiquibaseRun>
      </properties>
    </profile>
    <profile>
      <id>skipTestCompileAndRun</id>
      <activation>
        <property>
          <name>maven.test.skip</name>
          <value>true</value>
        </property>
      </activation>
      <properties>
        <skipLiquibaseRun>true</skipLiquibaseRun>
      </properties>
    </profile>
    <profile>
      <id>skipTestRun</id>
      <activation>
        <property>
          <name>skipTests</name>
        </property>
      </activation>
      <properties>
        <skipLiquibaseRun>true</skipLiquibaseRun>
      </properties>
    </profile>
</profiles>

Use the new property in the liquibase plugin section to decide if the run should be skipped, like this:

<configuration>
    <skip>${skipLiquibaseRun}</skip>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>jdbc:mysql://${test.mysql.db.host}:${test.mysql.db.port}/${test.mysql.db.sid}</url>
    <username>${test.mysql.db.user}</username>
    <password>${test.mysql.db.password}</password>
    <changeLogFile>${project.build.directory}/db.changelog-master.xml</changeLogFile>
    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>

Not tested, but hope it works ;-)

Problem

I'm using Maven 3.0.3. I have this plugin, which normally I want to run before my JUnit tests are executed: ``` <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <test.mysql.db.user>sbjunituser</test.mysql.db.user> <test.mysql.db.password></test.mysql.db.password> <test.mysql.db.prefix>sbjunit</test.mysql.db.prefix> <test.mysql.db.sid>${test.mysql.db.prefix}_${project.artifactId}</test.mysql.db.sid> <test.mysql.db.host>localhost</test.mysql.db.host> <test.mysql.db.port>3306</test.mysql.db.port> <test.mysql.dataSource.url>jdbc:mysql://${test.mysql.db.host}:${test.mysql.db.port}/${test.mysql.db.sid}</test.mysql.dataSource.url> <test.mysql.dataSource.driverClassName>com.mysql.jdbc.Driver</test.mysql.dataSource.driverClassName> </properties> <build> <plugins> <!-- Run the liquibase scripts --> <plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>2.0.1</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency> </dependencies> <executions> <execution> <id>build-database</id> <phase>process-test-classes</phase> <configuration> <driver>com.mysql.jdbc.Driver</driver> <url>jdbc:mysql://${test.mysql.db.host}:${test.mysql.db.port}/${test.mysql.db.sid}</url> <username>${test.mysql.db.user}</username> <password>${test.mysql.db.password}</password> <changeLogFile>${project.build.directory}/db.changelog-master.xml</changeLogFile> <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase> </configuration> <goals> <goal>update</goal> </goals> </execution> </executions> </plugin> ``` However, if someone specifies `-Dmaven.test.skip=true` or `-DskipTests`, I would like to skip this plugin from running. How do I do that? I tried changing the execution phase to "test", but then my unit tests get run before this plugin, which is not what I want.

Original source