使用groovy腳本使gradle靈活加載本地jar包的兩種方式

本人在使用Jenkins作測試項目的可持續集成過程當中,構建工具用的gradle,但因爲一些jar包是並私有倉庫給用,暫時沒有搭建計劃。這就致使了我構建項目的時候須要的jar的地址每每是不同的,並且服務器和本地的版本可能也有所差異,常常其餘同窗提交代碼時候把build.gradle文件一併提交了,卻是倉庫文件比較亂。爲了解決這個問題,看了一些資料再研究了一點點gradle的使用後總結了兩種方法。java

第一種思路:把每一個人的項目依賴的jar包地址給固定了,而後用判斷當前用戶是哪一個,再去給complie files參數賦值。比較笨,可是比較容易理解,因爲框架的jar包和一些固定的jar包版本不怎麼發生變化,維護成本較低。也是我這個菜鳥想到的第一個辦法,雖然已經不用了,仍是記錄一下比較好web

第二種思路:每次去局域網服務器下載jar包,比對版本,若是同樣則下載到項目的文件夾裏,再去給complie files參數賦值。這個比較簡單,並且可以作到jar包版本更新的時候自動同步(服務端的jar有Jenkins生成)。暫時想到的比較好的辦法。spring

分享一下代碼,供你們參考:apache

buildscript {
    ext {
        springBootVersion = '1.5.13.RELEASE'
    }
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
 
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
 
group = 'com.fission.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
 
repositories {
    mavenLocal()
    mavenCentral()
}
def jarversion = "api_test_najm-1.0-SNAPSHOT.jar"
def path = getPath2(jarversion)
 
def getPath() {
    def local;
    File file1 = new File("/Users/Vicky/Documents/workspace/api_test_najm/build/libs/api_test_najm-1.0-SNAPSHOT.jar")
    File file2 = new File("/go/jar/api_test_najm-1.0-SNAPSHOT.jar")
    if (file1.exists()) {
        local = file1.getAbsoluteFile()
    } else if (file2.exists()) {
        local = file2.getAbsoluteFile()
    }
    println local
    return local
}
 
def getPath2(String v) {
    def jarpath = new File("").getAbsolutePath() + "/long/" + v
    if (new File(jarpath).exists()) return jarpath
    def url = new URL("http://**.***.**.**:****/go/jar/" + v)
    def out = new FileOutputStream(jarpath)
    out << url.newInputStream()
    return jarpath
}
 
 
dependencies {
    runtime('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile "org.springframework.boot:spring-boot-starter-thymeleaf"
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.8.0-beta2'
    compile files(path)
}

因爲以前接觸過groovy語言,寫起了也很是順手,先簡單寫個例子。之後有機會繼續分享gradle自定義腳本任務和Jenkins集成的實踐經驗。編程

往期文章精選

  1. java一行代碼打印心形
  2. Linux性能監控軟件netdata中文漢化版
  3. 接口測試代碼覆蓋率(jacoco)方案分享
  4. 性能測試框架
  5. 如何在Linux命令行界面愉快進行性能測試
  6. 圖解HTTP腦圖
  7. 寫給全部人的編程思惟
  8. 將json數據格式化輸出到控制檯
  9. 如何測試機率型業務接口
  10. 將swagger文檔自動變成測試代碼
  11. Mac+httpclient高併發配置實例
  12. httpclient處理多用戶同時在線

公衆號地圖 ☢️ 一塊兒來~FunTester

相關文章
相關標籤/搜索