gradle学习(5)-创建eclipse项目

最后更新于:2022-04-01 06:51:30

上一篇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://docs.gechiui.com/gc-content/uploads/sites/kancloud/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://docs.gechiui.com/gc-content/uploads/sites/kancloud/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 } ~~~ 具体的信息学习完再讲。暂时我也不清楚
';