How do I skip a Maven plugin execution if "-DskipTests" or "-Dmaven.test.skip=true" is specified?
Solution 1
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 ;-)
Solution 2
This worked for me:
<configuration>
<skip>${skipTests}</skip>
</configuration>
OR
<configuration>
<skip>${maven.test.skip}</skip>
</configuration>
Solution 3
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.
Solution 4
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>
Solution 5
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.
Dave
Updated on July 10, 2020Comments
-
Dave almost 4 years
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. -
Dave about 11 yearsYou 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 about 11 yearsMy code was already part of a profile. I like your solution, but I don't want to ignore everything being executed in my profile.
-
carlspring about 11 yearsWell, then I guess you should further describe your problem, as that doesn't become immediately clear. Perhaps you could provide some more details.
-
Dave about 11 yearsMy 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.
-
Jason over 10 yearsThis 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.
-
Compeek over 6 yearsNice! This is the only answer I've found that handles both maven.test.skip and skipTests.
-
flup about 6 yearsThe link to the failsafe plugin documentation is broken.
-
BartoszMiller almost 3 yearsIt will not work for -Dpluginxyz.skip=false
-
Marinos An almost 3 years@BartoszMiller I never said that it will. It is indeed a hacky way, but it does the job.
-
Jared almost 2 years@BartoszMiller But it does work for
-Dpluginxyz.skip=false
! It will skip the tests like you wanted because you gave it a value!