1. 程式人生 > >gradle 構建聚合專案

gradle 構建聚合專案

構建一個聚合專案,專案有多個子專案構成,需要對專案中用到構件進行管理。專案選擇了gradle而非maven,所以我專門了的研究了一下gradle,參考多篇文章,不保證這些文章內容都是正確的,但確實收到啟發和獲得到了知識。

我參考的文章列表:

專案實戰

1.構建的聚合專案結構

build-some-emp

some-common

some-core

some-emp

    其中build-some-emp 相當於maven的parent,some-emp依賴於some-common和some-core

而some-common依賴於some-core

    這裡,build-some-emp我使用eclipse的gradle外掛建立的gradle專案,其餘均為普通java專案

2.具體配置

在build-some-emp中build.gradle,內容如下

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
//    if ($bp != null) { buildDir = "$bp" }
}

buildscript {
    repositories {
        mavenCentral()
    }
}

subprojects {
    apply plugin: 'java'
    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

    repositories {
        mavenCentral()
        maven {url 'http://jaspersoft.artifactoryonline.com/jaspersoft/jr-ce-releases'}
        maven {url 'https://oss.sonatype.org/content/repositories/snapshots' }
        mavenCentral artifactUrls: [
            'http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/'
        ]
    }

    dependencies {
        compile 'com.google.code.gson:gson:2.3.1'
        compile group: 'org.apache.poi', name: 'poi', version: '3.15'
    }
}
project(':some-emp') {
    dependencies {
        compile project(':some-common')
        compile project(':some-core')
    }
}
project(':some-core') {
 	sourceSets {
        main {
            resources {
                srcDir 'src/main/sql'
                srcDir 'src/main/xml'
            }
            java{
               srcDir 'src/main/ojdbc'
            }

        }
    }
    dependencies {
    }
}
project(':some-common') {
    dependencies {
 	compile 'net.lingala.zip4j:zip4j:1.3.2'
    compile project(':some-core')
    }
}
task wrapper(type: Wrapper) {
    gradleVersion = '2.14.1'
}
  • allprojects 全部專案構建設定,設定當前專案和其子專案的構建引數,這裡主要是為了忽略構建警告
  • buildscript 構建指令碼,這個是gradle指令碼執行所需依賴
  • subprojects 所有子專案構建依賴設定,這裡我們設定了java外掛,編碼集和倉庫位置,此外還有所有子專案都依賴的構件
  • project 具體子專案的構建設定,這裡除了設定依賴之外,還設定了sourceSets,因為gradle預設的source folder 就是maven構建的那四個資料夾,其餘source folder必須在配置檔案指出,否則每次構建都會有
  • task wrapper 解決沒有安裝gradle想要構建專案時的配置

在build-some-emp中的settings.gradle的內容如下

rootProject.name = 'build-some-emp'
includeFlat 'some-core','some-common','some-emp'
  • 設定根專案名,要與當前專案一致
  • includeFlat 指定子專案名,網上說平級時使用includeFlat,單我用include一樣也能構建成功

經過上述配置後,就可以看到我們想要的結果了。

另附一個spring-boot專案配置:

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.10.RELEASE'
    }
}

subprojects {
    apply plugin: 'java'

    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

    repositories {
        mavenCentral()
        maven {url 'http://jaspersoft.artifactoryonline.com/jaspersoft/jr-ce-releases'}
        maven {url 'https://oss.sonatype.org/content/repositories/snapshots' }
        mavenCentral artifactUrls: [
            'http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/'
        ]
    }

    dependencies {
        compileOnly 'org.projectlombok:lombok:1.16.10'
        compile 'com.google.code.gson:gson:2.3.1'
        compile group: 'org.apache.poi', name: 'poi', version: '3.15'
        compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15'
        // jdbc
        compile('org.springframework.boot:spring-boot-starter-jdbc')
        // Doma
        compile group: 'org.seasar.doma', name: 'doma', version: '2.19.2'
        compile 'org.seasar.doma.boot:doma-spring-boot-starter:1.1.0'
        // Thymeleaf
        compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
        // Test
        testCompile 'junit:junit:4.12'
        testCompile 'org.glassfish.web:javax.el:2.2.4'
        testCompile 'org.springframework.boot:spring-boot-starter-test:1.4.1.RELEASE'
        // Log
        runtime 'org.slf4j:jul-to-slf4j:1.7.12'
        // Spring
        compile 'org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE'
        compile 'org.springframework.boot:spring-boot-devtools:1.4.1.RELEASE'
        // Flyway
        compile 'org.flywaydb:flyway-core:4.0.3'
        compile 'org.springframework.boot:spring-boot-starter-jdbc:1.4.1.RELEASE'
        // metrics-mackerel
        compile group: 'net.unit8.metrics', name: 'metrics-mackerel', version: '0.1.1'
        compile group: 'io.dropwizard.metrics', name: 'metrics-parent', version: '3.2.4', ext: 'pom'
        compile group: 'io.dropwizard.metrics', name: 'metrics-core', version: '3.2.4', ext: 'pom'
        // zip4j
        compile 'net.lingala.zip4j:zip4j:1.3.2'
    }

    ext['thymeleaf.version'] = '3.0.9.RELEASE'
    ext['thymeleaf-layout-dialect.version'] = '2.3.0'
}

project(':pdfconv-batch') {
    apply plugin: 'java'

    sourceSets {
        main {
            resources {
                srcDir 'src/main/sql'
            }
        }
    }

    sourceCompatibility = targetCompatibility = 1.8

    processResources.destinationDir = compileJava.destinationDir

    compileJava.dependsOn processResources

    dependencies {
        compile project(':some-batch-common')
        compile project(':some-common')
        compile project(':some-core')
    }
}

project(':some-batch-common') {
    apply plugin: 'java'

    sourceCompatibility = targetCompatibility = 1.8

    sourceSets {
        main {
            resources {
                srcDir 'src/main/sql'
            }
        }
    }

    // Doma設定    
    processResources.destinationDir = compileJava.destinationDir
    compileJava.dependsOn processResources

    dependencies {
        compile project(':some-common')
        compile project(':some-core')
    }
}

project(':some-common') {
    apply plugin: 'java'

    sourceCompatibility = targetCompatibility = 1.8

    sourceSets {
        main {
            resources {
                srcDir 'src/main/sql'
            }
        }
    }


    processResources.destinationDir = compileJava.destinationDir
    compileJava.dependsOn processResources

    dependencies {
        compile project(':some-core')
    }
}

project(':some-core') {
    apply plugin: 'java'
    dependencies {
        compile files ('lib/ojdbc7.jar')
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.14.1'
}