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>
  • 相关阅读:
    【转】Java8学习笔记(1) -- 从函数式接口说起
    解决sublime text 2总是在新窗口中打开文件(标签中打开)
    Cocoapod安装使用
    使用工具来提升Android开发效率
    Material Design之FloatingActionButton的使用
    HDU2842-Chinese Rings(递推+矩阵高速幂)
    阿里笔试题(2015)持续更新中
    Newton‘ method 的优缺点
    解决的方法:warning: Clock skew detected. Your build may be incomplete.
    云享 值得一用的在线文档即时通讯平台 新用户持续免费
  • 原文地址:https://www.cnblogs.com/sloong/p/6710240.html
Copyright © 2011-2022 走看看