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>
原文地址:https://www.cnblogs.com/Narule/p/13200848.html