This is the first day of my participation in the August Challenge. For details, see:August is more challenging

The reason why I study the package optimization of SpringBoot project is that I recently learned about SpringCloud microservices. I wrote a SpringBoot project that only provides functions in eureka registration service. After 50 meters of packaging, I consider that multiple applications may actually be deployed to the same server. And that’s a big part of it

In daily work, only after fixing a bug and changing a few files, I had to repackage and then go to Sunflower to provide a package of nearly 100M on site. The speed of file transfer was really too slow

Project environment

Maven: 3.5.9 SpringBoot: 2.3.8.RELEASE Java: 1.8Copy the code

Plan 1

Packaging plug-inspring-boot-maven-plugin

When creating a project from the Spring Starter Project, it comes with this plug-in by default and automatically excludes lombok dependencies, so all we need to do is add additional configurations

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <! Select * from Main Class where Main Class is unique -->
    <mainClass>com.wx.demo.EurekaServiceApplication</mainClass>
    <! -- insert third-party JAR package into JAR -->
    <includeSystemScope>true</includeSystemScope>
    <fork>true</fork>
    <! -- set to ZIP, This mode spring - the boot - maven - the plugin will Manifest the manifest.mf in the Main - Class set to org. Springframework.. The boot loader. PropertiesLauncher - >
    <layout>ZIP</layout>
    <includes>
      <! -- Remove dependencies that do not change in a production environment -->
      <! --<excludeGroupIds> -->
      <! --org.springframework.boot -->
      <! --</excludeGroupIds> -->
      <include>
        <! -- Exclude all Jar -->
        <groupId>nothing</groupId>
        <artifactId>nothing</artifactId>
      </include>
    </includes>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
        <! -- you can package dependent packages into the generated Jar -->
      </goals>
    </execution>
  </executions>
</plugin>
Copy the code

Dependencies related to processing plug-insmaven-dependency-plugin

Adds a plug-in to copy the JAR packages used in the project to the specified path

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>copy</id>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <! -- Copy the jar package to the MVN project build path, such as the project name /target/libs/ directory -->
        <! -- <outputDirectory>${project.build.directory}/libs</outputDirectory> -->
        <! -- Put the JAR in a fixed location for easy management -->
        <outputDirectory>C:/Users/wangbin1/Desktop/libs</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>
Copy the code

To test the plug-inmaven-surefire-plugin

The Maven-Surefire-Plugin is a plugin for executing test cases in Maven. The default configuration will be used if the configuration is not displayed. The plugin’s SureFire :test command is bound by default to the Test phase of Maven execution.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <! -- Skip packaged unit tests -->
    <skipTests>true</skipTests>
  </configuration>
</plugin>
Copy the code

Disadvantages: When running the JAR package, we need to specify additional run parameters-Dloader.path=libs/Where does the virtual machine need to load the associated JAR in the project

Scheme 2

Second package plug-inmaven-jar-plugin

Replace the spring-boot-maven-plugin with a plugin that specifies where to load the dependent JAR when the jar package runs

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <! -- the runtime needs to load additional read jar locations -->
        <classpathPrefix>libs/</classpathPrefix>
        <mainClass>com.wx.demo.EurekaServiceApplication</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>
Copy the code

If the directory is libs/, this is a relative path. When we run the eureka.jar package, we need to put eureka.jar and libs/ in the same directory

|- libs
	|- a.jar
	|- b.jar
	|- xxx
|- eureka.jar
Copy the code

Or, put the libs package in the JDK environment, such as my local JDK directory C:\Program Files\Java\jdk1.8.0_181

Note: In the package plug-in,<classpathPrefix>Relative paths are configured. In the dependency management plug-in,<outputDirectory>The absolute path is configured


: /${env.java_home}/three-part-libs/eureka/ Start the application, an Exception in the thread “is the main” Java. Lang. NoClassDefFoundError: org/springframework/boot/SpringApplication

One of the purposes of this configuration is that we can simply put the jar packages that the project needs together with the JDK so that we don’t have to worry too much about deploying the server.jar

subsequent

Because it is in the study of microservices, registry, producer, consumer 3 SpringBoot project package, need to run through CMD, every time manually open CMD is very troublesome, find the relevant information online

Open a new CMD command line in the current CMD

start cmd
Copy the code

Open a CMD command line and execute a command

start cmd /k java -jar eureka-service-0.0.1.jar
Copy the code

The difference between /k and /C

/k The window is not closed after the command is executed. /C The window is closed after the command is executedCopy the code

Open the command line and run the command in the background to output the running data to the specified file

Execute a command and output the result to a file at the specified path

Overwrite write > Filepath

Append to >> Filepath

> filepathh 2>&1, 2>&1, error is directed to the output, standard input is appended to file, so error and CMD output are appended to file

cmd /c start/b java-jar > "d:/log/autopack/ webprocess.log"2> &1 "AutoPackageLine-1.jar"
Copy the code

Closes the specified process

/T terminates the specified process and the child processes enabled by it. /F Specifies to forcibly terminate the process. /IM imagename specifies the imagename of the process to be terminated. The wildcard '*' can be used as example code to close all Java processestaskkill -t -f  /im java
Copy the code

Note: Refer to CSDN blog to successfully implement SpringBoot packaging optimization