java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > 使用assembly打包项目和启动脚本

springboot如何使用assembly打包项目和启动脚本

作者:向日葵的种籽

这篇文章主要介绍了springboot如何使用assembly打包项目和启动脚本问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用assembly打包项目和启动脚本

图为项目的具体布局

其中bin 是 启动脚本目录

assembly 是打包配置

pom.xml关键配置

 
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<executions>
					<execution>
						<id>bundle</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
						<configuration>
							<descriptors>
								<descriptor>src/main/assembly/repository.xml</descriptor>
							</descriptors>
						</configuration>
					</execution>
				</executions>
			</plugin>

repository.xml配置

<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>project</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false
    </includeBaseDirectory> <!-- disable the creation of root's distribution dir in the archive -->
    <fileSets>
        <!-- config files -->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <excludes></excludes>
            <includes>
                <include>*.properties</include>
                <include>*.docx</include>
                <include>*.xml</include>
            </includes>
            <fileMode>0644</fileMode>
            <outputDirectory>config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${basedir}/src/main/resources/doc</directory>
            <excludes></excludes>
            <includes>
                <include>*.docx</include>
            </includes>
            <fileMode>0644</fileMode>
            <outputDirectory>config/doc</outputDirectory>
        </fileSet>
        <!-- scripts -->
        <fileSet>
            <directory>bin</directory>
            <includes>
                <include>*.sh</include>
            </includes>
            <fileMode>0755</fileMode>
            <outputDirectory>/bin</outputDirectory>
        </fileSet>
        <!-- executable jar -->
        <fileSet>
            <directory>target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

start.sh

#!/bin/sh
#功能简介:启动上层目录下的jar文件
#参数简介:
#    $1:jar文件名(包含后缀名)
#    注意:jar文件必须位于startup.sh目录的上一层目录。
#启动参数
#JAVA_OPTS="-server -Xms400m -Xmx400m -Xmn300m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xverify:none -XX:+DisableExplicitGC -Djava.awt.headless=true"
#JAR_NAME=`ls -lt ${parent_dir}/lib | head -2 | tail -1 | awk '{print $NF}'`
this_dir="$( cd "$( dirname "$0"  )" && pwd )"
parent_dir=`dirname "${this_dir}"`
JAR_NAME=`ls -lt ${parent_dir}/lib | head -2 | tail -1 | awk '{print $NF}'`
log_dir="${parent_dir}/logs"
log_file="${log_dir}/catalina.out"
jar_file="${parent_dir}/lib/${JAR_NAME}"
 echo -e "${jar_file}"
#参数个数<1或者参数空值时,中断执行
#if [ $# -lt 1 ] || [ -z $1 ]; then
 #   echo -e "\033[31m请输入要部署的jar包名称!\033[0m"
  #  exit 1
#fi
#日志文件夹不存在,则创建
if [ ! -d "${log_dir}" ]; then
    mkdir "${log_dir}"
fi
#父目录下jar文件存在
if [ -f "${jar_file}" ]; then
    #启动jar包;重定向标准错误输出到文件,丢掉标准输出
    #java $JAVA_OPTS -jar  ${jar_file} 1>/dev/null 2>"${log_file}" &
    java  -jar ${jar_file} --spring.config.location=${parent_dir}/config/application.properties 1>/dev/null 2>"${log_file}" &
    exit 0
else
    echo -e "\033[31m${jar_file}文件不存在!\033[0m"
    exit 1
fi

springboot assembly打包及windows执行脚本

在使用assembly来打包springboot项目前,说一说springboot项目传统方式打包部署时的几个痛点:

本文提供的打包方式,使得springboot项目的部署、执行更加方便、简单。

本文采用assembly打包方式,生成zip包,部署时直接解压zip包,执行bin目录的start文件就可以运行。

1、pom.xml文件添加plugin

先修改pom文件的plugins,如下

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <mainClass>xxx.xxx.xxx</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>copy-lib</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>
          <includeScope>compile</includeScope>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <appendAssemblyId>false</appendAssemblyId>
      <descriptors>
        <descriptor>src/main/resources/assembly.xml</descriptor>
      </descriptors>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

2、创建assembly.xml文件

在resource目录下创建assembly.xml文件,配置文件放到/config目录,执行批处理文件放到/bin目录,依赖jar包放到/lib目录,执行的jar包放到zip根目录

内容如下:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
 <id>package</id>
 <formats>
    <format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
    <fileSet>
        <directory>${basedir}/src/main/resources</directory>
        <includes>
            <include>*.yml</include>
        </includes>
        <filtered>true</filtered>
        <outputDirectory>${file.separator}config</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>src/main/resources/runScript</directory>
        <outputDirectory>${file.separator}bin</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}/lib</directory>
        <outputDirectory>${file.separator}lib</outputDirectory>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
    <fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory>${file.separator}</outputDirectory>
        <includes>
            <include>*.jar</include>
        </includes>
    </fileSet>
</fileSets>
</assembly>

3、编写执行的批处理文件(或shell脚本)

在resource下建立runScript目录,在runScript目录建立Start.bat文件,脚本如下:

@echo on
set LIB_JARS=""
cd …\lib
for %%i in (*) do (
set LIB_JARS=!LIB_JARS!;…\lib%%i
)
echo !LIB_JARS!
cd …
set START_JARS=""
for %%i in (*.jar) do (
set START_JARS=%%i
)
echo !START_JARS!
java -Xms64m -Xmx1024m -XX:MaxPermSize=64M -classpath …\config;%LIB_JARS% -jar %START_JARS%
pause

到这步打包的zip包就可以直接解压执行了,不过springboot的jar还不干净,配置文件、assembly.xml、resource下的runScript目录都打包到jar包里了,这些文件都需要排除,需要配置pom.xml的resources

4、springboot打包的jar包中排除不必要的文件及目录

配置pom.xml的resources,如下:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <excludes>
      <exclude>**/application.yml</exclude>
      <exclude>**/assembly.xml</exclude>
      <exclude>runScript/**</exclude>
    </excludes>
  </resource>
</resources>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

您可能感兴趣的文章:
阅读全文