Gradle 实战 - SourceSet 配置 -ApiHug准备-工具篇-011

2024-04-14 00:44

本文主要是介绍Gradle 实战 - SourceSet 配置 -ApiHug准备-工具篇-011,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  🤗 ApiHug × {Postman|Swagger|Api...} = 快↑ 准√ 省↓

  1. GitHub - apihug/apihug.com: All abou the Apihug   
  2. apihug.com: 有爱,有温度,有质量,有信任
  3. ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace

ApiHug 整个工具链基于 Gradle, 使用 ApiHug 准备工作最先需要学习的就是 gradle. 工欲善其事,必先利其器

多 Sourceset 管理 也是 ApiHug 喜欢使用的, 我们称之为 干湿分离, 不同代码放置不同目录

SourceSet 给我们在 gradle 项目中组织我们代码结构的自由; Gradle SourceSet 定义open in new window & org.gradle.api.tasks.SourceSet 支持的APIopen in new window。

SourceSet到底是什么,官网中像下面这样解释的,SourceSet包括源文件及位置、它们的依赖、编译输出的位置这三个概念,SourceSet可以把不同文件目录里的文件按照逻辑关系组织起来。

  1. the source files and where they’re located
  2. the compilation classpath, including any required dependencies (via Gradle configurations)
  3. where the compiled class files are placed

Source sets and Java compilation

#项目结构

一般的项目结构可能看起来:

source-sets ├── src │    ├── main │    │    └── java │    │        ├── SourceSetsMain.java│    │        └── SourceSetsObject.java│    └── test │         └── java │             └── SourceSetsTest.java└── build.gradle 

build.gradle 内容:


apply plugin : "java"
description = "Source Sets example"
test {testLogging {events "passed", "skipped", "failed"}
}
dependencies {   implementation('org.apache.httpcomponents:httpclient:4.5.12')testImplementation('junit:junit:4.12')
}

java plugin 默认 src/main/java & src/test/java 为源码目录, 手动打印下:

task printSourceSetInformation(){doLast{sourceSets.each { srcSet ->println "["+srcSet.name+"]"print "-->Source directories: "+srcSet.allJava.srcDirs+"\n"print "-->Output directories: "+srcSet.output.classesDirs.files+"\n"println ""}}
}

SourceSet 文档open in new window, 有非常多的字段,这里不一一枚举。

你可以看到输出如下:

 ./gradlew printSourceSetInformation> Task :source-sets:printSourceSetInformation
[main]
-->Source directories: [.../source-sets/src/main/java]
-->Output directories: [.../source-sets/build/classes/java/main][test]
-->Source directories: [.../source-sets/src/test/java]
-->Output directories: [.../source-sets/build/classes/java/test]

默认两个 sourcesets 一个是 main 另外一个是 test 。


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

#其他默认配置

java 插件同样生成一些默认的 gradle 配置, 基本遵循这样的规则 <sourceSetName><configurationName>, 我们熟悉的 main & test

比如我们配置的 build.gradle:

dependencies { implementation('org.apache.httpcomponents:httpclient:4.5.12') testImplementation('junit:junit:4.12') 
}

注意 implementation 而不是 mainimplementation, gradle 为 main 做了例外。

默认 testImplementation 继承 implementation, 继承他所有的 dependencies & outputs

打印验证:


task printSourceSetInformation(){doLast{sourceSets.each { srcSet ->println "["+srcSet.name+"]"print "-->Source directories: "+srcSet.allJava.srcDirs+"\n"print "-->Output directories: "+srcSet.output.classesDirs.files+"\n"print "-->Compile classpath:\n"srcSet.compileClasspath.files.each { print "  "+it.path+"\n"}println ""}}
}

输出如下:


[main]
// same output as before
-->Compile classpath:.../httpclient-4.5.12.jar.../httpcore-4.4.13.jar.../commons-logging-1.2.jar.../commons-codec-1.11.jar[test]
// same output as before
-->Compile classpath:.../source-sets/build/classes/java/main.../source-sets/build/resources/main.../httpclient-4.5.12.jar.../junit-4.12.jar.../httpcore-4.4.13.jar.../commons-logging-1.2.jar.../commons-codec-1.11.jar.../hamcrest-core-1.3.jar

注意 test 的编译路径上, 既包含 main 也包含自己的比如 junit 依赖。


public class SourceSetsTest {@Testpublic void whenRun_ThenSuccess() {SourceSetsObject underTest = new SourceSetsObject("lorem", "ipsum");assertThat(underTest.getUser(), is("lorem"));assertThat(underTest.getPassword(), is("ipsum"));}
}

所以测试用例的项目, 得以运行, 依赖 junit + main


