Home > Java > javaTutorial > body text

How to run springboot project as jar package

王林
Release: 2023-05-13 10:52:13
forward
3789 people have browsed it

1. Pom file configuration

1. Change the packaging method to jar

<packaging>jar</packaging>
Copy after login

2. Configure the maven plug-in for the executable jar package

<build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
                <!-- 如果有依赖外部jar,则必须加上此配置includeSystemScope-->
                <configuration>
                    <includesystemscope>true</includesystemscope>
                </configuration>
            </plugin>
        </plugins>
    </build>
Copy after login

3. Introduce external jar Dependencies (optional)

If an external jar is introduced into the project, you can create a lib folder in the project root directory and put the jar package into lib:

How to run springboot project as jar package

Then introduce the jar package in the pom file:

How to run springboot project as jar package

PS: If the project is packaged in the form of war and an external jar is introduced, the configuration of the maven plug-in is as follows

<build>
        <plugins>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-war-plugin</artifactid>
                <version>2.6</version>
                <configuration>
                    <webresources>
                        <resource>
                            <directory>${project.basedir}/lib</directory>
                            <targetpath>WEB-INF/lib</targetpath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webresources>
                </configuration>
            </plugin>
        </plugins>
    </build>
Copy after login

2. Pack and run

1. Packaging

Packaging command is very simple:

mvn install
Copy after login

2. Run

In production and testing environment, generally you need to keep the process running in the background without interrupting the terminal, you need to execute the following command:

nohup java -jar test.jar &>>log.out &
Copy after login

3. Configuration file priority

Spring boot will be in the following order Go find the configuration file:

1. Under the /config folder of the "current directory"

2. Under the "current directory"

3. The /config folder of the classpath Next

4, under classpath

Note:

  • The current directory refers to the directory where the java command is executed, if it is executed through a shell script java command, the current directory refers to the directory where the shell script is located. If you cd to a certain directory and then execute the java command in the shell script, the current directory refers to the directory after the cd.

  • Configuration files with different priorities can be read, but for the same configuration items, the configuration of the configuration file with higher priority will override the configuration of the configuration file with lower priority.

  • The configuration file in the jar package is not convenient to modify. In production, we generally put the configuration file outside the jar package.

In production, you can directly place the configuration file in the config directory at the same level as the directory where the jar package is located, then cd to the directory where the jar package is located in the script, and execute the java command to read the configuration. File.

The above is the detailed content of How to run springboot project as jar package. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template