如果指定了"-DskipTests"或"-Dmaven.test.skip = true",如何跳过Maven插件执行?

Dav*_*ave 37 testing plugins automated-tests maven

我正在使用Maven 3.0.3.我有这个插件,通常我想在执行JUnit测试之前运行它:

    <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>
Run Code Online (Sandbox Code Playgroud)

但是,如果有人指定-Dmaven.test.skip=true或者-DskipTests,我想跳过这个插件来运行.我怎么做?我尝试将执行阶段更改为"test",但之后我的单元测试在此插件之前运行,这不是我想要的.

alb*_*ano 25

这对我有用:

<configuration>
   <skip>${skipTests}</skip>
</configuration>
Run Code Online (Sandbox Code Playgroud)

要么

<configuration>
   <skip>${maven.test.skip}</skip>
</configuration>
Run Code Online (Sandbox Code Playgroud)


FrV*_*aBe 21

您可以使用在使用单元测试跳过属性之一时激活的配置文件来设置一个新属性(例如skipLiquibaseRun),如果liquibase应该运行,则该属性保存该标志

<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>
Run Code Online (Sandbox Code Playgroud)

使用liquibase插件部分中的新属性来决定是否应该跳过运行,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

没有经过测试,但希望它有效;-)


mfu*_*n26 6

我认为最直接的方法是在POM中级联"跳过"属性:

<properties>
  <maven.test.skip>false</maven.test.skip>
  <skipTests>${maven.test.skip}</skipTests>
  <skipITs>${skipTests}</skipITs>
</properties>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在插件的配置中使用上面设置的最后一个"skip"属性:

<configuration>
  <skip>${skipITs}</skip>
</configuration>
Run Code Online (Sandbox Code Playgroud)

有关本答案中提到的每个不同"跳过"属性的更多详细信息,请参阅Maven故障安全插件:跳过测试.

  • 谢谢@flup.我已经更新了链接. (2认同)

Mar*_* An 6

对于配置中不支持的插件,还有另一种skip不使用配置文件的方式可以跳过。

在阶段声明中,您可以附加一个属性pluginxyz.skip

例如

<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>
Run Code Online (Sandbox Code Playgroud)

然后,当从命令行执行时,您可以添加-Dpluginxyz.skip=truetrue值附加到阶段名称的内容。

由于没有称为process-test-classestrue目标的阶段,因此不会执行。