Deploy spring boot as a windows service

Directory of series articles

Deploy the executable jar package as a windows service

Tips: It is recommended to read the previous chapter [Deploying executable jar packages as windows services] before reading this article, which will help you understand the content of this chapter more easily

Article directory

  • Table of Contents of Series Articles
  • Preface
  • 1. How is your project packaged?
  • 2. What’s the difference?
    • 1.Different directories
    • 2.MANIFEST.MF
  • 3. Why can’t it be deployed directly as a Windows service?
  • 4. Problem-solving ideas
    • plan 1
    • Scenario 2
  • Summarize
    • Previous chapter: [Deploying executable jar packages as windows services](https://blog.csdn.net/RensDong/article/details/134285446)

Foreword

The text explains how to deploy the spring boot project as a windows service according to the following ideas
First, introduce how the spring boot project is packaged
Secondly, analyze the difference between the jar package created by spring boot through plug-in and the ordinary jar package.
Then let’s talk about why the jar package created by spring boot via plug-in cannot be directly deployed as a windows service
Finally, we will introduce how to package the spring boot project and deploy it as a windows service.

Tips: The following is the text of this article, the following cases are for reference

1. How is your project packaged?

Using the Spring Boot Maven Plugin, you can directly package the Spring Boot project into an executable jar package
The main role here is goals (repackage)

pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <finalName>${project.artifactId}</finalName>
                    <mainClass>com.example.demo.DemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals><goal>repackage</goal></goals>
                    </execution>
                </executions>
            </plugin>
          <plugins>
<build>

The java command runs the jar package.

java-demo.jar

2. What’s the difference?

1. Different directories

repackage package directory
repackage
Original package directory
original

After repackage
org directory (springframework project file)
The BOOT-INF directory contains project files and dependency files (lib directory).

The original package contains only project files.

2.MANIFEST.MF

repackage package Manifest

Manifest-Version: 1.0
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: Rens
Implementation-Vendor-Id: com.example
Spring-Boot-Version: 1.5.8.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.example.demo.DemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_144
Implementation-URL: http://projects.spring.io/spring-boot/demo/

Original package Manifest

Manifest-Version: 1.0
Implementation-Title: demo
Implementation-Version: 0.0.1-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: Rens
Implementation-Vendor-Id: com.example
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_144
Implementation-URL: http://projects.spring.io/spring-boot/demo/
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: com.example.demo.DemoApplication

repackage Manifest file
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.example.demo.DemoApplication

Original package manifest file
Main-Class: com.example.demo.DemoApplication

3. Why can’t it be deployed directly as a windows service?

When Apache Commons Daemon installs the service, the Procrun parameter StartClass sets the main class of the project, which is the Main-Class of the Manifest file. After repackage, the value of Main-Class is the JarLauncher of spring boot, so although the service can be deployed, it cannot be started. .

4. Problem-solving ideas

Option 1

Directly set Procrun’s StartClass to org.springframework.boot.loader.JarLauncher
The default value of Procrun’s SatrtClass parameter is the main method and does not need to be specified.
If the project needs to handle the logic of starting and stopping the service, you need to add input parameters. The input parameters will be passed to the main method of the project, and the logic of starting and stopping the service will be processed through the input parameters.

I am ashamed that when I wrote this article, I thought about whether I could directly set the StartClass of Procrun to org.springframework.boot.loader.JarLauncher. Through testing, I found that it can be started, but the main method must pass parameters and then process them according to the parameters. Start and stop the service, otherwise the windows service cannot be stopped after it is started.

class DemoApplication;

static void main(String [] args){<!-- -->
    String mode = args[0];
    if ("start".equals(mode){<!-- -->
        //The processing of input parameters is omitted here
        [...]
SpringApplication.run(DemoApplication.class, args);
    }
else{<!-- -->
System.exit(0);
}
}

Install ‘demo Service’ service

prunsrv //IS//demo–DisplayName=”demo Service”
–Install=prunsrv.exe –Jvm=auto –StartMode=jvm –StopMode=jvm
–StartClass=org.springframework.boot.loader.JarLauncher –StartParams=start
–StopClass=org.springframework.boot.loader.JarLauncher –StopParams=stop

Option 2

spring boot separate packaging
maven jar plugin + maven dependency plugin

pom.xml

<build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
               <configuration>
                   <archive>
                       <manifestEntries>
                           <Implementation-Version>${project.version}</Implementation-Version>
                       </manifestEntries>
                       <manifest>
                           <addClasspath>true</addClasspath>
                           <classpathPrefix>lib/</classpathPrefix>
                           <mainClass>com.example.demo.DemoApplication</mainClass>
                           <useUniqueVersions>false</useUniqueVersions>
                       </manifest>
                   </archive>
               </configuration>
           </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
       </plugins>
   </build>

The generated directory is as shown in the figure

Copy the jar package and lib to the deployment directory

Summary

For example: The above is what I will talk about today. This article only briefly introduces the method of deploying spring boot as a windows service. Maven has very powerful functions, and there are many ways to achieve similar functions. I will continue to take you to explore together in the future.
In fact, I have been thinking about how to package the project files and dependency files into a jar package, just like the spring boot maven plugin. Is the BOOT-INF folder very close to what our ideal jar looks like?

Previous chapter: Deploy the executable jar package as a windows service