Spring Boot Maven multi-module development web project deployment using external containers, springmaven

Source: Internet
Author: User

Spring Boot Maven multi-module development web project deployment using external containers, springmaven

Spring Boot has its own Tomcat container. Therefore, the Spring Boot project can run only by running the main function. However, in previous web projects, we are used to running projects using our own installed Tomcat or using Tomcat and Jetty plug-ins, which is convenient and easy to deploy. We only need to place the generated war package under Tomcat's webapps, restart Tomcat to access the project. So how should Spring Boot be deployed and accessed through the above method?

In fact, the main idea is to block Tomcat that comes with Spring Boot, and then introduce new Tomcat, so that we can use Spring Boot to easily develop web projects and make simple deployment.

The project in this article is based on Maven multi-module development, so you need to have a little bit of this foundation, if you do not know a little about this blog http://www.cnblogs.com/advancing/category/1123469.html

The specific implementation of external container deployment is as follows:

1. Set the following configuration in the parent

<! -- This specifies that tomcat-related packages are no longer required during packaging to change the configuration added to the war package. -->
<Dependency>
<GroupId> org. springframework. boot </groupId>
<ArtifactId> spring-boot-starter-tomcat </artifactId>
<Scope> provided </scope>
</Dependency>

Add Tomcat and Jetty plug-ins to the parent pom, and the sub-module will inherit them. Then re-Modify the plug-in parameters, such as port and encoding, according to the features of the sub-module ....

<Plugin>
<GroupId> org. apache. tomcat. maven </groupId>
<ArtifactId> tomcat7-maven-plugin </artifactId>
<Version> 2.2 </version>
<! -- If the attribute (port or path) is set here, the path of the module does not work. -->
<Configuration>
<UriEncoding> UTF-8 </uriEncoding>
<! -- <Port> 9080 </port> -->
<! -- <Path>/</path> -->
</Configuration>
</Plugin>

<Plugin>
<GroupId> org. eclipse. jetty </groupId>
<ArtifactId> jetty-maven-plugin </artifactId>
<Version >$ {jetty-version} </version>
<Configuration>
<JvmArgs>-Dfile. encoding = UTF-8 </jvmArgs>
</Configuration>
</Plugin>

<! -- Surefire -->
<Plugin>
<GroupId> org. apache. maven. plugins </groupId>
<ArtifactId> maven-surefire-plugin </artifactId>
<Version> 2.19.1 </version>
<Configuration>
<SkipTests> true </skipTests>
</Configuration>
</Plugin>

2. Create a New Method for modifying the startup class at the same level as the class containing the main method in Spring Boot in the sub-module.

/**
* Modify the startup class, inherit SpringBootServletInitializer, and override the configure method.
* Created by kk on April /11/21.
*/
Public class SpringBootStartApplication extends SpringBootServletInitializer {

@ Override
Public SpringApplicationBuilder configure (SpringApplicationBuilder builder ){
Return builder. sources (class name. class containing the Main method );
}

}

3. Add the packaging dependencies and Tomcat and Jetty plug-ins to the sub-module pom.

<Plugins>
<! -- In order to protect application. yml and application. properties, spring-boot modified the default placeholder $ {...} to @... @ -->
<! -- To replace the yml and properties files of spring boot with maven variables, use the $ {} placeholder -->
<Plugin>
<ArtifactId> maven-resources-plugin </artifactId>
<Configuration>
<Encoding> UTF-8 </encoding>
<Usedefadeldelimiters> true </usedefadeldelimiters>
</Configuration>
</Plugin>

<Plugin>
<GroupId> org. springframework. boot </groupId>
<ArtifactId> spring-boot-maven-plugin </artifactId>
<Configuration>
<! -- Specify the Main Class as the unique global portal -->
<MainClass> cn. XX. wechat. authorization. XXWechatAuthorizationCloudApplication </mainClass>
<Layout> ZIP </layout>
</Configuration>
<Executions>
<Execution>
<Goals>
<! -- You can package all dependent packages into the generated Jar package -->
<Goal> repackage </goal>
</Goals>
</Execution>
</Executions>
</Plugin>

<! -- This configuration tells maven that no web. xml is required when packaging the war package. If no web. xml error is found, it will be reported. -->
<Plugin>
<GroupId> org. apache. maven. plugins </groupId>
<ArtifactId> maven-war-plugin </artifactId>
<! -- <Version> 2.4 </version> -->
<Configuration>
<FailOnMissingWebXml> false </failOnMissingWebXml>
</Configuration>
</Plugin>

<Plugin>
<GroupId> org. apache. tomcat. maven </groupId>
<ArtifactId> tomcat7-maven-plugin </artifactId>
<Version> 2.2 </version>
<Configuration>
<! -- Start Tomcat and directly access http: // localhost: 8080/-->
<! -- <Url> http: // localhost: 8080/</url> -->
<! -- <Server> tomcat </server> -->
<! -- <Username> manager </username> -->
<! -- <Password> manager.123 </password> -->
<! -- <Path>/</path> -->
<UriEncoding> UTF-8 </uriEncoding>
<Path >/$ {project. module. name} </path>
<Port> 8866 </port>
</Configuration>
</Plugin>

<Plugin>
<GroupId> org. eclipse. jetty </groupId>
<ArtifactId> jetty-maven-plugin </artifactId>
<Version >$ {jetty-version} </version>
<Configuration>
<JvmArgs>-Dfile. encoding = UTF-8 </jvmArgs>
<SystemProperties>
<SystemProperty>
<Name> jetty. port </name>
<Value> 9083 </value>
</SystemProperty>
<SystemProperty>
<Name> org. eclipse. jetty. server. Request. maxFormContentSize </name>
<Value>-1 </value>
</SystemProperty>
</SystemProperties>
<StopKey> stop </stopKey>
<StopPort> 5599 </stopPort>
<WebAppConfig>
<ContextPath>/$ {project. module. name} </contextPath>
<ParentLoaderPriority> true </parentLoaderPriority>
</WebAppConfig>
<ScanIntervalSeconds> 0 </scanIntervalSeconds>
</Configuration>
</Plugin>
</Plugins>

4. In this way, you can run it locally using the container you configured. After the sub-module is successful, the following mark will be displayed.

5. to publish sub-modules to production, you need to set packaging to war in the sub-module. In the parent module, mvn install (because it depends on other modules), and then in the mvn package, then, put the generated war package in production.

 

 

 

Extended:

Use Tomcat development that comes with Spring Boot without the above operations. There are two ways to deploy the Spring Boot project:

I. Generate a jar package for the Spring Boot project and then use it.

1. Run cmd to enter the jar file directory packaged in the Spring Boot project.
2. Run the java-jar project jar name. jar
3. Access the path defined in the project

2. Deploy through Docker.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.