Example of application packaging deployment implemented by spring boot
1. Spring boot built-in web
Spring boot is integrated into the web container by default. The startup method is started by the main function entry like ordinary Java programs. The built-in Tomcat container or jetty container depends on the configuration (default Tomcat). Of course, you can also package the project into a war package and put it into a separate web container (tomcat, Weblogic, etc.). Of course, you need to make simple adjustments to the program entry before that.
Briefly explain several common configurations of server:
Tomcat
Tomcat is the default container for spring boot. Here are some common configurations:
pom. XML dependency configuration:
Jetty
If you want to select jetty, it is very simple to exclude the Tomcat dependency in POM and add the dependency of jetty container, as follows:
For project construction, we use Maven or gradle, which will make project dependency, jar package management, and packaging deployment very convenient.
2. Maven builds the executable jar package of the spring boot framework
An attractive feature of spring boot is that it can directly package the application into a jar / war, and then the jar / war can be started directly without configuring another web server. Separate jar packages, and then through Java - jar < name > The jar command runs.
1.1 Maven
The springboot Maven plug-in provides spring boot support for Maven. It allows you to package executable jar or War archives and then run the application in place. In order to use it, you need to use Maven 3.2 (or later).
Maven users can inherit the spring boot starter parent project to get the appropriate default settings. The parent project provides the following features: 1. The default compilation level is java 1.6 2. The source code is UTF-8 3. A dependency management node allows you to omit the < version > tag of common dependencies and inherit from spring boot dependencies POM. 4. Appropriate plug-in configuration (exec plug-in, surefire, GIT commitid, shade) 5. Resource filtering for application.properties and application.yml 6. Last point: because the default configuration file receives spring style placeholders (${...}), Maven filtering uses @@ Placeholder (you can override it with Maven attribute resource. Delimiter).
1.2 inherit starter parent
To configure your project to inherit spring boot starter parent, simply set the parent to:
Note: you should only specify the spring boot version on this dependency. Like his starters, you can safely omit the version number.
1.3 using springboot without parent POM
Not everyone likes to inherit spring boot starter parent POM. You may need to use the company standard parent, or you may prefer to explicitly declare all Maven configurations.
If you do not use spring boot starter parent, you can still get the benefits of dependency management by using a dependency with scope = import:
1.4 changing java version
Spring boot starter parent selects a fairly conservative Java compatibility strategy. If you follow our advice and use the latest java version, you can add a Java Version attribute:
1.5 using spring boot Maven plug-in
Springboot includes a maven plug-in that packages the project into an executable jar. If you want to use it, you can add the plug-in to the < plugins > node:
Note: if you use spring boot starter parent POM, you only need to add the plug-in without configuring it, unless you want to change the settings defined in the party.
This configuration repackages a jar or war in the package phase of the Maven lifecycle. The following example shows that there are both repackaged jars and original jars in the target directory:
1.6 packaging method under Linux:
Packaging using the MVN clean package command
If Maven is not already installed:
Or download and install separately:
Set environment variables:
You can then compile using the following command:
You can append the parameter - dmaven test. Skip = true skip the test.
target/myproject-1.0. 0.jartarget/myproject-1.0. 0.jar. original
1.6 packaging method under Eclipse:
Open the Maven package of the Maven plug-in to package:
Packaged files:
If you do not include < execution / > as above, you can run the plug-in yourself (but only if the package target is also used). For example:
target/myproject-1.0. 0.jar target/myproject-1.0. 0.jar. original
If you use a milestone or snapshot version, you also need to add the correct pluginrepository element:
Package the executable jar and war files
Once the spring boot Maven plugin is included in your POM XML, it will automatically try to rewrite the archives using the spring boot: repackage target to enable them to execute. To build a jar or war, you should configure your project using the regular packaging element:
The generated archive will be enhanced by springboot during the package phase. The main class you want to start can be implemented either by specifying a configuration option or by adding a main class attribute to the manifest. If you do not specify a main class, the plug-in will search for classes with public static void main (string [] args) methods.
To build and run an artifact for a project, you can enter the following command:
In this way, as long as the console is closed, the service cannot be accessed. Let's make the jar package run in the background: Java - jar spring-boot01-1.0-snapshot jar > log. file 2>&1 &
In order to build a war file that is executable and can be deployed to an external container, you need to mark the embedded container dependency as "provided", for example:
4. How spring boot starts when packaged as a single jar
After Maven is packaged, two jar files will be generated:
Where demo-0.0 1-SNAPSHOT. jar. Original is the package generated by the default Maven jar plugin.
demo-0.0. 1-SNAPSHOT. Jar is a jar package generated by the spring boot Maven plug-in, which contains application dependencies and spring boot related classes. It is called fat jar below.
First, check the directory structure of the package packed by spring boot (omit the unimportant ones):
Take a look at these contents in turn.
MANIFEST. MF
You can see that the main class is org springframework. boot. loader. Jarlauncher, this is the main function started by jar.
Another start class is com example. Springbootdemoapplication, which is our own main function
COM / example directory
Below this is the application Class file.
Lib directory
The jar package files that Maven of the application depends on are stored here.
Such as spring beans, spring MVC and other jars.
Org / springframework / boot / loader directory
The spring boot loader is stored below Class file.
Start:
Let's start directly: Java - jar demo-0.0 1-SNAPSHOT. jar
5. Maven add local jar package
Sometimes our projects rely on external jars. When we use eclipse to develop, we can directly add jars through build path, but this package will be missing when we use MVN packaging.
1. Use system scope
We directly introduce rabbitmq client jar。 This method is more flexible and does not require additional operations to the new server.
1. Groupid, artifact ID and version can be filled in freely. The scope must be filled in as system, and the SystemPath can now be the directory address of our jar package
2. ${basedir} is the root directory of the project
2. Install the jar package into the local repository
This requires the MVN install: install file command to be executed on the new machine.
For example, execute the command: