gradle 1+1

来源:互联网 发布:3cdaemon 端口 编辑:程序博客网 时间:2024/05/21 09:21

设置编译等级

sourceCompatibility=1.6targetCompatibility=1.6

依赖包


dependencies {      compile "group:name:version"//运行时候的依赖包      testCompile "group:name:version"//测试依赖包      //compile org.hibernate:hibernate-core:3.6.7.Final'}



依赖本地文件

includes表示在include内的文件才会被包含其他的文件不被包含excludes于includes相反。
dependencies {compile fileTree(dir:"WebRoot/WEB-INF/lib/",includes:["c3p0-0.9.1.jar","ojdbc14.jar"])//只引入c3p0-0.9.1.jar,ojdbc14.jar 其他jar均不引入compile fileTree(dir:"WebRoot2/WEB-INF/lib/",excludes:["c3p0-0.9.1.jar","ojdbc14.jar"])//除c3p0-0.9.1.jar,ojdbc14.jar 其他jar均引入}

去除传递依赖

方法1:

dependencies { compile ("xxx:xxx:xxx"){     transitive = false//去除传递依赖}}

方法2

dependencies { compile "xxx:xxx:xxx@jar"//加入@jar去除传递依赖}



比如xxx依赖于xxx2 那么如果不配置transitive=false那么xxx2.包括xxx2所依赖的包都会被下载。

例如:

ext{   springversion="3.2.4.RELEASE"}dependencies {   compile "org.springframework:spring-aop:${springversion}"}



查看依赖使用命令 gradle dependencies

效果如下

红色部分就是传递依赖的东西。这些东西都会被下载即使我们不用

当加去除传递依赖后

ext{   springversion="3.2.4.RELEASE"}dependencies {   compile ("org.springframework:spring-aop:${springversion}"){   transitive = false//去除传递依赖}}



效果。不会再下载依赖的包了。只会下载我们配置的包

全局去除传递依赖

configurations {     all*.transitive = false}

去除部分依赖

我们想引入传递依赖的大部分jar,但是有的部分我们不想引入

dependencies {compile("xxx:xxx:xx"){exclude group: 'xxx', module: 'xxx'}}




全局去除部分依赖

configurations {    all*.exclude group: 'xxx'}
将不会有group 'xxx'的依赖包

更改webRoot名称

war{webAppDirName="newName"}



设置sourceSets

sourceSets {    main {        java {            srcDirs '源码目录1','源码目录' //都是相对路径        }        resources {            srcDirs '资源目录1','资源目录2' //都是相对路径        }    }      test {        java {            srcDirs '测试源码目录1' ,'测试源码目录2'//都是相对路径        }        resources {            srcDirs '测试资源目录1','测试资源目录2' //都是相对路径        }    }}



设置版本号

version="1.0"


设置变量

基于代码

ext{ 变量名称=值}

配置代码仓库

maven仓库
repositories { maven {url "http://192.168.1.10:8081/nexus/content/groups/public"}  maven {url "http://192.168.1.10:8081/nexus/content/repositories/local"} maven{ url 'http://maven.oschina.net/content/groups/public/'} mavenCentral()}


拷贝依赖到指定目录

task copyjars(type: Copy){  from configurations.compile  into "c:/WebRoot/WEB-INF/lib2"}


发布包到maven仓库

apply plugin: 'maven'apply plugin:'maven-publish'apply plugin:   'java'version="1.0"group="xx.xx.xx"task sourceJar(type: Jar) {//将源码导入  from sourceSets.main.java  from sourceSets.main.resources  classifier "sources"}publishing  {    repositories {        maven {            url 'http://192.168.1.10:8081/nexus/content/repositories/local'            credentials {                username 'admin'                password 'admin123'            }        }    }     publications {   maven(MavenPublication) {       artifacts = [jar,sourceJar]      }  }}



更换编译器

因为有的时候我们使用eclipse开发的代码使用jdk自带的编译器是无法通过的。这时候就需要更换编译器为我们开发环境的编译器。
compileJava { options.encoding = 'utf-8'options.debug=falseoptions.warnings=falseoptions.fork = trueoptions.forkOptions.executable = 'java'//ecj是eclipse的编译器更多参数参见eclipse ecjoptions.forkOptions.jvmArgs = ['-cp','d:\\ecj-4.4.1.jar','org.eclipse.jdt.internal.compiler.batch.Main','-target','1.5','-source','1.5']}


设置编码

[compileJava, javadoc, compileTestJava]*.options*.encoding = 'UTF-8'

javadoc乱码

javadoc{  options.addStringOption("encoding", "UTF-8")  options.addStringOption("charset", "UTF-8")}

示例大全

https://github.com/pkaq/GradleSide


0 0
原创粉丝点击