Maven 插件配置,SpringBoot 项目 mvn install 发布至服务器

664 阅读4分钟

前言

这是自己在公司编写过很多次的配置,但一直都没认真记录,记录并分享一下。这个目的是利用 CICD 自动执行 mvn 发布到研发环境中,自己的项目还能做到直接发布到的线上主机。当然有更好方案的话,希望可以得到大家的留言!

目标

假定内网目录是下图:

我们的目标是把 jar 包分包并上传到 lib 位置,resources 目录下的文件可以有选择地发布到 config 中,最后重启项目。

STEP 1 利用mvn插件对项目进行打包

  • 希望项目可以分包,而不是打成一个大包。(求讨论一下你们公司的发布方式)
        <finalName>game</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- 分包新增内容 开始 -->
                    <!-- 指定该Main Class为全局的唯一入口 -->
                    <layout>ZIP</layout>
                    <includes>
                        <include>
                            <groupId>nothing</groupId>
                            <artifactId>nothing</artifactId>
                        </include>
                    </includes>
                    <!-- 分包新增内容 结束 -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
  • 编译时不要带有任何的配置文件
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.3</version>
               <configuration>
                   <!-- 指定jdk版本 -->
                   <source>1.8</source>
                   <target>1.8</target>
                   <!-- 使用encoding参数解决java文件的编码问题 -->
                   <encoding>UTF8</encoding>
                   <excludes>
                    <!-- 编译不要带任何配置文件 -->
                       <exclude>*.xml</exclude>
                       <exclude>*.cfg</exclude>
                       <exclude>*.properties</exclude>
                       <exclude>*.proto</exclude>
                   </excludes>
               </configuration>
           </plugin>
  • 配置 jar MAINFEST.MF 的数据。主要是为了让配置独立出来,并且告诉 jar 包配置目录位置。
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
               <version>2.4</version>
               <configuration>
                   <archive>
                       <manifest>
                        <!-- 定义项目入口 -->
                           <mainClass>com.huoji.h5.center.H5CenterApplication</mainClass>
                       </manifest>
                       <manifestEntries>
                       <!-- 定义项目配置文件位置 -->
                           <Class-Path>./ ../conf/</Class-Path>
                       </manifestEntries>
                   </archive>
                   <excludes>
                       <exclude>*.yml</exclude>
                       <exclude>*.xml</exclude>
                       <exclude>*.properties</exclude>
                   </excludes>
               </configuration>
           </plugin>
位置:jar/META-INF/MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: center
Implementation-Version: 0.0.1-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: Administrator
Implementation-Vendor-Id: com.huoji
Class-Path: ./ ../conf/     // 配置路径
Spring-Boot-Version: 2.1.4.RELEASE
Main-Class: org.springframework.boot.loader.PropertiesLauncher
Start-Class: com.huoji.h5.center.H5CenterApplication        // 启动主类
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_121
  • 拷贝配置文件的到 target/conf 目录。当然,这里也可以直接通过 ssh 提交到服务器
           <plugin>
               <!-- 打包配置文件到build目录下 -->
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-resources-plugin</artifactId>
               <version>2.5</version>
               <executions>
                   <execution>
                       <configuration>
                       <!-- 输出目录 -->
                           <outputDirectory>${project.build.directory}/conf</outputDirectory>
                           <resources>
                               <resource>
                               <!-- 资源路径 -->
                                   <directory>${basedir}/src/main/resources</directory>
                                   <filtering>true</filtering>
                                   <includes>
                                       <include>*.properties</include>
                                       <include>*.xml</include>
                                       <include>*.yml</include>
                                   </includes>
                               </resource>
                           </resources>
                       </configuration>
                       <id>dev-config</id>
                       <phase>package</phase>
                       <goals>
                           <goal>copy-resources</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>

STEP 2 发布并重启。

  • 发布 jar 包到指定主机的目录,并重启。这里需要引入一个 wagon-ssh 的工具,协助我们发送 ssh 命令。
<build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.8</version>
            </extension>
        </extensions>
</build>
// ================================= 分割线 ====================================================
        <plugin>
               <!--发布配置文件,项目jar包到内网,并重启-->
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>wagon-maven-plugin</artifactId>
               <version>1.0</version>
               <executions>
                   <execution>
                       <!--发布配置文件-->
                       <id>upload</id>
                       <phase>install</phase>
                       <goals>
                           <goal>upload</goal>
                       </goals>
                       <configuration>
                           <fromDir>target/conf</fromDir>
                           <url>scp://${user}:${password}@${host}</url>
                           <toDir>${center_location}/conf</toDir>
                       </configuration>
                   </execution>
                   <execution>
                       <!--传送jar包,并重启-->
                       <id>sshexec</id>
                       <phase>install</phase>
                       <goals>
                           <goal>upload-single</goal>
                           <goal>sshexec</goal>
                       </goals>
                       <configuration>
                           <fromFile>target/H5center.jar</fromFile>
                           <url>scp://${user}:${password}@${host}/${center_location}/lib</url>
                           <commands>
                               <command>${center_restartcmd}</command>
                           </commands>
                           <displayCommandOutputs>true</displayCommandOutputs>
                       </configuration>
                   </execution>
               </executions>
           </plugin>

笔记完毕。