41

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.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Dave
  • 15,639
  • 133
  • 442
  • 830

6 Answers6

33

This worked for me:

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

OR

<configuration>
   <skip>${maven.test.skip}</skip>
</configuration>
albogdano
  • 2,710
  • 2
  • 33
  • 43
25

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

messivanio
  • 2,263
  • 18
  • 24
FrVaBe
  • 47,963
  • 16
  • 124
  • 157
10

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.

mfulton26
  • 29,956
  • 6
  • 64
  • 88
6

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.

Marinos An
  • 9,481
  • 6
  • 63
  • 96
2

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>
carlspring
  • 31,231
  • 29
  • 115
  • 197
  • My code was already part of a profile. I like your solution, but I don't want to ignore everything being executed in my profile. – Dave Apr 02 '13 at 18:09
  • Well, then I guess you should further describe your problem, as that doesn't become immediately clear. Perhaps you could provide some more details. – carlspring Apr 02 '13 at 18:17
  • My problem is -- I want to prevent the plugin (listed in the question) from executing if someone specifies "-Dmaven.test.skip=true" or "-DskipTests". I included the complete plugin info and the profile it appears within in my question. Let me know what other details you'd like me to include. – Dave Apr 02 '13 at 18:29
  • This is REALLY good for plugins that do not implement some skip functionality. You're stepping outside these plugins and taking control at a higher level. – Jason Dec 16 '13 at 17:03
1

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

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • You made up the "liquibase.should.run" property, right? How do you make that property mean that the plugin doesn't run? What code do you have to add to the liquibase plugin execution? – Dave Apr 02 '13 at 17:25