為什么要配置、依賴文件分離:
1.在傳統jar包中,所有文件都打包到一個jar包中,jar非常臃腫,在進行服務更新部署時非常不便,而且傳輸不穩定時導致傳輸失敗。如果過實行文件分離、在依賴不改變的情況下,僅僅上傳更新后的 編譯文件是非常xxxxxxxxxxxxxxxxxxxxxxxxxxxx方便的。
- 如果要修改一些配置文件:properties、xml,靜態文件等可以直接在服務器上編輯。
那么怎么實行配置、依賴文件分離呢?
插件介紹
- maven-jar-plugin 這個插件式專門用來打包用的,可以配置需要打包進去的文件,程序的入口類等。
- maven-resources-plugin 這個插件是用來拷貝資源文件的。
- maven-maven-dependency-plugin 這個插件是用來拷貝依賴庫的。
- maven-assembly-plugin 可以說包含了以上插件的功能,但是可以做到更精細的控制。
- spring-boot-maven-plugin 這個不用說,springboot 項目最重要的插件,整個項目的打包處理過程還是要依附於它。
打包成可執行jar,不僅僅局限SpringBoot項目(主入口函數存在)
maven-jar-plugin 插件打包jar
在pom文件中配置,但是這樣 依賴的jar並不會打進來(后面會有解決方法),適用不需要依賴文件的項目。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<!--是否添加依賴-->
<addClasspath>true</addClasspath>
<!--設置啟動類-->
<mainClass>xxx.xxx.Main</mainClass>
</manifest>
</archive>
<!--設置生成jar輸出位置-->
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
maven-assembly-plugin 插件打包jar
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!--不添加AssemblyId--> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <!--配置打包的時候一並打包依賴jar--> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <!--入口類--> <mainClass>xxx.xxx.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!--綁定生命周期--> <phase>package</phase> <goals> <!--執行assembly --> <goal>assembly</goal> </goals> </execution> </executions> </plugin>
打包SpringBoot 項目
方案一、
<plugins> <!--打包jar--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <!--MANIFEST.MF 中 Class-Path 加入前綴--> <classpathPrefix>lib/</classpathPrefix> <!--jar包不包含唯一版本標識--> <useUniqueVersions>false</useUniqueVersions> <!--指定入口類--> <mainClass>xxx.xxx.Application</mainClass> </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 加入資源文件目錄--> <Class-Path>/resources</Class-Path> </manifestEntries> </archive> <outputDirectory>${project.build.directory}/dis</outputDirectory> </configuration> </plugin> <!--拷貝依賴 copy-dependencies--> <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}/dis/lib/ </outputDirectory> </configuration> </execution> </executions> </plugin> <!--拷貝資源文件 copy-resources--> <plugin> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal