本文主要 进行 spring boot 项目的编译,打包,运行,首页是项目结构图:
项目的结构图:
bin 里存放的是的jar包的执行文件,logs是日志的存放地址, pid 存放该jar包运行后的进行id
具体步骤如下:
1 pom 文件build节点:
我们需要在build节点内使用assemble 编译插件进行编译,它可以定制化我们的编译内容,使用改编译插件,我们可以将maven依赖的jar 包以及项目一块打包成压缩包。
在assemble 节点下,可以配置该插件的配置文件,这里我们将配置文件的位置放在 src/main/assembly/dist-bin.xml 。
<?xml version="1.0" encoding="utf-8"?>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<!-- 项目名 -->
<finalName>temp</finalName>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 主要使用该编译插件进行编译-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions> <!-- 配置执行器 -->
<execution>
<id>assemble</id>
<phase>package</phase> <!-- 绑定到package生命周期阶段上 -->
<goals>
<goal>single</goal> <!-- 只运行一次 -->
</goals>
<inherited>false</inherited>
<configuration>
<descriptors>
<descriptor>src/main/assembly/dist-bin.xml</descriptor> <!--配置描述文件路径-->
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
2 多环境配置
我们可以将项目配置为多个环境,并且设置默认值。
<profiles>
<profile>
<id>online</id>
<properties>
<package.environment>online</package.environment>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<package.environment>test</package.environment>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<package.environment>dev</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
3 assemble 文件配置
<?xml version="1.0" encoding="utf-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>${package.environment}-bin</id> <!-- 生成的压缩包名,生成格式为 : build 节点下的 filename + “-” + 该节点内容 -->
<formats>
<format>zip</format> <!--打包的文件格式,也可以有:war zip-->
</formats>
<includeBaseDirectory>false</includeBaseDirectory> <!--tar.gz压缩包下是否生成和项目名相同的根目录-->
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact> <!--是否把本项目添加到依赖文件夹下,默认的,可以不用设置-->
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope> <!--将scope为runtime的依赖包打包-->
</dependencySet>
</dependencySets>
<fileSets> <!--设置压缩包文件夹内容 -->
<fileSet>
<outputDirectory>conf</outputDirectory>
<filtered>true</filtered>
<directory>src/main/resources</directory>
<includes>
<include>*.xml</include>
<include>*.yml</include>
</includes>
</fileSet>
<fileSet>
<directory>bin</directory>
<fileMode>755</fileMode>
</fileSet>
<fileSet>
<directory>logs</directory>
</fileSet>
<fileSet>
<directory>pid</directory>
</fileSet>
</fileSets>
</assembly>
4 maven 打包:
执行命令: mvn clean install -Dmaven.test.skip=true -Ponline
将maven 编译环境设置为online,并且跳过测试用例 打包。
这里解释一下 maven 的p 参数和 d 参数 。
P 参数对应与maven环境,本项目为 dev , test ,online
D 参数对应与pom文件里的<properties>节点,如有一下节点:
<properties>
<attr>defaultattr</attr>
</properties>
那么 -Dattr = 1 ,那么则会将 attr 设置为 1 ,具体规则如下:
mvn -DpropertyName=propertyValue
如果propertyName不存在pom.xml,它将被设置。
如果propertyName已经存在pom.xml,其值将被作为参数传递的值覆盖-D。
如果要发送多个变量,请使用多个空格分隔符加-D:
5 bin文件内的可执行脚本:
这里我们用shell脚本编写,脚本为 deploy.sh
#!/bin/bash
## 设置项目文件夹
dir=D:\\workspace\temp
# 设置lib
lib="${dir}/lib"
# 设置可执行文件目录
bin="${dir}/bin"
#
log="${dir}/logs"
# 进入bin
cd $bin
# 这里使用 Java 的 初始参数 java.ext.dirs,并将启动日志放入 job.loog
java -Djava.ext.dirs="${lib}" Application > "${log}"/job.log
# 将上个执行命令的进程id 保存到 pid 的 pids 内
echo $! > "${dir}"/pid/pids
解释一下 java.ext.dirs (扩展包文件夹,默认为 $JAVA_HOME/lib/ext,ExtClassLoader会去加载该参数下面的jar文件 ) 参数:
【1】当我们运行jar包时,一般可以通过如下的命令:
java -cp ".\a.jar;.\b.jar" -jar myjar.jar MainClass
(-cp不支持文件夹的方式)。
【2】如果需要指定其他依赖lib包的文件夹,可以采用:
java -Djava.ext.dirs="mydirectory" -jar myjar.jar MainClass
【3】但是上面的方式会会有一个问题,就是会覆盖默认的ext值。这意味着你将失去一些功能,例如java自带的加解密算法
ext的默认值是JRE/LIB/EXT,这时我们需要将原来的参数路径加到后面,可以使用 “:“ 来进行追加
java -D 参数可以通过如下方式获取
System.out.println(System.getProperty("java.ext.dirs"));
如果该脚本是在window上进行编写,拿到linux环境上部署的话,可能脚本会执行不了,是由于二者编码格式不同。使用vim命令打开,在命令行模式下执行 set fileformat=unix ,然后 wq就可以了
6 运行:
执行 sh deploy.sh 即可执行spring boot 项目。