[Java] IntelliJ IDEA不同项目的依赖与引用

相关文章

[Java] IntelliJ IDEA 2019.3.1创建完整的Spring Boot项目

[Java] IntelliJ IDEA通过gradle搭建spring boot多模块项目

环境

IntelliJ IDEA 2019.3.1

gradle: https://services.gradle.org/distributions/gradle-5.2.1-all.zip

Java: 8

关于如何在IntelliJ IDEA,如何通过gradle建立多模式项目,在相关文章里面已有说明,就不再详细阐述。

先看我们本次文章的代码目录结构

▾ id-center/
  ▸ common/
  ▸ gradle/
  ▸ out/
  ▸ src/
    build.gradle
    gradle.properties
    gradlew*
    gradlew.bat
    settings.gradle
▾ pea-core/                                                                                                                 
  ▸ build/
  ▸ gradle/
  ▸ src/
    build.gradle
    gradlew*
    gradlew.bat
    settings.gradle

id-center项目,是微服务的一个子系统。pea-core项目,是各个微服务共用的类库。id-center项目是依赖于pea-core项目的。

有些人会说,那我可以将pea-core打包成jar,然后在 id-center引进来就可以了。的确,这种方法也是解法之一。这里不打包jar的原因是,pea-core会经常变动,而在团队开发的时候,不断的要打包jar然后分发给各个同事也是一件很繁琐的事来的,所以干脆将pea-core作为依赖,引入到id-center项目当中。

关于pea-core就不多说了,当作普通的gradle项目来建立就好,不懂的请看上面列的相关文章,或者google。关键是id-center的引用

id-center/settings.gradle

rootProject.name = 'id-center'
include('pea-core')
// !!!FBI WARNING!!!关键代码
project(':pea-core').projectDir=file('../pea-core')

id-center/build.gradle

buildscript {
    repositories {
        mavenLocal()
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
        maven {
            url "https://oss.sonatype.org/content/groups/public/"
        }
        maven {
            url "https://repo.spring.io/libs-milestone/"
        }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        // classpath "se.transmode.gradle:gradle-docker:${transmodeGradleDockerVersion}"
    }
}
allprojects {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'idea'

    group = 'com.ppwang.services.idcenter'
    version = '1.0-SNAPSHOT'
    sourceCompatibility = 1.8

    repositories {
        mavenLocal()
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
        maven {
            url "https://oss.sonatype.org/content/groups/public/"
        }
        maven {
            url "https://repo.spring.io/libs-milestone/"
        }
        jcenter()
        mavenCentral()
    }

    dependencies {
        // !!!FBI WARNING!!!关键代码,如果不加这个,会导致错误Recursively include external project and its subprojects
        if (!project.name.startsWith('pea')) {
            implementation project(':pea-core')
        }
        testImplementation "org.springframework.boot:spring-boot-starter-test"
    }

    dependencyManagement {
        imports {
            mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        }
    }

    // 这里一定得要。在多模块下,不然编译失败,因为不会把信赖模块给打包。
    jar {
        enabled = true
    }
}

注意以上就够了,至于其它问题也没那么难了

还有,引进其它项目的某些模块作为依赖,还可以这么操作

id-center/settings.gradle

include "shared-library"
project(":shared-library").projectDir = new File("/projects/shared-library")

include "shared-library:module2"
project('shared-library:module2').name = ':module2'

include "shared-library:module1"
project('shared-library:module1').name = ':module1'

id-center/build.gradle

implementation project(":module1")

Have fun with Java!

原文地址:https://www.cnblogs.com/davidhhuan/p/12289527.html