1. 程式人生 > >gradle複習(2)-eclipse中新增依賴jar包

gradle複習(2)-eclipse中新增依賴jar包

我所說的依賴,是指編寫程式碼的時候,你找不到包,就是沒有匯入到專案的Library中。例如下面的情況:

我在做TestNG學習的時候,用了TestNG的jar包,build中指令碼如下:

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.7
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    compile 'org.testng:testng:6.8.17'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

這個時候怎麼把jar包放到類似於下面的引用庫中呢?這樣才能匯入jar包。


解法1

有人說可以通過下面指令碼任務:

task copyJars(type:Copy){
	from configurations.runtime
	into 'libs'
}

執行該任務後,會新增一個libs目錄,並把我們的jar包都放到該目錄下:

但這並不能解決問題。

2.gradle eclipse命令

其實上面直接敲gradle eclipse就可以解決

D:\eclipse\workspare\TestNG_gradl>gradle eclipse
:eclipseClasspath
:eclipseJdt
:eclipseProject
:eclipse

BUILD SUCCESSFUL

Total time: 4.103 secs

回到eclipse可以看到發生了下面的變化

多了一個Referenced Libraries,然後錯誤也沒了。原來是我小白啦,唉,汗顏啊