springboot+maven打包成bin+conf+lib目录结构
Healthy Mind Lv3

环境准备

  1. maven
  2. springboot

需求:当我们完成一个项目需要部署到 Linux上运行的时候 一般的项目结构都是如下:

​ 方便运行和修改配置

打包配置

  1. 这里打包需要在项目编译完成后生成*.jar后执行命令所以使用 maven-antrun-plugin 在项目pom.xml中添加如下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
    <execution>
    <id>copy-jar-file</id>
    <phase>package</phase>
    <goals>
    <goal>run</goal>
    </goals>
    <configuration>
    <target>
    <!--todir 目的地的文件路径
    dir 源文件路径
    include 需要拷贝的文件
    ${build.package} 这里是打包后生成的项目目录
    -->
    <copy todir="${project.basedir}/${build.package}/lib">
    <fileset dir="${project.basedir}/target">
    <include name="*.jar" />
    </fileset>
    </copy>

    <copy todir="${project.basedir}/${build.package}/conf">
    <fileset dir="${project.basedir}/target/classes">
    <include name="*.yml" />
    <include name="*.yaml" />
    <include name="*.xml" />
    <include name="*.properties" />
    </fileset>
    </copy>

    <copy todir="${project.basedir}/${build.package}/bin">
    <fileset dir="${project.basedir}">
    <include name="*.sh" />
    </fileset>
    </copy>
    </target>
    </configuration>
    </execution>
    </executions>
    </plugin>
  2. 在项目根目录下创建一个XX.sh的文件全部内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #!/bin/bash
    case $1 in
    start)
    java -Dfile.encoding=UTF-8 -jar lib/kb-data-rest-0.0.1-SNAPSHOT.jar --spring.config.location=conf/application.properties > logs.out &
    ;;
    stop)
    ps -ef|grep lib/kb-data-rest-0.0.1-SNAPSHOT* |grep -v grep |awk '{print $2}' | sed -e "s/^/kill -9 /g" | sh -
    ;;
    restart)
    "$0" stop
    sleep 3
    "$0" start
    ;;
    status) ps -ef|grep lib/kb-data-rest-0.0.1-SNAPSHOT*
    ;;
    *)
    echo "Example: lpservice.sh [start|stop|restart|status]" ;;
    esac

    这里的 kb-data-rest-0.0.1-SNAPSHOT 名字是jar的名字根据实际情况修改。

​ 最终打包出来 目录结构如下:

1
2
3
4
5
6
7
8

├── bin
| └── kb-data-rest.sh
├── conf
| └── application.properties
└── logback-spring.xml
├── lib
| └── kb-data-rest-0.0.1-SNAPSHOT.jar

拷贝到 Linux 服务执行命令 启动命令

1
./bin/kb-data-rest.sh start 

如果没有可执行权限,执行命令

1
chmod +x ./bin/kb-data-rest.sh

然后再 运行启动命令 ;

结束;