1. 程式人生 > >springboot2.0 maven打包分離lib,resources

springboot2.0 maven打包分離lib,resources

springboot將工程打包成jar包後,會出現獲取classpath下的檔案出現測試環境正常而生產環境檔案找不到的問題,這是因為

1、在除錯過程中,檔案是真實存在於磁碟的某個目錄。此時通過獲取檔案路徑,是可以正常讀取的,因為檔案確實存在。

2、而打包成jar以後,實際上檔案是存在於jar這個檔案裡面的資原始檔,在磁碟是沒有真實路徑的(\BOOT-INF\classes!test/test.json )。所以通過ResourceUtils.getFile或者this.getClass().getResource("")方法無法正確獲取檔案。

其中的解決方案之一就是將resources資料夾下的檔案分離出來,maven打包程式碼配置如下:

 

<build>
    <finalName>api</finalName>
    <plugins>
        <!-- 分離lib -->
        <plugin>
            <!--這個外掛就是把依賴的jar包複製出來放到編譯後的target/lib目錄,並且在打包時候排除內部依賴-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 分離資原始檔 -->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/resources</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--打包jar-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <!-- 指定資原始檔目錄,與打包的jar檔案同級目錄 -->
                    <manifestEntries>
                        <Class-Path>resources/</Class-Path>
                    </manifestEntries>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.wangzhuan.api.ApiApplication</mainClass>
                    </manifest>
                </archive>
                <!-- 打包時忽略的檔案(也就是不打進jar包裡的檔案),本例將resources下的.yml、.xml、.db檔案全部排除 -->
                <excludes>
                    <exclude>**/*.yml</exclude>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.db</exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- spring boot repackage -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>non-exists</groupId>
                        <artifactId>non-exists</artifactId>
                    </include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins&