封裝程式及其依賴的 library 為單一 jar 檔

Posted by Bruce Tsai

利用 gradle plugin 產生可執行檔 (shadow plugin)

build.gradle (application)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
group 'com.sample.runnman'
version '1.0'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // 加入 shadow plugin 到專案
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

build.gradle (module)

group 'com.sample.runman'
version '1.0'

apply plugin: 'java'
// 加入 plugin 使用
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'

sourceCompatibility = 1.6

repositories {
    mavenCentral()
}

// 預設的執行程式
mainClassName = 'com.prhythm.erotic.task.ScanSourceTask'

shadowJar {
    // 設置 manifest
    manifest {
        attributes 'Built-By': System.getProperty('user.name')
        attributes 'Build-Jdk': System.getProperty('java.version')
        attributes 'Implementation-Title': project.name
        attributes 'Implementation-Version': project.version
        attributes 'Implementation-Vendor-Id': project.group
    }
    mergeServiceFiles()
    // 使用 spring 時,避免合併 spring.handlers 檔,檔案覆蓋的問題
    mergeServiceFiles('META-INF/spring.handlers')
    // 使用 spring 時,避免合併 spring.schemas 檔,檔案覆蓋的問題
    mergeServiceFiles('META-INF/spring.schemas')
}

task configCopy(type: Copy) {
    from '../source.json'
    from '../app.properties'
    into 'build/libs'
}

dependencies {
    compile 'org.springframework:spring-core:4.1.4.RELEASE'
    compile 'org.springframework:spring-context:4.1.4.RELEASE'
    compile 'org.springframework:spring-beans:4.1.4.RELEASE'
    compile 'org.springframework:spring-jdbc:3.2.8.RELEASE'
    compile 'org.springframework:spring-tx:3.2.8.RELEASE'
    compile 'c3p0:c3p0:0.9.1.2'
    compile 'net.sourceforge.jtds:jtds:1.3.1'
    compile 'org.mybatis:mybatis-spring:1.2.2'
    compile 'log4j:log4j:1.2.17'
    compile 'jcifs:jcifs:1.3.17'
}

執行建置

# shadow the runtime configuration with project code into ./build/libs/
$ gradle shadowJar

results matching ""

    No results matching ""