如果指定了“

编程入门 行业动态 更新时间:2024-10-17 13:36:58
如果指定了“-DskipTests”或“-Dmaven.test.skip = true”,如何跳过Maven插件执行?(How do I skip a Maven plugin execution if “-DskipTests” or “-Dmaven.test.skip=true” is specified?)

我使用的是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=true或-DskipTests ,我想跳过此插件运行。 我怎么做? 我尝试将执行阶段更改为“测试”,但随后我的单元测试在此插件之前运行,这不是我想要的。

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.

最满意答案

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

没有测试过,但希望它可以工作;-)

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

更多推荐

本文发布于:2023-04-29 03:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!