上一篇gradle学习文章学到构建java项目,这一篇来看看怎么讲上一次的java项目变成eclipse项目
## 1.标示eclipse项目
~~~
apply plugin: 'eclipse'
~~~
build.gradle如下
~~~
apply plugin: 'java'
apply plugin:'eclipse'
repositories{
mavenCentral()
}
dependencies {
compile group:'commons-collections',name:'commons-collections',version:'3.2'
testCompile group:'junit',name:'junit',version:'4.0+'
}
sourceCompatibility = 1.5
version = '2.0'
jar {
manifest {
attributes 'Implementation-Title':'Gradle Quickstart',
'Implementation-Version':version
}
}
test {
systemProperties 'property':'value'
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
~~~
执行gradle eclipse构建项目
~~~
<pre name="code" class="html">qianhuis-Mac-mini:1228_1 qianhui$ gradle eclipse
:eclipseClasspath
Download https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2/commons-collections-3.2.pom
Download https://repo1.maven.org/maven2/junit/junit/4.0/junit-4.0.pom
Download https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2/commons-collections-3.2.jar
Download https://repo1.maven.org/maven2/junit/junit/4.0/junit-4.0.jar
Download https://repo1.maven.org/maven2/commons-collections/commons-collections/3.2/commons-collections-3.2-sources.jar
Download https://repo1.maven.org/maven2/junit/junit/4.0/junit-4.0-sources.jar
:eclipseJdt
:eclipseProject
:eclipse
BUILD SUCCESSFUL
Total time: 2 mins 33.783 secs
~~~
但是目录结构没发现什么改变,希望随着学习的深入能发现什么端倪。
## 2.多重项目构建
项目目录结构如下:
multprojec目录下含有3个项目[api,services,shared],services项目下又含有[shared,webservice]
![](https://box.kancloud.cn/2016-01-07_568e4665a6d7f.jpg)
## 3.settings.gradle
为了构建多重项目,需要在多重项目的根目录定义一个文件,包含所有的子项目。该文件名必须定义为settings.gradle
~~~
qianhuis-Mac-mini:multproject qianhui$ touch settings.gradle
qianhuis-Mac-mini:multproject qianhui$
~~~
在该文件中用include来包含所有的子项目
~~~
include "shared","api","services:webservice","services:shared"
~~~
![](https://box.kancloud.cn/2016-01-07_568e4665b9fab.jpg)
## 4.配置注入
使用configuration injection来管理所有的项目,主项目像一个容器,通过subprojects方法能够遍历所有的子项目,然后为每一个项目注入特殊的配置信息。这样我们就能够很容易的为依赖和压缩包定义配置内容。如下所示,我们首先根据settings.gradle里的配置信息得到所有的子项目,然后逐个位每个项目注入下面的配置。
~~~
subprojects{
apply plugin:'java'
apply plugin:'eclipse-wtp'
repositories{
mavenCentral()
}
dependencies{
testCompile 'junit:junit:4.11'
}
jar{
manifest.attributes provider:'gradle'
}
}
~~~
这个时候在根项目下执行gradle build,你会发现它为每个项目都做了构建
~~~
qianhuis-Mac-mini:multproject qianhui$ gradle build
:api:compileJava UP-TO-DATE
:api:processResources UP-TO-DATE
:api:classes UP-TO-DATE
:api:jar
:api:assemble
:api:compileTestJava UP-TO-DATE
:api:processTestResources UP-TO-DATE
:api:testClasses UP-TO-DATE
:api:test UP-TO-DATE
:api:check UP-TO-DATE
:api:build
:services:compileJava UP-TO-DATE
:services:processResources UP-TO-DATE
:services:classes UP-TO-DATE
:services:jar
:services:assemble
:services:compileTestJava UP-TO-DATE
:services:processTestResources UP-TO-DATE
:services:testClasses UP-TO-DATE
:services:test UP-TO-DATE
:services:check UP-TO-DATE
:services:build
:shared:compileJava UP-TO-DATE
:shared:processResources UP-TO-DATE
:shared:classes UP-TO-DATE
:shared:jar
:shared:assemble
:shared:compileTestJava UP-TO-DATE
:shared:processTestResources UP-TO-DATE
:shared:testClasses UP-TO-DATE
:shared:test UP-TO-DATE
:shared:check UP-TO-DATE
:shared:build
:services:shared:compileJava UP-TO-DATE
:services:shared:processResources UP-TO-DATE
:services:shared:classes UP-TO-DATE
:services:shared:jar
:services:shared:assemble
:services:shared:compileTestJava UP-TO-DATE
:services:shared:processTestResources UP-TO-DATE
:services:shared:testClasses UP-TO-DATE
:services:shared:test UP-TO-DATE
:services:shared:check UP-TO-DATE
:services:shared:build
:services:webservice:compileJava UP-TO-DATE
:services:webservice:processResources UP-TO-DATE
:services:webservice:classes UP-TO-DATE
:services:webservice:jar
:services:webservice:assemble
:services:webservice:compileTestJava UP-TO-DATE
:services:webservice:processTestResources UP-TO-DATE
:services:webservice:testClasses UP-TO-DATE
:services:webservice:test UP-TO-DATE
:services:webservice:check UP-TO-DATE
:services:webservice:build
BUILD SUCCESSFUL
Total time: 6.571 secs
~~~
相应的子项目目录下都生成了jar包。但是要注意上面的配置不会对root project产生作用。
## 5.项目之间的依赖
比如A项目依赖B项目,那么在A项目下定义一个build.gradle 。写法如下:
~~~
dependencies{
compile project(':B')
}
~~~
那么在A项目构建前,会先构建B项目。然后将B项目的jar包作为A项目的依赖jar,构建A的jar包。
## 6.调配项目
~~~
task dist(type: Zip) {
dependsOn spiJar
from 'src/dist'
into('libs') {
from spiJar.archivePath
from configurations.runtime
}
}
artifacts {
archives dist
}
~~~
具体的信息学习完再讲。暂时我也不清楚
- 前言
- gradle学习(1)-helloworld
- gradle学习(2)-基础语法
- gradle学习(3)-基础认识
- gradle学习(4)-构建java项目
- gradle学习(5)-创建eclipse项目
- gradle学习(6)-依赖管理
- gradle学习(7)-groovy
- gradle学习(8)-gradle的命令行
- gradle学习(9)-获取build相关信息
- gradle学习(10)-gui
- gradle学习(11)-编写构建脚本
- gradle学习(12)-groovy一些基础语法
- gradle学习(13)-有的没的
- gradle学习(14)-任务
- gradle学习(15)-任务
- gradle学习(16)-操作文件
- gradle学习(17)-被合并的ant
- gradle学习(18)-ant的属性
- gradle学习(19)-log系统
- gradle学习(20)-详解java插件
- gradle学习(21)-在eclipse中构建java项目
- gradle复习(1)-2种定义任务方式的区别
- gradle复习(2)-eclipse中添加依赖jar包
- gradle复习(3)-在gradle项目中使用TestNG
- gradle复习(4)-Cannot find System Java Compiler
- gradle复习(5)-Test remote debug
- gradle复习(6)-深入Jacoco
- gradle复习(7)-深入Jacoco
- gradle复习(8)-Task中行为
- gradle学习(22)-Sonar
- gradle学习(23)-Sonar runner