How to Skip a Maven Plugin Execution When Skipping Tests

How to Skip a Maven Plugin Execution When Skipping Tests

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Managing Maven builds can sometimes be challenging, especially when you need to conditionally execute certain plugins based on whether tests are being skipped. This guide provides a clear solution to skip a Maven plugin execution if either -DskipTests or -Dmaven.test.skip=true is specified.

Problem Statement

Imagine you have a Maven plugin that you typically want to run before your JUnit tests are executed. However, when tests are skipped using -DskipTests or -Dmaven.test.skip=true, you want to ensure that this plugin also does not run. How do you achieve this?

Solution

The most straightforward solution involves utilizing the skip property within the plugin’s configuration. Here are two approaches you can use:

Using Configuration Properties

One effective way is to directly set the skip property in your plugin’s configuration:

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

Alternatively, you can use the maven.test.skip property:

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

Using Profiles

Another robust method is to use Maven profiles to manage the execution of the plugin based on the test skip properties. This approach provides greater flexibility and control:

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

Then, in your plugin configuration, you can use the skipLiquibaseRun property to control the execution:

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

Conclusion

By leveraging configuration properties or Maven profiles, you can effectively manage the conditional execution of plugins based on whether tests are being skipped. This ensures a smoother and more controlled build process.

Automating Tests with Repeato

While managing your Maven builds, it’s also crucial to have an efficient test automation tool. Repeato is a no-code test automation tool for iOS and Android that helps you create, run, and maintain automated tests for your apps. With its intuitive test recorder and scripting interface, Repeato allows you to automate complex use cases quickly and efficiently. Additionally, Repeato can test websites inside an Android emulator or device, making it a versatile choice for your testing needs. Download Repeato today and streamline your test automation process.

Like this article? there’s more where that came from!