zoukankan      html  css  js  c++  java
  • add spring-boot modules to maven project

    spring boot 项目中 多modules parent 冲突

    如果一个maven project 建立多modules ,pom文件应该是这样:

    <groupId>cn.ifengkou</groupId>
    <artifactId>spring-cloud-test</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    
    <modules>
        <module>spring-boot-test</module>
        <module>spring-cloud-config</module>
    </modules>
    

    module 的pom文件应该是这样:

    <parent>
        <artifactId>spring-cloud-test</artifactId>
        <groupId>cn.ifengkou</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>spring-boot-test</artifactId>
    

    大家在一开始学spring-boot时,都应该看过 官网的quick start项目,里面pom文件也有一个parent

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    

    这样,modules 项目中的 要继承父项目,而spring boot 又要继承spring-boot-starter-parent,一个pom文件不能存在两个parent,这个问题怎么解决呢?

    网上提供了一种方式,通过在子module 的parent 中,加 ../pom.xml 节点,实现父项目依赖管理的继承。但这种方式违背了按需加载依赖的理念,并不可取

    #project(parent) pom
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>
    <modules>
        <module>dao</module>
    </modules>
    
    # module pom
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.luyh.projectv1</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    

    其实spring-boot-starter-parent 只是一种快速开始spring boot 的方式,没必要非要用它,它所做的只是提供依赖管理和plugin管理(dependency management and plugin management),我们也可以自己做这些,当然如果想省事,spring 也提供了 spring-boot-dependencies 这个依赖(等价于 parent) 去管理依赖项,要实现同样的效果,你只需要用 scope=import, 像下面例子:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    完整pom.xml,或者查看githubspring-cloud-test

    #project(parent) pom
    
    <?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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>cn.ifengkou</groupId>
        <artifactId>spring-cloud-test</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
    
        <modules>
            <module>spring-boot-test</module>
            <module>spring-cloud-config</module>
        </modules>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
    
            <spring.boot.version>1.5.2.RELEASE</spring.boot.version>
        </properties>
    
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring.boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    
    
    # module pom
    <?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/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>spring-cloud-test</artifactId>
            <groupId>cn.ifengkou</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>spring-boot-test</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    </project>
  • 相关阅读:
    es6 javascript对象方法Object.assign()
    在vue中使用axios实现跨域请求并且设置返回的数据的格式是json格式,不是jsonp格式
    Vue中应用CORS实现AJAX跨域,及它在 form data 和 request payload 的小坑处理
    nvm、nzm、npm 安装和使用详解
    Win7系统出现提示: “Windows已遇到关键问题,将在一分钟后自动重新启动......
    npm安装/删除/发布/更新/撤销发布包
    web前端性能优化总结
    一道经典面试题-----setTimeout(function(){},0)和引发发的其它面试题
    渐进增强和优雅降级之间的区别在哪里?
    大学物理(上)期中考试参考答案
  • 原文地址:https://www.cnblogs.com/sloong/p/6710240.html
Copyright © 2011-2022 走看看