gradle学习点滴

来源:互联网 发布:网红淘宝店推荐 编辑:程序博客网 时间:2024/05/01 21:27

gradle 默认的目录结构是src/main/java ,src/main/webapps。

如何在老项目上使用gradle

sourceSets {     main {              //一般老项目的源代码目录是src          java.srcDir "$projectDir/src"     } }

如何用gradle引用jar包

repositories {     flatDir(dirs: "$projectDir/libs") } dependencies {     compile ':junit:4.4' } 
注:repositories 相当一个存储 jar 包的仓库,我们可以指定本地的依赖 jar 包,也可以利用 Maven 所指定的仓库,如 mavenCentral(); 通过 dependencies 来包含所有真正要依赖的 jar 包,格式为 goup:name:version,':junit:4.4:' 就是表示 dirs 路径下的 junit-4.4.jar 这个包。

pply plugin: 'java'//so that we can use 'compile', 'testCompile' for dependenciesdependencies {  //for dependencies found in artifact repositories you can use  //the group:name:version notation  compile 'commons-lang:commons-lang:2.6'  testCompile 'org.mockito:mockito:1.9.0-rc1'  //map-style notation:  compile group: 'com.google.code.guice', name: 'guice', version: '1.0'  //declaring arbitrary files as dependencies  compile files('hibernate.jar', 'libs/spring.jar')  //putting all jars from 'libs' onto compile classpath  compile fileTree('libs')}


如何用gradle实现 copy 工作

task copyOne(type: Copy) {     from 'src/main/test'     into 'build/anotherDirectory' }

对 copy 文件的过滤

  有时候一个目录下的文件数目很多,而我们只想复制某一部分文件,比如只复制 java 文件或资源文件等,这时候我们就要用到 copy 任务的 include 属性,这一点和 Ant 是一样的。比如只复制 java 文件到某一指定目录,实现这个需求我们要在 build.gradle 文件中增加 的内容。

task copyTwo(type: Copy) { //(type:Copy)声明task的类型为Copy类型   from 'src/main/test'    into 'build/anotherDirectory'    include '**/*.java' } 

如果我们只想排除一些文件,不想把这一类文件 copy 过去,这时候我们要用到 exclude 属性,比如我们不想把 java 文件复制到指定目录中,那么我们只需要将上面清单 15 中的 include 替换成 exclude 即可。

发布 jar 文件

      做项目时经常会遇到一个 project 中的类依赖另一个 project 中类的情况,如果用 Ant,我们会这样做,首先将被依赖的类文件打成 jar 包,然后利用 copy 命令将这个 jar 包复制到指定目录下,我们可以想象到要向 build.xml 添加好多行代码,这里我们就不一一列出了,不会的同学们可以参考上面的知识。下面我们看下 Gradle 是怎样来完成这一需求的,Gradle 不但可以讲 jar 包发布到本地的指定目录中,而且还可以发布到远程目录中。

发布 jar 包到本地目录

publishJarFile {    repositories {      flatDir(dirs: file('jarsDerectory'))    } } 

然后我们利用 gradle publishJarFile 命令即可,将工程下的 java 类文件全部打成 jar 包,然后放到工程目录下的 jarsDerectory 子目录中。

Gradle创建项目目录结构

如何使用Gradle创建如maven那样的项目结构呢 ?

gradle不像maven那样有固定的项目结构,gradle原声API是不支持的,要想做到这一点,我们可以自定义一个task。

1. 创建一个createJavaProject文件夹

2. 在此目录下创建gradle的构建脚本,build.gradle,内容如下:

apply plugin: 'java'

task "create-dirs" << {
   sourceSets*.java.srcDirs*.each { it.mkdirs() }
   sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}

3. 命令行运行 gradle create-dirs


gradle设置默认任务

defaultTasks 'clean', 'run'task clean << {    println 'Default Cleaning!'}task run << {    println 'Default Running!'}task other << {    println "I'm not a default task!"}

gradle中获取task和project

task helloprintln tasks.hello.nameprintln tasks['hello'].name------------------------------project(':projectA') {    task hello}task helloprintln tasks.getByPath('hello').pathprintln tasks.getByPath(':hello').pathprintln tasks.getByPath('projectA:hello').pathprintln tasks.getByPath(':projectA:hello').path

自定义sourcesets

sourceSets {    main {        groovy {            srcDirs = ['src/groovy']        }    }    test {        groovy {            srcDirs = ['test/groovy']        }    }}

把myibatis的配置文件打包到build对应的classes目录

把myibatis的配置文件打包到build对应的classes目录?

task copyXml(type: Copy) {    from 'src/main/java/com/mybatis/model'    into 'build/classes/main/com/mybatis/model'    exclude '*.java'}//拷贝myibatis的配置文件processResources.dependsOn copyXml//处理资源文件的task依赖于copyXml
首先执行处理资源文件的task,流程图如下:





0 0
原创粉丝点击