羅列筆者認爲比較有用的一些maven打包插件,方便後續查閱html
springboot自帶的maven插件,可用於簡單的JAR/WAR方式打包,官方地址爲https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.htmljava
簡單的應用以下web
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--當使用springboot構建服務,則該配置可省略--> <version>2.1.4.RELEASE</version> </plugin>
當執行mvn clean package命令的時候,其會默認執行該插件的repackage任務。其會在target目錄下生成如下兩個文件spring
> ls target/*.jar > target/demo-springboot-web-0.0.1-SNAPSHOT.jar target/demo-springboot-web-0.0.1-SNAPSHOT.jar.original
其中demo-springboot-web-0.0.1-SNAPSHOT.jar.original文件,用戶可將.original後綴去掉即可獲得用戶本身編寫的項目包。apache
而demo-springboot-web-0.0.1-SNAPSHOT.jar文件每每比上面那個文件要大的多,其實其內部已經將用戶編寫的項目所相關的依賴都打進去了,百聞不如一見。
springboot
由圖中可知,相應的依賴均被打入至\BOOT-INF\lib目錄,而相應的源碼則被編譯放置到\BOOT-INF\classes目錄。服務器
上述打出的JAR包,由\META-INF\MANIFEST.MF文件的屬性Main-Class可知具體的啓動由springboot官方編寫的org.springframework.boot.loader.JarLauncher類來啓動,且其會去加載Start-Class屬性指定的類來做爲真正的啓動類。而用戶層的調用則可執行命令java -jar demo-springboot-web-0.0.1-SNAPSHOT.jar
即可app
而用戶若是想個別指定相應的main-class即可使用如下配置maven
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--當使用springboot構建服務,則該配置可省略--> <version>2.1.4.RELEASE</version> <configuration> <archive> <manifest> <mainClass>com.example.demo.DemoSpringApplication</mainClass> </manifest> </archive> </configuration> </plugin>
apache開發的maven插件,可用於複雜方式打包,好比支持ZIP、TAR等方式的輸出,有助於集成。官方地址爲http://maven.apache.org/plugins/maven-assembly-plugin/plugin-info.htmlspring-boot
英文單詞assembly帶有組裝的意思,其功能比較強大,通常用戶都比較青睞的工具。
簡單的應用以下
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.1</version> </plugin>
假若咱們直接使用mvn clean package命令,你會發現毫無有用的包打出來,看來是須要添加一些配置才能。
1.官方提供了默認的裝配方式,好比jar-with-dependencies
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.1</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> <descriptorRefs> </configuration> <!--bind goal to package--> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> <goals> </execution> </executions> </plugin>
這時候可以使用mvn clean package進行打包了,仍是以上述的項目爲例,打包後的文件出如今target目錄
> ls target\*.jar > target/demo-springboot-web-0.0.1-SNAPSHOT.jar target/demo-springboot-web-0.0.1-SNAPSHOT-jar-with-denpendencies.jar
與spring-boot-maven插件打包相似,前者爲項目源碼,後者爲相應的依賴包。簡單看下相應的依賴包狀況
由上圖可知相應的源碼也被打進包中,全部的依賴包的代碼都會打進了該包中。固然用戶若是想指定相應的啓動類,則採起archive配置便可。
2.使用最多的仍是用
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> *** <build> <finalName>demoWeb</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--若是指定爲true,則表示打出來的jar包爲可執行jar包,直接能夠在*nix服務器上執行./${name}.jar即可運行。--> <!--官方註釋不建議使用java -jar等方式調用打出來的包,當此配置爲true時--> <executable>true</executable> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.1</version> <configuration> <descriptors> <descriptor>src/main/assembly/descriptor.xml</descriptor> </descriptors> </configuration> <!--bind to package phase--> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
descriptor.xml
<?xml version="1.0" encoding="UTF-8"?> <assembly> <!-- 可自定義,這裏指定的是項目環境 --> <id>release</id> <!-- 打包的類型,若是有N個,將會打N個類型的包 --> <formats> <format>tar.gz</format> <!--<format>zip</format>--> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <!-- 0755->即用戶具備讀/寫/執行權限,組用戶和其它用戶具備讀寫權限; 0644->即用戶具備讀寫權限,組用戶和其它用戶具備只讀權限; --> <!-- 將src/main/assembly/bin目錄下的全部文件輸出到打包後的bin目錄中 --> <fileSet> <directory>${basedir}/src/main/assembly/bin</directory> <outputDirectory>bin</outputDirectory> <fileMode>0755</fileMode> <includes> <include>**.sh</include> <include>**.bat</include> </includes> </fileSet> <!-- 指定輸出resource中的配置文件到conf目錄中 --> <fileSet> <directory>${basedir}/src/main/resource</directory> <outputDirectory>conf</outputDirectory> <fileMode>0644</fileMode> <includes> <include>*.*</include> <include>**/**</include> </includes> </fileSet> <!-- 將項目啓動jar打包到./目錄中 --> <fileSet> <directory>${basedir}/target</directory> <outputDirectory>.\</outputDirectory> <fileMode>0755</fileMode> <includes> <include>${project.name}-${project.version}.jar</include> </includes> </fileSet> </fileSets> </assembly>
通過上述的配置即可指定打包成相應的.tar.gz壓縮包,默認打包出來的格式爲${project.name}-${project.version}-${assembly.id}.tar.gz,也可經過如下配置進行更改
<plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <finalName>${project.artifactId}-${project.version}</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/descriptor.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin>