对于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之类的。

main

Contains the production source code of the project, which is compiled and assembled into a JAR.

test

Contains 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://docs.gradle.org/current/userguide/building_java_projects.html#sec:custom_java_source_set_paths

https://blog.csdn.net/honjane/article/details/52579304

Gradle中的SourceSet理解的更多相关文章

  1. 深入理解gradle中的task

    目录 简介 定义task tasks 集合类 Task 之间的依赖 定义task之间的顺序 给task一些描述 task的条件执行 task rule Finalizer tasks 总结 深入理解g ...

  2. Gradle 1.12 翻译——第十七章. 从 Gradle 中调用 Ant

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  3. Gradle中的闭包

    Gradle是基于Groovy的DSL基础上的构建工具,Gradle中的闭包,其原型上实际上即Groovy中闭包.而在表现形式上,其实,Gradle更多的是以约定和基于约定基础上的配置去展现.但本质上 ...

  4. gradle问题总结与理解(一篇文章带你理解android studio 与gradle 的关系)

    前言:近日在网上找了个很不错的安卓二维码美化,由于下载的项目经常出问题,且不方便依赖使用,因此我想把它写个demo,并把源码发布到jcenter中,修改还是很顺利的,运行项目到手机也没问题,发布遇到了 ...

  5. Ant,Maven与Gradle的概念的理解

    转载地址:http://www.jianshu.com/p/cd8fe9b16369# 我们还是以AndroidStudio 2.1.1为例来讲. 用AndroidStudio就逃不开跟Gradle打 ...

  6. Gradle中的buildScript,gradle wrapper,dependencies等一些基础知识

    就想收藏一篇好文,哈哈,无他 Gradle中的buildScript代码块 - 黄博文 然后记录一些gradle的基础知识: 1.gradle wrapper就是对gradle的封装,可以理解为项目内 ...

  7. 在gradle中构建java项目

    目录 简介 构建java项目的两大插件 管理依赖 编译代码 管理resource 打包和发布 生成javadoc 简介 之前的文章我们讲到了gradle的基本使用,使用gradle的最终目的就是为了构 ...

  8. gradle中使用嵌入式(embedded) tomcat, debug 启动

    在gradle项目中使用embedded tomcat. 最开始部署项目需要手动将web项目打成war包,然后手动上传到tomcat的webapp下,然后启动tomcat来部署项目.这种手动工作通常还 ...

  9. Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区

    Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...

随机推荐

  1. JQuery --- 第五期 (JQuery节点操作)

    学习笔记 1.JQuery添加节点相关方法 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  2. Webservice发布

    此文甚好,转载自:http://blog.163.com/java_player@126/blog/static/127930738200981555021925/ 某些地方笔者已经加以改进. 使用工 ...

  3. Unity3d之Coroutine

    在Unity3d中使用C#时,Coroutine是一个大有用处的好东西,至于怎么用网上多的是讲,我仅在此记录最近一次使用中的小发现. 因为某种需求,要在一个Coroutine实现中使用while循环, ...

  4. Sqlserver 密码过期时间查询

    DECLARE @login nvarchar(30) -- 查询设定密码过期的登陆账号SELECT @login = nameFROM sys.sql_loginsWHERE is_expirati ...

  5. 升级windows 10后网络连接异常

    升级 windows 10,QQ无法连接,显示“登陆超时,请检查网络或者防火墙设置”.打开360软件助手,准备升级QQ试试,360软件助手也显示网络异常. 解决方法: 右键点击开始菜单,命令提示符(管 ...

  6. 纸壳CMS的插件加载机制

    纸壳CMS是一个开源的可视化设计CMS,通过拖拽,在线编辑的方式来创建网站. GitHub https://github.com/SeriaWei/ZKEACMS.Core 欢迎Star,Fork,发 ...

  7. MaxScript通过.net发送邮件

    Fn SmtpSendMail argSmtpAddress argSenderAddress argSenderPassword argTargetAddress argTitle argMessa ...

  8. Android 的一些中文文档

    https://blog.csdn.net/qq_36467463/article/details/77990089    //安卓mediaformat api详解 https://www.cnbl ...

  9. Java50道经典习题-程序17 猴子吃桃问题

    题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上想再吃时,见只 ...

  10. iOS没你想的那么安全?

    iOS应用由于直接运行在用户的手机上,而不是运行在后台服务器上,所以更容易被攻击. 任何系统都会有木马病毒的产生,不存在绝对的安全,iOS应用由于直接运行在用户的手机上,而不是运行在后台服务器上,所以 ...