SpringBoot項目部署的打包方式(maven)


項目使用maven打包

在springboot 項目中,將項目打包部署到服務器,使用maven的方式很常見,此處用到assembly插件
在項目代碼寫好,運行沒有問題后,就可以將項目打包,部署到服務器

leaves project url:http://github.com/wunanyu/leaves

mvn

mvn是maven命令

常見的有

mvn clean # 清空maven輸出(打包內容)

mvn install #項目打包並且放入maven倉庫

mvn package # 項目打包

還可以一起使用,如 mvn clean install #先清空maven 項目輸出, 再install 打包項目

result

pom.xml

leaves項目pom.xml配置文件如下(使用assembly插件將配置文件與jar文件分開)

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
    <!-- 項目信息 -->
	<groupId>com.domoment</groupId>
	<artifactId>leaves</artifactId>
	<version>0.0.1</version>
	<name>leaves</name>
	<description>Demo project for Spring Boot</description>
	<!-- jdk版本 -->
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<!-- 項目依賴 -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
		</dependency>
	</dependencies>
	<!-- maven構建配置 -->
	<build>
	      <plugins>
                    <plugin>
                      <groupId>org.apache.maven.plugins</groupId>
                      <artifactId>maven-jar-plugin</artifactId>
                      <configuration>
                          <archive>
                              <manifest>
                              <!-- 依賴文件目錄 -->
                                  <addClasspath>true</addClasspath>
                                  <classpathPrefix>lib/</classpathPrefix>
                                  <!-- 啟動類 -->
                                  <mainClass>com.domoment.leaves.LeavesApplication</mainClass>
			      </manifest>
                         <!-- 配置文件目錄 -->
			      <manifestEntries>
				<Class-Path>config/</Class-Path>
		              </manifestEntries>
			 </archive>
                      <!-- 排除配置文件(使jar包不含配置文件) -->
			 <excludes>
			      <exclude>*.properties</exclude>
			      <exclude>**/*.properties</exclude>
			      <exclude>*.yml</exclude>
			      <exclude>*.xml</exclude>
			      <exclude>**/*.xml</exclude>
			      <exclude>*.conf</exclude>
			 </excludes>
	             </configuration>
	           </plugin>
            <!-- assembly插件 -->
	           <plugin>
		         <groupId>org.apache.maven.plugins</groupId>  
		         <artifactId>maven-assembly-plugin</artifactId>  
		         <version>2.4</version>
			 <configuration>  
		               <finalName>domoment</finalName>  
			       <descriptors>
                        <!-- assembly配置文件(配置如何打包項目) -->
			             <descriptor>/src/main/resources/assembly.xml</descriptor>  
			       </descriptors>
			       <outputDirectory>output</outputDirectory>
			      <!-- <appendAssemblyId>false</appendAssemblyId> -->
		        </configuration>
	                <executions>  
		              <execution>  
			            <phase>package</phase>  
			            <goals>  
			                  <goal>single</goal>  
			            </goals>  
            		      </execution>  
		        </executions>  
                  </plugin>
            </plugins>
	</build>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.mybatis.spring.boot</groupId>
				<artifactId>mybatis-spring-boot-starter</artifactId>
				<version>2.1.1</version>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

打包方式

所有文件打成一個包

包括依賴的jar文件,保持springboot-maven啟動時初始樣子就好,什么也不用配置

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

文件大小為 30M左右,因為包括依賴jar包,全部打包進一個jar文件中

依賴放在lib包

將自己寫的源碼分開打成一個jar包,依賴jar文件統一放在lib目錄下

<!-- 將自己的項目代碼打包成jar文件 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <!-- 指定包含的依賴文件位置 -->
                <classpathPrefix>lib/</classpathPrefix>
                <!--指定啟動類-->
				<mainClass>com.domoment.leaves.LeavesApplication</mainClass>
			</manifest>
            
        </archive>
    </configuration>
</plugin>
<!-- 將依賴的jar文件全部放到lib目錄下 -->
<plugin>
    <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>

項目源代碼生成的jar包文件大小只有27KB

其他依賴放入lib目錄

配置文件外置,依賴放到lib目錄

雖然前面配置已經做到將依賴jar文件與項目jar文件區分開,但是配置文件依然在項目jar文件內,有些時候,只是需要改動配置文件,而不需要修改源代碼,因此配置文件放在jar文件外,會方便些。這里配置項目單獨打包jar包,並提取config配置文件到項目jar包外面

assembly輸出

tar.gz 壓縮內容

config

lib為依賴jar包目錄,config配置文件目錄,leaves-0.0.1.jar 項目class文件

這里用到 assembly插件,

1.將依賴jar包,放到lib目錄,

2.本身項目單獨生成jar包,放到根目錄,並排除配置文件,

3.將項目配置文件放到config目錄下(jar文件外,可以動態修改)

4.將上面三個分類文件統一打包壓縮放入一個文件中tar.gz

<!-- 將自己的項目單獨打包成jar文件,並且排除 application.yml 等配置文件 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
				<mainClass>com.domoment.leaves.LeavesApplication</mainClass>
             </manifest>
            <!-- 指定配置文件所在的文件夾 -->
            <manifestEntries>
                <Class-Path>config/</Class-Path>
            </manifestEntries>
        </archive>
        <!--將配置文件排除掉,不打包到jar文件內-->
        <excludes>
            <exclude>*.properties</exclude>
            <exclude>**/*.properties</exclude>
            <exclude>*.yml</exclude>
            <exclude>*.xml</exclude>
            <exclude>**/*.xml</exclude>
            <exclude>*.conf</exclude>
        </excludes>
    </configuration>
</plugin>

<!-- assembly插件 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-assembly-plugin</artifactId>  
    <version>2.4</version>
    <configuration>  
        <finalName>domoment</finalName>
        <!--指定assembly配置文件配置-->
        <descriptors>
            <descriptor>/src/main/resources/assembly.xml</descriptor>  
        </descriptors>
        <!--打包tar.gz輸出位置-->
        <outputDirectory>output</outputDirectory>
        <!-- <appendAssemblyId>false</appendAssemblyId> -->
    </configuration>
    <executions>  
        <execution>  
            <phase>package</phase>  
            <goals>  
                <goal>single</goal>  
            </goals>  
        </execution>  
    </executions>  
</plugin>

assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>leaves</id>
    <formats>
        <!--壓縮文件形式 可選 zip tar.gz等 -->
        <format>tar.gz</format>
    </formats>

	<includeBaseDirectory>true</includeBaseDirectory>

	<!-- Adds dependencies to tar.gz package under lib directory -->
    
    <!-- 依賴jar文件處理 全部放入lib目錄 -->
    <dependencySets>
        <dependencySet>
            <!-- 本項目所生成的jar包不放入lib文件夾中 -->
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!-- <unpack>false</unpack> -->

        </dependencySet>
    </dependencySets>
    
    
	<!-- 項目文件處理 -->
    <fileSets>
        <!-- 配置文件放到config文件夾內 -->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>**</include>
            </includes>
            <!-- <excludes>
                <exclude>assembly.xml</exclude>
            </excludes> -->
            <filtered>true</filtered>
            <!--配置文件輸出位置 根目錄config文件夾下-->
            <outputDirectory>${file.separator}config</outputDirectory>
        </fileSet>

        <!-- 自己寫的項目代碼生成的jar文件 放在根目錄  -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM