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:eclipseworkspareTestNG_gradl>gradle eclipse
:eclipseClasspath
:eclipseJdt
:eclipseProject
:eclipse

BUILD SUCCESSFUL

Total time: 4.103 secs

回到eclipse可以看到发生了下面的变化

多了一个Referenced Libraries,然后错误也没了。原来是我小白啦,唉,汗颜啊

文章导航