1. Change the packaging method to jar
<packaging>jar</packaging>
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>
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:
Then introduce the jar package in the pom file:
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>
1. Packaging
Packaging command is very simple:
mvn install
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 &
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!