创建外部依赖项并使用gradle将其添加到其他项目

时间:2017-07-18 19:04:46

标签: gradle

我有两个spring boot projet

A和B

想要创建一个新的项目来放置常见的东西

一个 乙

公地

如何像外部依赖一样将公共添加到A和B?

2 个答案:

答案 0 :(得分:1)

假设你的项目布局如下:

project
|- common
|- proja
|- projb
  1. 您需要settings.gradle

    rootProject.name = 'project'
    include 'common'
    include 'proja'
    include 'projb'
    
  2. 然后您需要在project下更新build.gradle,如下所示:

    plugins {
        id 'org.springframework.boot' version '1.5.4.RELEASE'
    }
    
    subprojects { // common configurations for all subprojects
        apply plugin: 'groovy'
        apply plugin: 'org.springframework.boot'
    
        repositories {
            jcenter()
        }
    
        dependencies { // common dependencies for all subprojects
            compile 'org.codehaus.groovy:groovy-all:2.4.10'
    
            testCompile 'org.springframework.boot:spring-boot-starter-test'
            testCompile 'org.spockframework:spock-spring:1.0-groovy-2.4'
        }
    }
    
    project ("proja") {
        dependencies { // proja-specific dependencies
            compile 'com.google.guava:guava:21.0'
            compile project(":common")
        }
    
    }
    
    project ("projb") {
        dependencies { // projb-specific dependencies
    
            compile project(":common")
        }
    }
    
  3. 让我知道这是否有效。

答案 1 :(得分:0)

外部解决方案似乎要走了

我的commons build.gradle文件

apply plugin: 'java-library-distribution'

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.0.0.M2'
}

distributions {
    main{
        baseName = 'commons-code'
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8


tasks.withType(JavaCompile) {
    options.compilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation", "-parameters"]
}

在我的主项目中

我有一个build.gradle

在依赖关系部分我有

compile project (':commons-code')

在settings.gradle文件中我有

include ":commons-code"
project(":commons-code").projectDir = file("/home/bob/dev/project/commons/")