SpringBoot's maven plugin

1. Description  

  SpringBoot's Maven plug-in can provide SpringBoot support for applications in a Maven manner, which means that SpringBoot applications can perform Maven operations.
  SpringBoot's Maven plugin can package a SpringBoot application as an executable jar or war package, and then run the SpringBoot application in the usual way, such as java -jar.

2. Corresponding plugin

  5 Goals of Spring Boot Maven plugin:

  • repackage, the default goal. After the mvn package, package the executable jar / war again, while retaining the jar / war generated by the mvn package as .origin
  • run, run SpringBoot application
  • start, in the mvn integration-test stage, manage the SpringBoot application life cycle
  • stop, in the mvn integration-test phase, manage the SpringBoot application lifecycle
  • build-info, generate the build information file build-info.properties used by Actuator

3. Related configuration

The corresponding pom.xml file configuration is as follows:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.xxx.bs.SearchBsBootstrap</mainClass>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

4. Repackage description

  The main goal of the Spring Boot Maven plugin is repackage, which can package the package generated by mvn package as an executable package again in the Maven package life cycle stage, and rename the package generated by mvn package to * .original

  Based on the above configuration, execute the following command on a project that generates the Jar software package.

mvn package spring-boot:repackage

   You can see the two jar files generated, one is * .jar and the other is * .jar.original.

  In the process of executing the above command, Maven first packaged and generated the * .jar file in the package stage; then executed spring-boot: repackage to repackage, looking for the Main-Class attribute configured in the Manifest file, as shown below:

Manifest-Version: 1.0
Implementation-Title: search-bs
Implementation-Version: 0.1.0
Archiver-Version: Plexus Archiver
Built-By: sam
Implementation-Vendor-Id: org.springframework
Spring-Boot-Version: 2.1.2.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.xxx.bs.SearchBsBootstrap
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.6.0
Build-Jdk: 1.8.0_155

  Note that the Main-Class attribute value is org.springframework.boot.loader.JarLauncher;

  Start-Class attribute value is com.xxx.bs.SearchBsBootstrap

  The com.xxx.bs.SearchBsBootstrap class defines the main () method, which is the entry point of the program.

  Usually, the SpringBoot Maven plugin will automatically set the Main-Class attribute for the Manifest file during the packaging process. In fact, the geometry of this attribute can also be controlled by the configuration attribute layout of the SpringBoot Maven plugin. Examples are as follows:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <version>2.1.2.RELEASE</version>
  <configuration>
    <mainClass>${start-class}</mainClass>
    <layout>ZIP</layout>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>repackage</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Note that the layout attribute value here is ZIP.

The value of the layout attribute can be as follows:

  • JAR, the usual executable jar

   Main-Class: org.springframework.boot.loader.JarLauncher

  • WAR, the usual executable war, the required servlet container depends on WEB-INF / lib-provided

   Main-Class: org.springframework.boot.loader.warLauncher

  • ZIP, or DIR, is similar to JAR

   Main-Class: org.springframework.boot.loader.PropertiesLauncher

   MODULE, package all the dependent libraries (except those whose scope is provided), but do not package any Launcher of Spring Boot.

   NONE, package all dependent libraries, but not any Launcher of Spring Boot.

5. Question answer

Springboot hits the jar package, and the classes in the jar package cannot be referenced. Why?

Problem Description:

  My directory structure is a parent project, there are three sub projects client, common, server. The client is all external interfaces provided. After packaging this project, the client's package is provided externally. The problem now is that other projects that depend on this package cannot reference the ordinary classes inside. And you must first compile to install before packaging, otherwise the client will report that you cannot refer to the class in common.
Question answer:

  Because of the springboot packaging method mentioned above, we have to understand that the jar here is not a jar package in the ordinary sense or a public class library in Java, but an independent Web application. In fact, the jar here should be war. Just because of SpringBoot's design idea, the application uses jar. If your jar is not accessed as a service, there is no need to repackage it with the SpringBoot plugin, because there is no entry class at all, and no executable jar is needed. So for this situation, it depends on the situation:

  1. If the package is an executable jar package, the internal public library needs to be encapsulated into a jar package that should be a public library. This executable jar package or war package provides external services. , Cannot directly rely on references.

  2. If it is just a common public class library jar package, then only need to modify the pom file using springboot repackage packaging method to maven ordinary packaging method, that is:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>8</source>
        <target>8</target>
    </configuration>
</plugin>

 

Guess you like

Origin www.cnblogs.com/liang1101/p/12735320.html