Springboot+Maven多模块开发 (一)初始化工程(新建第一个web工程)

学习Springboot+maven多模块开发笔记。


首先创建一个空项目,新建一个pom文件,该pom文件是整个工程的parent pom。


pom文件内容如下:

<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cn</groupId>
    <artifactId>SpringBoot</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <modules>
        <module>webapp-pc</module>
    </modules>
    <name>SpringBoot Maven Webapp </name>
    <url>http://maven.apache.org</url>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.3.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--如果要把springboot工程打包成war执行,需要该jar-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-legacy</artifactId>
                <version>1.0.2.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>


 

这里用import的方式引入springboot的parent,因为我们要分模块开发,由一个父pom来管理 所有模块的依赖,总不能在每个模块里面都引入。

新建第一个Maven web工程:

webapp-pc的pom文件内容如下:
<pre name="code" class="html"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>SpringBoot</artifactId>
        <groupId>com.cn</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>webapp-pc</artifactId>
    <packaging>war</packaging>
    <name>webapp-pc Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--要使用LOG4J,去掉该依赖-->
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>log4j-over-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--使用log4j,不使用默认的logback-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
        <!--要打成war包执行,需要该依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-legacy</artifactId>
        </dependency>
        <!--要打成war包执行,去掉内嵌的tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>webapp-pc</finalName>
    </build>
</project>


 
我一般喜欢打包成war放到tomcat下运行,不喜欢直接打包成一个富jar执行,所以加入了 spring-boot-legacy依赖,去掉了 spring-boot-starter-tomcat运行依赖。
如果你的servlet容器版本低于2.5,在maven打包war的时候会找不到web.xml而报错,需要在WEB-INF下加入一个空的web.xml:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <!--

      这个空的xml,为了假装自己是一个web工程,不要在这里写任何东西
  -->
</web-app>

在app启动类里面:
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
 * Created by XX on 2016/5/2.
 */
@SpringBootApplication
public class App extends SpringBootServletInitializer {

//    private Logger logger = LoggerFactory.getLogger(App.class);

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(App.class);
    }

    public static void main(String[] args){
        SpringApplication.run(App.class, args);
    }
}
为了打包war包执行,需要继承 SpringBootServletInitializer 。

下面新建一个测试Controller:
package com.cn.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by XX on 2016/5/2.
 */
@RestController
public class TestController {
    
    @RequestMapping("/test")
    public String test(){
        return "webapp start..";
    }
    
}
最后在tomcat启动该工程,在浏览器访问:http://localhost:8080/webapp-pc/test

因为我打包成war包在自己的tomcat发布,所以工程路径和端口都是以tomcat里面设置的为准。

代码路径: 代码github地址

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一款非常流行的Java Web框架,而多模块项目构建则是在实际开发中非常常见的需求。Maven是Java应用的依赖管理工具,结合Spring Boot可以很方便地构建、运行和打包模块项目。 首先,需要创建一个父模块,它将在编译和打包时引用所有子模块。在父模块的pom.xml文件中,需要指定每个子模块的groupId、artifactId和version等信息。 接下来,我们可以创建子模块,每个子模块都可以包含一个或多个Spring Boot应用程序。在子模块的pom.xml文件中,需要指定它们的父模块坐标,以及它们自身的坐标。 在Java代码中,我们只需要使用@SpringBootApplication注解启动Spring Boot应用程序,以便使用Spring Boot的自动配置和构件功能。同时,我们也可以使用@SpringBootApplication注解打包可执行jar文件,另外需要使用Maven插件将其构建为可执行jar包,这样我们就可以使用"java -jar"命令行运行应用程序。 最后,我们需要为所有子模块构建和打包项目。在Maven的父模块pom.xml文件中,执行"mvn clean install"命令即可完所有子模块和父模块的构建和打包工作。 总结来说,Spring Boot和Maven的多模块构建、运行和打包实战需要以下步骤:创建父模块和子模块,使用@SpringBootApplication注解启动应用程序,使用Maven插件构建jar包,最后执行"mvn clean install"命令完构建和打包。这个过程需要多次尝试和调整,但可以大大提高开发效率和项目的可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值