.\gradlew.bat  source-sets:build test  BUILD SUCCESSFUL in 3s
6 actionable tasks: 1 executed, 5 up-to-date

#定制 Source Sets

特别在我们定制一些 integration 测试目录的时候, 有两种方式一种是筛选 IT 结尾的测试类, 另外一种是用独立的 sourceset.

#文件后缀方式

include "**/*IT*", "**/*IntTest*" 表达式来匹配:

task integrationTest(type: Test) {useJUnitPlatform()description = "Execute integration tests."group = "verification"include "**/*IT*", "**/*IntTest*"testLogging {events 'FAILED', 'SKIPPED'}
}

#独立文件目录

目录结构:


└─src├─itest│  └─java│      └─com│          └─dearxue│              └─itest│                      SourceSetsItest.java│├─main│  └─java│      └─com│          └─dearxue│              └─main│                      SourceSetsMain.java│                      SourceSetsObject.java│└─test└─java└─com└─dearxue└─testSourceSetsTest.java

build 配置


sourceSets {itest {java {}}
}

这里我们没有配置任何source 目录, 因为目录 itest 匹配 sourceSets 名称 itest。 当然我们也可以定制一个自己的名字:

sourceSets{itest {java {srcDirs("src/itest")}}
}

目录输出:

Task :source-sets:printSourceSetInformation
[itest]
-->Source directories: [....\source-sets\src\itest\java]
-->Output directories: [....\source-sets\build\classes\java\itest]
-->Compile classpath:
....

#Source Sets 定制依赖

如果你还记得 main & implementation 和 test & testImplementation, 那么你的定制 sourcesets 也可以有自己的依赖关系;

遵从这样规则: <sourceSetName><configurationName>


dependencies {implementation('org.apache.httpcomponents:httpclient:4.5.12')testImplementation('junit:junit:4.12')itestImplementation('com.google.guava:guava:29.0-jre')
}

itestImplementation 只对我们的 itest 生效, 添加如下测试用例:


public class SourceSetsItest {@Testpublic void givenImmutableList_whenRun_ThenSuccess() {SourceSetsObject underTest = new SourceSetsObject("lorem", "ipsum");List<String> someStrings = ImmutableList.of("deraxue", "is", "cool");assertThat(underTest.getUser(), is("lorem"));assertThat(underTest.getPassword(), is("ipsum"));assertThat(someStrings.size(), is(3));}
}

添加独立针对 itest 的任务:


task itest(type: Test) {description = "Run integration tests"group = "verification"testClassesDirs = sourceSets.itest.output.classesDirsclasspath = sourceSets.itest.runtimeClasspath
}

注意这部分声明是在 gradle 的 configuration 阶段执行的, 也就是他们的执行的顺序非常重要, 我们还不能引用到 itest 的sources set; 这样的执行结果会:


$ ./gradlew clean itest// some compilation issuesFAILURE: Build failed with an exception.* What went wrong:
Execution failed for task ':source-sets:compileItestJava'.
> Compilation failed; see the compiler error output for details.

新的 itest 生成独立的配置, 也就是 itestImplementation 没有继承 JUnit 依赖, 也没有继承 main 的 output


sourceSets{itest {compileClasspath += sourceSets.main.outputruntimeClasspath += sourceSets.main.outputjava {}}
}// dependencies declaration
configurations {itestImplementation.extendsFrom(testImplementation)itestRuntimeOnly.extendsFrom(testRuntimeOnly)
}

现在再运行就没有问题了。

项目地址: Gradle Source Sets 例子open in new window

#一些配置参考


//protobuf generated 目录配置
sourceSets {main {java {srcDirs 'build/generated/source/proto/main/grpc'srcDirs 'build/generated/source/proto/main/java'}}
}//分门别类设置不同测试目录
sourceSets {componentTest {compileClasspath += sourceSets.main.output + sourceSets.test.outputruntimeClasspath += sourceSets.main.output + sourceSets.test.output}apiTest {compileClasspath += sourceSets.main.output + sourceSets.test.outputruntimeClasspath += sourceSets.main.output + sourceSets.test.output}
}//各个测试任务的目录依赖
task componentTest(type: Test) {description = 'Run component tests.'group = 'verification'testClassesDirs = sourceSets.componentTest.output.classesDirsclasspath = sourceSets.componentTest.runtimeClasspathshouldRunAfter test
}task apiTest(type: Test) {description = 'Run API tests.'group = 'verification'testClassesDirs = sourceSets.apiTest.output.classesDirsclasspath = sourceSets.apiTest.runtimeClasspathshouldRunAfter componentTest
}check.dependsOn componentTest
check.dependsOn apiTest

api-hug-contact

这篇关于Gradle 实战 - SourceSet 配置 -ApiHug准备-工具篇-011的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/901671

相关文章

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可