SpringBoot项⽬部署的打包⽅式(maven)项⽬使⽤maven打包
在springboot 项⽬中,将项⽬打包部署到服务器,使⽤maven的⽅式很常见,此处⽤到assembly插件
在项⽬代码写好,运⾏没有问题后,就可以将项⽬打包,部署到服务器
mvn
mvn是maven命令
常见的有
mvn clean # 清空maven输出(打包内容)
mvn install #项⽬打包并且放⼊maven仓库
mvn package # 项⽬打包
还可以⼀起使⽤,如 mvn clean install #先清空maven 项⽬输出,再install 打包项⽬
result
leaves项⽬l配置⽂件如下(使⽤assembly插件将配置⽂件与jar⽂件分开)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
mysql下载jar包<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&batis.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/l</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&batis.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输出
< 压缩内容
config
lib为依赖jar包⽬录,config配置⽂件⽬录,leaves-0.0.1.jar 项⽬class⽂件
这⾥⽤到 assembly插件,
1.将依赖jar包,放到lib⽬录,
2.本⾝项⽬单独⽣成jar包,放到根⽬录,并排除配置⽂件,
3.将项⽬配置⽂件放到config⽬录下(jar⽂件外,可以动态修改)
4.将上⾯三个分类⽂件统⼀打包压缩放⼊⼀个⽂件中
<!-- 将⾃⼰的项⽬单独打包成jar⽂件,并且排除 l 等配置⽂件 -->
<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/l</descriptor>
</descriptors>
<!--打包输出位置-->
<outputDirectory>output</outputDirectory>
<!-- <appendAssemblyId>false</appendAssemblyId> -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<assembly xmlns="/ASSEMBLY/2.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/ASSEMBLY/2.0.0 /xsd/assembly-2.0.0.xsd"> <id>leaves</id>
<formats>
<!--压缩⽂件形式可选 等 -->
<format&</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<!-- Adds dependencies 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&l</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>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论