[EG]用Gradle进行多项目构建

Leon.Wood 2012-12-21

1.首先在workspace下建立如下文件

gradle.properites

 

//此处配置编译所用的JDK,如与JAVA_HOME中一样可省去此配置
org.gradle.java.home=c:/Program Files/Java/jdk1.6.0_11/
//配置文件编码为UTF-8,旨在支持中文文件
systemProp.file.encoding=UTF-8

 

 settings.gradle

 

//需要进行打包的项目
include "SAFEMS", "PMTMS"

 

 build.gradle

//应用到所有子项目,此处workspace相当于根项目
subprojects {
        apply plugin: 'war'
	version = '1.0'
	ext { 
		//发布位置
		targetDir = 'F:/Target/jar';	
		//java目录
		javaDir =  'src/cmes/java';
		//无需打包的包路径
		javaExclude = ['com/okok/cmes/base/**']
	}

	//自定义目录结构
	sourceSets {
		main {
			java {
				srcDir javaDir;
				exclude(javaExclude);
			}
		}
	}
	
	//中央仓库
    repositories {
		mavenCentral()
	}	
	
	//加载本地依赖
	dependencies {
		providedCompile fileTree(dir: 'F:/WF/libs', include: '*.jar')

		compile fileTree(dir: 'WebRoot/WEB-INF/lib', include: '*.jar')
	}
	//设置编码
	tasks.withType(Compile) {
		options.encoding = "UTF-8"
	}
}

 

 

 

子项目build.gradle

ext{
	//模块名
	moduleName = "m1";
	//jsp && js文件路径
	webappDir = "WebRoot/$moduleName/";
	//资源文件路径
	resourceDir = '/WebRoot/WEB-INF/classes/config';
	//资源文件名称
	resourceList =["**/*-"+moduleName+".xml"]
}

task getjar(overwrite: true,dependsOn: jar) <<{
	//复制jar
	println "move jar file : ${project.name}-${version}.jar"
	copy{
		from 'build/libs/'
		into "$targetDir/WebRoot/WEB-INF/lib"
	}
}
//发布jar包
task publish(overwrite: true,dependsOn: getjar) <<{	
	//复制jsp以及js
	println "move jsp and javascript files"
	copy{
		from webappDir
		into "$targetDir/$webappDir"
	}
	//复制资源文件
	println "move resource files"
	copy{
		from resourceDir
		into "$targetDir/$resourceDir"
		include(resourceList)
	}
 }
 

 

 

打jar包时只要在workspace下执行

 

gradle -q publish 

 

便会逐个执行settings文件中设置的项目下脚本中的publish任务,实现将打出的jar文件复制到指定位置

 

注:正在学习中,如有不足,恳请指出~

 

 

 

 

 

Global site tag (gtag.js) - Google Analytics