Gradle中的SourceSet理解
对于maven项目来说,目录结构是固定的,也就是像这样:
src/main/
src/main/java/
src/main/resources/ src/test/
src/test/java/
src/test/jresources/
Gradle也是一样的,他也有一个约定的目录结构,格式和maven的结构一样。但不同的是,gradle的目录结构是可以改的,官网中叫做Changing the project layout。怎么改,或者说怎么自定义?这就要用到SourceSet了。
SourceSet到底是什么,官网中像下面这样解释的,大意是说SourceSet包括源文件及位置、它们的依赖、编译输出的位置这三个概念,SourceSet可以把不同文件目录里的文件按照逻辑关系组织起来。具体请参考Declaring your source files via source sets。
Gradle’s Java support was the first to introduce a new concept for building source-based projects: source sets. The main idea is that source files and resources are often logically grouped by type, such as application code, unit tests and integration tests. Each logical group typically has its own sets of file dependencies, classpaths, and more. Significantly, the files that form a source set don’t have to be located in the same directory!
Source sets are a powerful concept that tie together several aspects of compilation:
the source files and where they’re located
the compilation classpath, including any required dependencies (via Gradle configurations)
where the compiled class files are placed
Gradle有两个SourceSet是默认必须有的,这两个SourceSet的特殊性在于,你不需要在build文件中去申明它们,并且,操作它们的时候,也不需要带上他们的名字,比如你编译的时候,只需要compile,测试的时候,只需要test,而不是compilemain,testtest之类的。
mainContains the production source code of the project, which is compiled and assembled into a JAR.
testContains your test source code, which is compiled and executed using JUnit or TestNG. These are typically unit tests, but you can include any test in this source set as long as they all share the same compilation and runtime classpaths
SourceSet有一些属性,比如名字、源文件位置、资源位置、classpath等等,这些属性有些是只读的,有些是可写的,并且可以被当作变量来引用。有可写的属性那么就提供了自定义的功能了,比如,你可以改变一个SourceSet源代码的位置,像这样下面这样,你就改变了main这个SourceSet的源代码目录和资源目录。
sourceSets {
main {
java {
srcDirs = ['src/java']
}
resources {
srcDirs = ['src/resources']
}
}
}
这样,gradle就只在src/java下搜源代码,而不是在src/main/java下。如果你只是想添加额外的目录,而不想覆盖原来的目录,则像下面这样:
sourceSets {
main {
java {
srcDir 'thirdParty/src/main/java'
}
}
}
此时,gradle就会同时在src/main/java和thirdParty/src/main/java两个目录下都进行源代码搜索。
除了可以更改原有的SourceSet,你也可以添加一个新的SourceSet。比如我们在test这个SourceSet中,做的一般都是单元测试,如果我们要做集成测试和验收测试,因为这些测试有它们自己的dependencies, classpaths, 以及其他的资源,所以这些测试应该是单独分开的,而不应该和单元测试混为一谈。但是,集成测试和验收测试也是这个项目的一部分,因此我们也不能为集成测试或验收测试单独建立一个项目。它们也不是子项目,所以用subproject也不合适。此时,我们就可以新建一个集成测试或者验收测试的SourceSet,把相关的测试资源管理起来。比如你需要建立一个集成测试,则你定义如下SourceSet:
sourceSets {
integrationTest {
java
resource
compileClasspath += sourceSets.test.runtimeClasspath
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}
然后,你添加一个集成测试的运行任务:
task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
}
在这个任务中,你指定了测试的目录为集成测试SourceSet的输出目录,classpath为集成测试的runtimeClasspath,这样,集成测试就可以运行了。
参考:
https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_source_sets
https://blog.csdn.net/honjane/article/details/52579304
Gradle中的SourceSet理解的更多相关文章
- 深入理解gradle中的task
目录 简介 定义task tasks 集合类 Task 之间的依赖 定义task之间的顺序 给task一些描述 task的条件执行 task rule Finalizer tasks 总结 深入理解g ...
- Gradle 1.12 翻译——第十七章. 从 Gradle 中调用 Ant
有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...
- Gradle中的闭包
Gradle是基于Groovy的DSL基础上的构建工具,Gradle中的闭包,其原型上实际上即Groovy中闭包.而在表现形式上,其实,Gradle更多的是以约定和基于约定基础上的配置去展现.但本质上 ...
- gradle问题总结与理解(一篇文章带你理解android studio 与gradle 的关系)
前言:近日在网上找了个很不错的安卓二维码美化,由于下载的项目经常出问题,且不方便依赖使用,因此我想把它写个demo,并把源码发布到jcenter中,修改还是很顺利的,运行项目到手机也没问题,发布遇到了 ...
- Ant,Maven与Gradle的概念的理解
转载地址:http://www.jianshu.com/p/cd8fe9b16369# 我们还是以AndroidStudio 2.1.1为例来讲. 用AndroidStudio就逃不开跟Gradle打 ...
- Gradle中的buildScript,gradle wrapper,dependencies等一些基础知识
就想收藏一篇好文,哈哈,无他 Gradle中的buildScript代码块 - 黄博文 然后记录一些gradle的基础知识: 1.gradle wrapper就是对gradle的封装,可以理解为项目内 ...
- 在gradle中构建java项目
目录 简介 构建java项目的两大插件 管理依赖 编译代码 管理resource 打包和发布 生成javadoc 简介 之前的文章我们讲到了gradle的基本使用,使用gradle的最终目的就是为了构 ...
- gradle中使用嵌入式(embedded) tomcat, debug 启动
在gradle项目中使用embedded tomcat. 最开始部署项目需要手动将web项目打成war包,然后手动上传到tomcat的webapp下,然后启动tomcat来部署项目.这种手动工作通常还 ...
- Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区
Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...
随机推荐
- DMV--sys.dm_os_ring_buffers
DMV 'sys.dm_os_ring_buffers' 可以用来诊断数据库连接和数据库内存方面的问题,但MSDN上找不到相应的介绍,网上找到以下相关资料: 1>sys.dm_os_ring_b ...
- Jenkins的多个任务并串联参数传递
Jenkins的多个任务并串联参数传递 Parameterized Trigger Plugin插件可以使多个job连接的时候可以传递一些job相关的参数信息. 1.Parameterized Tri ...
- Visual Studio Code Angular4 配置环境
首先在本机安装node.js才能用 npm 命令 环境搭建 安装node.jsnpm install -g @angular/cli 安装第三方库npm install --save bootstra ...
- java的一些命名规范吧
注意事项: 1.由于Java是面向对象编程的,所以在命名的时候尽量选择名词. 2.(Camel-Case)驼峰命名法:当变量名或函式名是由一个或多个单字连结在一起,而构成的唯一识别字时,首字母以小写开 ...
- 记录一下显示Map<String, ArrayList<String>>中的ArrayList里的数据的操作
1.有以下数据: ArrayList<Employee> emp = new ArrayList<>(); emp.add(new Employee("zhang&q ...
- django系列3.4-- request对象和response对象(未完待续)
一.request对象 详细信息可以查阅django官方文档 共有五种请求相关的常用值 request.path_info 返回用户访问的url不包括域名 request.method 请求中使用的H ...
- GO学习笔记 - Go 只有一种循环结构—— for 循环。
一,Go 只有一种循环结构—— for 循环. 官方教程:https://tour.go-zh.org/flowcontrol/1 Go 只有一种循环结构—— for 循环. 基本的 for 循环包含 ...
- “全栈2019”Java异常第二章:如何处理异常?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- [Bootstrap-Table] 中的事件用例
[Bootstrap-Table] 中的事件 -------------------------------------------------- <div class="alert ...
- 集合之六:Map接口
Map接口概述 Map接口中的集合和Collection接口中的集合在存储数据的格式上有很大的不同,Map接口下的内容是以<K , V> ,键值对的形式存储的,我们查询API,Map接口的 ...