SpringBoot Maven 常見問題

一,SpringBoot打包成jar後運行提示沒有主清單屬性

大家都知道Spring Boot 添加版本依賴有兩種方法

1,繼承入門級父級,要將您的項目配置爲繼承自spring-boot-starter-parent,請設置parent如下:

<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
</parent>

使用該設置,您還可以通過覆蓋自己項目中的屬性來覆蓋各個依賴項。例如,要升級到另一個Spring Data發佈火車,您可以將以下內容添加到您的pom.xml

<properties>
    <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>

並非每個人都喜歡從spring-boot-starter-parentPOM 繼承。您可能需要使用自己的公司標準父級,或者可能希望顯式聲明所有Maven配置。

如果您不想使用spring-boot-starter-parent,仍然可以通過使用scope=import依賴項來保留依賴項管理(而不是插件管理)的好處,如下所示:

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

如上所述,前面的示例設置不允許您使用屬性來覆蓋各個依賴項。爲了達到同樣的效果,你需要添加的條目dependencyManagement項目的之前spring-boot-dependencies條目。例如,要升級到另一個Spring Data發佈系列,可以將以下元素添加到pom.xml

<dependencyManagement>
    <dependencies>
        <!-- Override Spring Data release train provided by Spring Boot -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

如果您使用Spring Boot啓動器的父pom,則只需添加插件。除非您要更改父級中定義的設置,否則無需對其進行配置。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

而使用後者的時候需要

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${spring.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

因爲我使用spring-boot-dependencies這個BOM代替了spring-boot-starter-parent這個parent POM(詳見13.2.2. Using Spring Boot without the parent POM

導致spring-boot-maven-plugin的配置項丟失,使得打包後的jar中的MANIFEST.MF文件缺少Main-Class。

二,SpringBoot打包成jar過程中跳過測試

相信這個需求大家不陌生,每次打包的時候是默認執行測試用例的。那麼怎麼來跳過測試用例直接打包呢?

第一種 執行命令的時候添加 參數 

mvn package -Dmaven.test.skip=true 

然而每一次都加我們拒絕重複

spring-boot-maven-plugin插件已經集成了maven-surefire-plugin插件

會自動運行 junit test,只需要在pom.xml裏增加

<properties>
    <!-- <maven.test.skip>true</maven.test.skip> -->
    <skipTests>true</skipTests>
</properties>

這裏需要注意的是maven.test.skip, 不生成test的所有.class,skipTests會編譯測試類,即生成.class文件,只是不運行測試類

當然不嫌麻煩的話可以直接這樣

<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>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

三,未完待續,持續更新

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章