spring-boot 创建项目并配置远程部署到tomcat

17 篇文章 0 订阅

#spring-boot 创建项目并配置远程部署

1. 创建一个空白maven项目

创建一个Maven项目比较简单, 我使用的是IDEA InteliJ, 创建一个空白的Maven项目, GroupId 和 ArtfactId 按照自己的想法填写.

2. 增加spring boot的内容

先建立好spring boot的内容, 然后要修改为能够部署在tomcat中.

2.1 增加springboot的pom.xml配置

增加maven配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.2 增加main函数

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class Application {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

到此为止已经可以运行了, 一个初始化的spring boot项目. 但是由于spring boot内含tomcat, 那么会有一个问题就只能运行一个spring boot项目, 除非每次都换个端口.

要修改为可以部署在tomcat中.

3. 修改为从tomcat容器中启动

一共两步, 增加pom配置. 第二步, 继承SpringBootServletInitializer类, 重写SpringApplicationBuilder方法.

###3.1 增加pom配置

在依赖里增加配置:

<!-- marked the embedded servlet container as provided -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

pom.xml<version>标签下增加增加war配置:

   <groupId>com.slow.tech</groupId>
    <artifactId>resource</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging> //这行是增加的

3.2 继承类SpringBootServletInitializer

@Controller
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

这样就可以在tomcat里面启动了.

4. 增加远程部署

增加远程server里面的远程地址.

<build>
        <finalName>resource</finalName>
        <plugins>
            <plugin>
                <!-- https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat8-maven-plugin -->
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>

                <configuration>
                    <url>http://xx.xx.4.56:8080/manager/text</url>
                    <server>slowtech</server>
                    <username>slowtech</username>
                    <password>slowtech</password>
                    <update>true</update>
                    <path>/resource</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

增加完pom.xml配置, 增加远程tomcat配置 $TOMCAT_HOME/conf/web.xml.

<role rolename="admin-gui"/>
  <role rolename="admin-script"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-script" />
  <role rolename="manager-jmx"/>
  <user username="xxxx" password="xxxx" roles="manager-gui,admin-gui"/>
  <user username="xxxx" password="xxxx" roles="manager-script,admin-script"/> //这里是用来上传的权限

本地增加maven配置.m2/settings.xml:
servers标签里增加配置:

        <server>
                <id>slowtech</id>
                <username>xxxxx</username>
                <password>xxxxx</password>
        </server>

然后就可以远程部署了.

mvn tomcat7:deploy
重新部署:
mvn tomcat7:redeploy
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值