最近項目在作微服務的拆分,那麼咱們想讓咱們的容器啓動更加的輕量級,因此咱們選擇放棄tomcat等容器,而是經過maven生成帶有指定依賴的可執行jar包的方式進行處理,本文我將分享如何經過maven的maven-jar-plugin打包插件來進行生成目的可jar包。java
1.原理git
咱們能夠經過修改maven 打jar包的maven-jar-plugin插件的配置信息來生成咱們須要的指定依賴的可執行jar包。github
2.步驟:在項目的pom.xml文件中修改默認的jar插件spring
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifest> <!--運行jar包時運行的主類,要求類全名--> <mainClass>com.hafiz.Runner</mainClass> <!-- 是否指定項目classpath下的依賴 --> <addClasspath>true</addClasspath> <!-- 指定依賴的時候聲明前綴 --> <classpathPrefix>./lib/</classpathPrefix> <!--依賴是否使用帶有時間戳的惟一版本號,如:xxx-1.3.0-20121225.012733.jar--> <useUniqueVersions>false</useUniqueVersions> </manifest> </archive> </configuration> </plugin> </plugins> </build>
接着咱們還要配置maven的maven-dependency-plugin插件把當前項目的全部依賴放到target目錄下的lib文件夾下express
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-dependency-plugin</artifactId> 4 <executions> 5 <execution> 6 <id>copy</id> 7 <phase>package</phase> 8 <goals> 9 <goal>copy-dependencies</goal> 10 </goals> 11 <configuration> 12 <outputDirectory>${project.build.directory}/lib</outputDirectory> 13 </configuration> 14 </execution> 15 </executions> 16 </plugin>
其中,${project.build.directory}表示默認的target文件夾。apache
咱們經過上文的修改便完成了適用maven生成指定依賴的可執行jar包。tomcat
咱們發現生成的manifest文件中已經設置好了Main-Class以及Class-Path,以下:app
若是<addClasspath>設置爲false,則生成的manifest文件中不會聲明依賴(即不會有Class-Path聲明)maven
3.主類微服務
package com.hafiz; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Desc:主類 * Created by hafiz.zhang on 2018/4/07. */ public class Runner { public static void main(String[] args) { ApplicationContext context
= new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); System.out.println("context:" + context.getClass()); System.out.println("The Main Class Is Running...."); } }
4.pom.xml
<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> <groupId>com.hafiz</groupId> <artifactId>assembly-demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>assembly-demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.6.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifest> <!--運行jar包時運行的主類,要求類全名--> <mainClass>com.hafiz.Runner</mainClass> <!-- 是否指定項目classpath下的依賴 --> <addClasspath>true</addClasspath> <!-- 指定依賴的時候聲明前綴 --> <classpathPrefix>./lib/</classpathPrefix> <!--依賴是否使用帶有時間戳的惟一版本號,如:xxx-1.3.0-20121225.012733.jar--> <useUniqueVersions>false</useUniqueVersions> </manifest> </archive> </configuration> </plugin> <!--把當前項目全部的依賴打包到target目錄下的lib文件夾下--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <!--已存在的Release版本不重複copy--> <overWriteReleases>false</overWriteReleases> <!--已存在的SnapShot版本不重複copy--> <overWriteSnapshots>false</overWriteSnapshots> <!--不存在或者有更新版本的依賴才copy--> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
咱們如今進入生成的jar包所在的文件夾下,使用 java -jar xxx.jar來執行生成的jar包
到此咱們就完成了如何使用maven的jar包生成插件來進行生成指定依賴的可執行jar包。