zoukankan      html  css  js  c++  java
  • 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之类的。

    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

  • 相关阅读:
    projecthashing
    Windows 环境 gRPC环境搭建
    gitlab 适配性问题
    对 Golang 简洁架构实战的理解,以及 Golang 开源项目包定义的解惑
    Golang 关于百分比,小数点为数的问题
    Golang net/http 库日常 http 请求操作
    解决mysql建立事件时出现 “Cannot proceed because system tables used by Event Scheduler were found damaged at server start” 的错误
    如何在Linux安装Wine
    Public key for *.rpm is not installed
    从 IClassFactory 为 CLSID 为 {AA40D1D6CAEF4A56B9BBD0D3DC976BA2} 的 COM 组件创建实例失败,原因是出现以下错误: c001f011。
  • 原文地址:https://www.cnblogs.com/JMLiu/p/10734997.html
Copyright © 2011-2022 走看看