Maven跳過測試


Maven跳過測試

參考

http://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html

http://maven.apache.org/plugins/maven-compiler-plugin/testCompile-mojo.html

http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html

boolean - Set this to "true" to bypass unit tests entirely. Its use is NOT RECOMMENDED, especially if you enable it using the "maven.test.skip" property, because maven.test.skip disables both running the tests and compiling the tests. Consider using the skipTests parameter instead. Default value is: false. User property is: maven.test.skip.
boolean 2.4 Set this to "true" to skip running tests, but still compile them. Its use is NOT RECOMMENDED, but quite convenient on occasion. Failsafe plugin deprecated the parameter skipTests and the parameter will be removed in Failsafe 3.0.0 as it is a source of conflicts between Failsafe and Surefire plugin. Default value is: false. User property is: skipTests.

環境

E:\mozq\demo_project\shiro>mvn -v
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: D:\tools\maven\apache-maven-3.6.1\bin\..
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: D:\tools\jdk\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

案例一 跳過運行測試和編譯測試

配置

<maven.test.skip>true</maven.test.skip> 跳過運行測試,測試資源,測試代碼編譯

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<!-- 
    這個屬性被3個插件使用,跳過testResources,testCompile,test。不處理測試,會快一點。
    maven-resources-plugin:3.1.0:testResources
    maven-compiler-plugin:3.8.1:testCompile
    maven-surefire-plugin:2.22.2:test
 -->
    <maven.test.skip>true</maven.test.skip>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

運行結果

[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ http_01 ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ http_01 ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ http_01 ---
[INFO] Tests are skipped.

案例二 只跳過運行測試

配置

<skipTests>true</skipTests> 只跳過運行測試,不跳過測試資源和測試代碼編譯。

<properties>
    <skipTests>true</skipTests>
    <!--<maven.test.skip>true</maven.test.skip>-->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>6</maven.compiler.source>
    <maven.compiler.target>6</maven.compiler.target>
    <spring.version>4.3.2.RELEASE</spring.version>
    <mybatis.version>3.2.6</mybatis.version>
</properties>
<build>
    <!-- 資源文件 -->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>


    <!-- 插件jetty tomcat -->
    <plugins>
        <!-- 配置jetty運行方式 -->
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.3.0.M2</version>
            <configuration>
                <webAppConfig>
                    <contextPath>/xinhekqsys</contextPath>
                </webAppConfig>
                <httpConnector>
                    <port>8081</port>
                    <idleTimeout>10000</idleTimeout>
                </httpConnector>
            </configuration>
        </plugin>

        <!-- maven項目在tomcat下運行的配制文件 -->
        <!-- 配置tomcat運行方式 -->
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <!-- 注意tomcat7此處的url -->
                <url>http://localhost:8080/manager/text</url>
                <!-- 此處的名字必須和setting.xml中配置的ID一致 -->
                <server>tomcat</server>
                <!-- 此處的名字是項目發布的工程名 -->
                <path>/xinhekqsys</path>
            </configuration>
        </plugin>
        <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> 
    <version>2.4</version> <configuration> <webResources> <resource> <directory>src/main/webapp/WEB-INF</directory> 
    </resource> </webResources> </configuration> </plugin> -->

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> 
    <version>2.4</version> <configuration> <webResources> <resource> <directory>src/main/webapp/WEB-INF</directory> 
    <directory>src/main/webapp</directory> </resource> </webResources> </configuration> 
    </plugin> -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>${project.build.sourceEncoding}</encoding>
                <compilerArguments>
                    <verbose /> <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
                </compilerArguments>
            </configuration>
        </plugin>

    </plugins>
    <!-- 最終項目打包包名 -->
    <finalName>handordering</finalName>
</build>

運行結果

<skipTests>true</skipTests>
# 沒有跳過測試資源 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ handordering ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\mozq\02 project\handordering\src\test\resources
[INFO] 
# 沒有跳過測試代碼編譯
[INFO] --- maven-compiler-plugin:3.6.0:testCompile (default-testCompile) @ handordering ---
[INFO] No sources to compile
[INFO] 
# 跳過了運行測試
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ handordering ---
[INFO] Tests are skipped.
<maven.test.skip>true</maven.test.skip>
# 跳過了測試資源 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ handordering ---
[INFO] Not copying test resources
[INFO] 
# 跳過了測試代碼編譯
[INFO] --- maven-compiler-plugin:3.6.0:testCompile (default-testCompile) @ handordering ---
[INFO] Not compiling test sources
[INFO] No sources to compile
[INFO] 
# 跳過了運行測試
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ handordering ---
[INFO] Tests are skipped.

案例三 插件中配置跳過

插件和用戶屬性中同時對是否跳過測試進行配置,會以插件中為准。但是當跳過了測試資源和測試代碼編譯,又設置不跳過運行測試沒有意義,因為此時不會生成測試代碼。

配置

<properties>
    <!-- 配置了跳過測試用戶屬性 -->
    <maven.test.skip>true</maven.test.skip>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <!-- 插件中配置不跳過運行測試,以插件中配置為准。
				當跳過了測試資源和測試代碼編譯,又設置不跳過運行測試沒有意義,因為此時不會有測試代碼來運行。
			-->
            <configuration>
                <skip>false</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

運行結果

# 跳過了測試資源
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ http_01 ---
[INFO] Not copying test resources
# 跳過了測試代碼編譯
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ http_01 ---
[INFO] Not compiling test sources
# 沒有跳過運行測試,但是因為沒有編譯的測試代碼,打印了沒有測試需要運行。
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ http_01 ---
[INFO] No tests to run.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM