在Spring Boot項(xiàng)目中,通過(guò)Maven插件的配置,我們可以定制項(xiàng)目的打包過(guò)程,將依賴項(xiàng)抽取到指定的lib
目錄中。本文將演示如何使用Spring Boot Maven Plugin進(jìn)行項(xiàng)目打包,同時(shí)抽取依賴項(xiàng)到lib
目錄,并提供相應(yīng)的啟動(dòng)命令。
1. 配置Spring Boot Maven Plugin
首先,在項(xiàng)目的pom.xml
文件中,修改Spring Boot Maven Plugin的配置以適應(yīng)項(xiàng)目的需求。
<build>
<plugins>
<!-- Spring Boot Maven Plugin配置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 項(xiàng)目的啟動(dòng)類 -->
<mainClass>com.org.testDemo</mainClass>
<!-- 解決windows命令行窗口中文亂碼 -->
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
<layout>ZIP</layout>
<!-- 配置需要打包進(jìn)項(xiàng)目的jar 編寫代碼更改比較頻繁的模塊-->
<includes>
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
<!-- 如需,排除lombok-->
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 此插件用于將依賴包抽出 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<!-- 此插件用于創(chuàng)建lib目錄 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>create-lib-directory</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<mkdir dir="${project.basedir}/lib"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2. 執(zhí)行打包命令
使用以下Maven命令進(jìn)行打包:
mvn clean package
打包完成后,你將在target
目錄下得到一個(gè)testDemo.jar文件和lib目錄,其中lib
目錄中包含了所有的三方依賴。
3. 啟動(dòng)命令
使用以下命令啟動(dòng)項(xiàng)目:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-834543.html
java -jar -Dloader.path=lib testDemo.jar
通過(guò)以上步驟,你成功地將Spring Boot項(xiàng)目打包,并將三方依賴以及項(xiàng)目本身打包成一個(gè)可執(zhí)行的JAR文件,同時(shí)將依賴抽取到與JAR同級(jí)的lib
目錄。啟動(dòng)命令中的-Dloader.path=lib
指定了加載依賴項(xiàng)的路徑,確保項(xiàng)目能夠正確加載lib
目錄下的所有依賴。這樣的部署方式既方便管理依賴,又使得項(xiàng)目的啟動(dòng)和部署更加清晰。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-834543.html
到了這里,關(guān)于Spring Boot項(xiàng)目打包及依賴管理-瘦身的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!