Dave
Dave

Reputation: 19160

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

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.

Upvotes: 43

Views: 67117

Answers (6)

Marinos An
Marinos An

Reputation: 10826

For plugins that do not support skip in the configuration there is another hacky way to skip without the use of profiles.

In the phase declaration you can append an attribute pluginxyz.skip

e.g.

<properties>
   <property>
      <pluginxyz.skip><pluginxyz.skip>
   </property>     
</properties>
...
<plugin>
    <groupId>org.xyz</groupId>
    <artifactId>xyz-maven-plugin</artifactId>
    <version>2.0.1</version>
    <executions>
      <execution>
        <id>build-database</id>
        <phase>process-test-classes${pluginxyz.skip}</phase>
        <configuration>....</configuration>
     </execution>
<plugin>

Then when executing from the command line you can add -Dpluginxyz.skip=true which will append the true value to the phase name.

Since there is no phase called process-test-classestrue the goal will not be executed.

Upvotes: 8

mfulton26
mfulton26

Reputation: 31234

I think the most straight-forward way is to cascade the "skip" properties in your POM:

<properties>
  <maven.test.skip>false</maven.test.skip>
  <skipTests>${maven.test.skip}</skipTests>
  <skipITs>${skipTests}</skipITs>
</properties>

Then you can use the last "skip" property set above in your plugin's configuration:

<configuration>
  <skip>${skipITs}</skip>
</configuration>

See Maven Failsafe Plugin: Skipping Tests for more details on each of the different "skip" properties mentioned in this answer.

Upvotes: 11

albogdano
albogdano

Reputation: 2850

This worked for me:

<configuration>
   <skip>${skipTests}</skip>
</configuration>

OR

<configuration>
   <skip>${maven.test.skip}</skip>
</configuration>

Upvotes: 34

FrVaBe
FrVaBe

Reputation: 49341

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

Upvotes: 26

artbristol
artbristol

Reputation: 32407

I don't think this can be done directly, but you can add -Dliquibase.should.run=false to skip liquibase entirely (see http://www.liquibase.org/manual/maven_update#skip). You can bundle this property and skipTests into a separate profile if you don't want to type both.

<profiles>
    <profile>
        <id>skipTestAndDb</id>
        <properties>
            <skipTests>true</skipTests>
            <liquibase.should.run>false</liquibase.should.run>
        </properties>
    </profile>
</profiles>

Then you would just type mvn install -PskipTestAndDb

Upvotes: 1

carlspring
carlspring

Reputation: 32627

Try adding it to a profile like the example below, (however, this will only work for the cases when maven.test.skip is not specified:

 <profiles>
    <profile>
        <id>execute-liquibase</id>
        <activation>
            <property>
                <name>!maven.test.skip</name>
            </property>
        </activation>
        <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>
            </plugins>
        </build>
    </profile>
 <profiles>

Upvotes: 2

Related Questions