使用Intellj IDEA工具构建Springboot+gradle多模块项目

一、打开Intellj IDEA,点击左上角File->new project,新建一个Sringboot项目,以gradle为构建工具

 勾选自己需要的模块,点击下一步next,完成finish

 

二、删掉根项目的src目录,开始创建子模块 (test模块和test-api模块)

首先需要右击项目,在项目上新建模块

然后按照一开始创建项目的步骤在走一遍,要注意group要保持一致和选择gradle

新建子模块test:

 

 新建子模块test-api:

 

新建两个子模块完毕,接下来查看项目结构,将没必要的东西删除掉和对子模块(根目录下的setting.gradle)进行配置

include('test', 'test-api')

把所有的子模块都include进来,这里include的顺序要注意顺序,被依赖的放在前面,因为gradle打包时是按照顺序依赖的。

 

 

下一步,对根项目的build.gradle文件及逆行配置子项目通用的配置,添加自己项目所需要的依赖

根目录下的build.gradle

allprojects {
    group 'com.example'
    version '1.0.0'

    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'maven'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        flatDir {
            dirs 'libs'
        }
        mavenLocal()
        maven {
            url "http://nexus.parllay.cn/repository/maven-public/"
        }
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
        mavenCentral()
    }

    javadoc {
        classpath += sourceSets.main.compileClasspath
        source += sourceSets.main.allJava
    }
}

subprojects {
    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
        compile("org.springframework.boot:spring-boot-starter-web:1.5.4.RELEASE")

    }
}

project(':test-api') {
    dependencies {
        compile 'com.squareup.okhttp3:okhttp:3.2.0'
    }
}

project(':test') {
    ext {
        springBootVersion = '1.5.4.RELEASE'
        mybatisVersion = '1.3.0'
        mysqlVersion = '5.1.38'
        redisVersion = '2.9.0'
    }

    dependencies {
        compile(project(':test-api'))
        compile("org.springframework.boot:spring-boot-starter:${springBootVersion}")
    }
}

 

test模块下的build.gradle

buildscript {
    repositories {
        maven {
            url "http://nexus.parllay.cn/repository/maven-releases/"
        }
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
    }
    test {
        testLogging {
            // Show that tests are run in the command-line output
            events 'started', 'passed'
        }
    }
}

apply plugin: 'application'
apply plugin: 'org.springframework.boot'

mainClassName = 'com.example.test.TestApplication'

jar {
    manifest {
        attributes "Main-Class": "$mainClassName"
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude 'META-INF/*.SF'
        exclude 'META-INF/*.DSA'
    }
}

tasks.withType(Test).each { task ->
    task.enabled = false
}

bootRun {
    String activeProfile =  System.properties['spring.profiles.active']
    systemProperty "spring.profiles.active", activeProfile
}


dependencyManagement {
    resolutionStrategy {
        cacheChangingModulesFor 0, 'seconds'
    }
}

 

build-api模块下的build.gradle

group 'com.example'
version '1.0.0'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

 

目前,所有的配置就已经完成了。idea工具右上角的,gradle管理,删除多余的模块编译,只保留跟项目(即最全的项目),删除core和admin。然后点击刷新按钮刷新gradle。多模块项目就全部完事了

然后找到test模块下的TestApplication.java启动类,点击启动,项目启动成功!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值