Gradle 的主要配置文件是 build.gradle
,如果要使用 Maven 库,可以如下配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
repositories {
//Maven中心库(http://repo1.maven.org/maven2)
mavenCentral()
//本地库,local repository(${user.home}/.m2/repository)
mavenLocal()
//指定库
maven {
url
"http://repo.mycompany.com/maven2"
}
//指定库
maven {
// Look for POMs and artifacts, such as JARs, here
url
"http://repo2.mycompany.com/maven2"
// Look for artifacts here if not found at the above location
artifactUrls
"http://repo.mycompany.com/jars"
artifactUrls
"http://repo.mycompany.com/jars2"
}
//带认证的库
maven {
credentials {
username
'user'
password
'password'
}
url
"http://repo.mycompany.com/maven2"
}
}
|
其中有必要说说 mavenLocal(),能不能用 Maven 本地库也是笔者最关心的特性之一。
经实践,发现直接使用 mavenLocal() 时,gradle 会查找 Maven 配置文件 ${user.home}/.m2/settings.xml 来定位本地 Maven 库的路径,如果没有找到该文件,则默认本地库路径为 ${user.home}/.m2/repository,而笔者的 Maven 配置文件在 $M2_HOME/conf/settings.xml ,gradle 竟然不能读取到这个配置文件。
这个问题已经作为一个Improvement(#GRADLE-1900 [5])被提出,并显示在 1.0-milestone-9 版本中已经修正,而使用的1.0正式版时,竟然还有这个问题,真是相当诡异。
既然不能直接使用 mavenLocal(),就必须做一些变通,笔者最终测试用的 build.gradle
文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
apply plugin:
'java'
version:
'1.0-SNAPSHOT'
group:
'org.opoo'
repositories {
maven{ url
"file:///D:/m2.repo"}
//mavenLocal()
//mavenCentral()
}
dependencies {
compile group:
'commons-lang'
, name:
'commons-lang'
, version:
'2.1'
compile group:
'commons-logging'
, name:
'commons-logging'
, version:
'1.0.4'
testCompile group:
'junit'
, name:
'junit'
, version:
'4.+'
}
|