gradle复习(2)-eclipse中添加依赖jar包
最后更新于:2022-04-01 06:52:11
**我所说的依赖,是指编写代码的时候,你找不到包,就是没有导入到项目的Library中。例如下面的情况:**
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466a8d2ad.jpg)
我在做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包。
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466a9f509.jpg)
## 解法1
有人说可以通过下面脚本任务:
~~~
task copyJars(type:Copy){
from configurations.runtime
into 'libs'
}
~~~
执行该任务后,会添加一个libs目录,并把我们的jar包都放到该目录下:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466aabd5d.jpg)
但这并不能解决问题。
## 2.gradle eclipse命令
其实上面直接敲gradle eclipse就可以解决
~~~
D:\eclipse\workspare\TestNG_gradl>gradle eclipse
:eclipseClasspath
:eclipseJdt
:eclipseProject
:eclipse
BUILD SUCCESSFUL
Total time: 4.103 secs
~~~
回到eclipse可以看到发生了下面的变化
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e466abae6c.jpg)
多了一个Referenced Libraries,然后错误也没了。原来是我小白啦,唉,汗颜啊
[](http://blog.csdn.net/itfootball/article/details/42918445#)[](http://blog.csdn.net/itfootball/article/details/42918445# "分享到QQ空间")[](http://blog.csdn.net/itfootball/article/details/42918445# "分享到新浪微博")[](http://blog.csdn.net/itfootball/article/details/42918445# "分享到腾讯微博")[](http://blog.csdn.net/itfootball/article/details/42918445# "分享到人人网")[](http://blog.csdn.net/itfootball/article/details/42918445# "分享到微信")