preface

This is a configuration I have written many times in the company, but have not carefully recorded, recorded and shared. The purpose is to use CICD to automatically execute MVN to publish to the research and development environment, and their own projects can be published directly to the online host. Of course, there is a better plan, I hope you can get the message!

The target

Suppose the Intranet directory is as follows:

Our goal is to subcontract the JAR package and upload it to the lib location, optionally publish the files in the Resources directory to config, and finally restart the project.

STEP 1 usingmvnPlug-ins package projects

  • Hope the project can be subcontracted, not into a big package. (Please discuss your company’s distribution method)
<finalName>game</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <! -- Start subcontracting new content --> <! <layout>ZIP</layout> <includes> <include> <groupId>nothing</groupId> <artifactId>nothing</artifactId> </include> </includes> <! Executions > <execution> <goals> <goal>repackage</goal> </execution> </executions> </plugin>Copy the code
  • Do not compile with any configuration files
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> The < version > 3.3 < / version > < configuration > <! --> <source> 1.8 < /source> < target > 1.8 < / target > <! -- Use encoding parameter to resolve Java file encoding issues --> <encoding>UTF8</encoding> < Excludes > <! </exclude> <exclude>*.cfg</exclude> <exclude>*.properties</exclude> <exclude>*.proto</exclude> </excludes> </configuration> </plugin>Copy the code
  • Configure the data for JAR Mainfest. MF. The main purpose is to make the configuration independent and tell the JAR package where the configuration directory is.
< plugin > < groupId > org. Apache. Maven. Plugins < / groupId > < artifactId > maven - jar - plugin < / artifactId > < version > 2.4 < / version > <configuration> <archive> <manifest> <! - define the project entry - > < mainClass > com. Huoji. H5. Center. H5CenterApplication < / mainClass > < / manifest > < manifestEntries > <! -- Define project configuration file location --> < class-path >./.. /conf/</Class-Path> </manifestEntries> </archive> <excludes> <exclude>*.yml</exclude> <exclude>*.xml</exclude> <exclude>*.properties</exclude> </excludes> </configuration> </plugin>Copy the code
Jar/meta-INF/manifest.mf manifest-version: 1.0 implementation-title: center implementation-version: 0.0.1 -snapshot archiver-version: Plexus Archiver built-in: Administrator implementation-vendor-id: com.huoji Class-Path: ./ .. /conf/ / Configure path spring-boot-version: 2.1.4.RELEASE main-class: org.springframework.boot.loader.PropertiesLauncher Start-Class: Com. Huoji. H5. Center. H5CenterApplication / / start the main class Spring - the Boot - Classes: the Boot - INF/Classes/Spring - the Boot - Lib: Boots-inf /lib/ created-by: Apache Maven 3.3.9 build-JDK: 1.8.0_121Copy the code
  • Copy the configuration file to the target/conf directory. Of course, you can also SSH directly to the server
<plugin> <! GroupId >org.apache.maven.plugins</groupId> <artifactId> Maven-resources-plugin </artifactId> <version>2.5</version> <executions> <configuration> <! --> <outputDirectory>${project.build.directory}/conf</outputDirectory> <resources> <resource> <! -- Resource path --> <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>Copy the code

STEP 2 is released and restarted.

  • Publish the JAR package to the directory on the specified host and restart. Here we need to introduce a tool for wagon- SSH to help us send SSH commands.
<build> <extensions> <extension> <groupId>org.apache.maven.wagon</groupId> <artifactId>wagon-ssh</artifactId> < version > 2.8 < / version > < / extension > < / extensions > < build > / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = line ==================================================== <plugin> <! Publish config file, project JAR package to Intranet, Codehaus. mojo</groupId> <artifactId> Wagon -maven-plugin</artifactId> <version>1.0</version> <executions> <execution> <! 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> <! -- Transfer jar package, 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>
Copy the code

That’s it.