40

我正在使用 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>

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

4

6 回答 6

31

这对我有用:

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

或者

<configuration>
   <skip>${maven.test.skip}</skip>
</configuration>
于 2013-12-14T12:30:44.890 回答
25

您可以使用在使用单元测试跳过属性之一时被激活的配置文件来设置一个新属性(例如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>

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

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

于 2013-04-04T07:00:57.187 回答
9

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

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

然后您可以在插件配置中使用上面设置的最后一个“跳过”属性:

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

有关此答案中提到的每个不同“跳过”属性的更多详细信息,请参阅Maven Failsafe 插件:跳过测试

于 2014-04-10T21:29:45.307 回答
2

尝试将其添加到如下示例的配置文件中,(但是,这仅适用于maven.test.skip未指定的情况:

 <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>
于 2013-04-02T16:41:54.307 回答
2

对于配置中不支持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>

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

由于没有阶段调用process-test-classestrue,目标不会被执行。

于 2020-07-09T15:03:45.117 回答
1

我认为这不能直接完成,但您可以添加-Dliquibase.should.run=false以完全跳过 liquibase(请参阅http://www.liquibase.org/manual/maven_update#skip)。skipTests如果您不想同时键入这两个属性,则可以将此属性捆绑到单独的配置文件中。

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

然后你只需输入mvn install -PskipTestAndDb

于 2013-04-02T15:43:53.557 回答