Gradle 实战 - 插件-ApiHug准备-工具篇-015

  🤗 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. 工欲善其事,必先利其器

如何去扩展 gradle 自带的功能, gradle 是高度定制化, 我们日常很多功能也是社区插件提供的。 Developing Custom Gradle Pluginsopen in new window

#插件位置

#Build Script

位于 buildSrc 下的插件功能, 只能对本项目可见, 不能跨项目分享。

Gradle 会自动检查 来自buildSrc/src/main/java 的扩展, 如果有扩展, gradle 自动包含里面的插件。

#独立插件

完全独立的项目, 就像独立的第三方 Lib 一样, 可以在不同项目中共享重用。

#Build Script - 实现

两个比较完备的例子: Spring framework 内置插件open in new window & Spring boot 内置插件open in new window

定义插件:

  1. 继承 org.gradle.api.Plugin
  2. 扩展 org.gradle.api.Project

import org.gradle.api.Plugin;
import org.gradle.api.Project;public class GreetingPlugin implements Plugin<Project> {@Overridepublic void apply(Project project) {project.task("hello").doLast(task -> System.out.println("Hello Gradle!"));}
}

buildSrc/build.gradle 内容:


plugins {id 'java-gradle-plugin'
}gradlePlugin {plugins {compileConventionsPlugin {id = "com.dearxue.GreetingPlugin"implementationClass = "com.dearxue.GreetingPlugin"}}
}

在 app 项目中引入:


plugins {// Apply the application plugin to add support for building a CLI application in Java.id 'application'id 'com.dearxue.GreetingPlugin'
}

运行命令:

>gradlew.bat clean build -x testBUILD SUCCESSFUL in 3s
12 actionable tasks: 10 executed, 2 up-to-date
.......gradle-advanced>gradlew.bat app:hello> Task :app:hello
Hello Gradle!

#插件配置

扩展配置 GreetingPluginExtension

public class GreetingPluginExtension {private String greeter = "Baeldung";private String message = "Message from the plugin!"// standard getters and setters
}

改进后的类 GreetingPlugin


public class GreetingPlugin implements Plugin<Project> {@Overridepublic void apply(Project project) {GreetingPluginExtension extension = project.getExtensions().create("greeting", GreetingPluginExtension.class);project.task("hello").doLast(task -> {System.out.println("Hello, " + extension.getGreeter());System.out.println("I have a message for You: " + extension.getMessage());});}
}

修改后的 app/build.gradle:

greeting {greeter = "Stranger"message = "Message from the build script"
}

运行效果:

>gradlew.bat app:hello> Task :app:hello
Hello, Stranger
I have a message for You: Message from the build script

#独立插件 - 实现

最好的参照:

  1. Spring depedency 插件open in new window
  2. Spring boot 插件open in new window

例子参考例子项目中的 plugin 子项目, 包含相关的单元测试:

plugins {// Apply the Java Gradle plugin development plugin to add support for developing Gradle pluginsid 'java-gradle-plugin'
}gradlePlugin {// Define the pluginplugins {greeting {id = 'com.dearxue.greeting'implementationClass = 'com.dearxue.PppPlugin'}}
}configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)// Add a task to run the functional tests
tasks.register('functionalTest', Test) {testClassesDirs = sourceSets.functionalTest.output.classesDirsclasspath = sourceSets.functionalTest.runtimeClasspathuseJUnitPlatform()
}gradlePlugin.testSourceSets(sourceSets.functionalTest)tasks.named('check') {// Run the functional tests as part of `check`dependsOn(tasks.functionalTest)
}tasks.named('test') {// Use JUnit Jupiter for unit tests.useJUnitPlatform()
}

#单元测试

class PppPluginTest {@Testvoid pluginRegistersATask() {// Create a test project and apply the pluginProject project = ProjectBuilder.builder().build();project.getPlugins().apply("com.dearxue.greeting");// Verify the resultassertNotNull(project.getTasks().findByName("greeting"));}
}

#功能测试

@Testvoid canRunTask() throws IOException {writeString(getSettingsFile(), "");writeString(getBuildFile(), "plugins {" + "  id('com.dearxue.greeting')" + "}");// Run the buildGradleRunner runner = GradleRunner.create();runner.forwardOutput();runner.withPluginClasspath();runner.withArguments("greeting");runner.withProjectDir(projectDir);BuildResult result = runner.build();// Verify the resultassertTrue(result.getOutput().contains("Hello from plugin 'com.dearxue.greeting'"));}

#发布

ID 命名规范:

  1. They can contain only alphanumeric characters, “.” and “-“
  2. The id has to have at least one “.” separating the domain name from the plugin name
  3. Namespaces org.gradle and com.gradleware are restricted
  4. An id cannot start or end with “.”
  5. No two or more consecutive “.” characters are allowed

参考下 spring depedency management:


gradlePlugin {plugins {dependencyManagement {displayName = 'Dependency management plugin'description = 'A Gradle plugin that provides Maven-like dependency management functionality'id = 'io.spring.dependency-management'implementationClass = 'io.spring.gradle.dependencymanagement.DependencyManagementPlugin'}}
}

#总结

项目地址 gradle-advanced plugin 教程

我们

api-hug-contact

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/817219.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Unity WebGL 2021 Release-Notes

&#x1f308;WebGL 2021 Release-Notes 版本更新内容2021.3.33WebGL: Fixed a bug that caused for input to not be released when focus was removed from canvas on Windows Chrome.(UUM-53519)2021.3.33WebGL: Fixed the bug that caused for an error to be thrown when …

rv1103/buildroot系统中添加包如v4l2

v4l2: rv1103给出的包中已经有v4l,只需要在menuconfig中打开编译选项&#xff0c;步骤如下&#xff1a; 在luckfox的github网站中下载的源代码在~/linux/luckfox/luckfox-pico-main中目录结构如下&#xff1a; 打开编译选项 cd ./sysdrv/source/buildroot/buildroot-2023.02.…

一种驱动器的功能安全架构介绍

下图提供了驱动器实现安全功能的架构 具有如下特点&#xff1a; 1.通用基于总线或者非总线的架构。可以实现ethercat的FSOE&#xff0c;profinet的profisafe&#xff0c;或者伺服本体安全DIO现实安全功能。 2.基于1oo2D架构&#xff0c;安全等级可以达到sil3。 3.高可用性。单…

第十五篇【传奇开心果系列】Python自动化办公库技术点案例示例:深度解读Python 自动化处理图像在各行各业的应用场景

传奇开心果博文系列 系列博文目录Python自动化办公库技术点案例示例系列 博文目录前言一、行业应用场景介绍二、 **计算机视觉研究与开发示例代码**三、人工智能与机器学习示例代码四、医疗健康领域示例代码五、制造业与质量控制示例代码六、农业与环境科学示例代码七、电子商务…

ai智能机器人怎样获取评分等级话术分组

随着科技的进步&#xff0c;人工智能的理解能力和学习能力会不断加强&#xff0c;从而解放更多的人力&#xff0c;让他们去做更有意义的事情&#xff0c;那要创建一个 AI 电销机器人来获取评分等级并进行话术分组&#xff0c;可以按照以下步骤进行&#xff1a; 数据收集和准备&…

小米汽车值得去吗?最终拒了 offer。

车企选择 今天逛某职场 App 时&#xff0c;无意间看到一篇寻求 offer 抉择意见的帖子&#xff1a; 这位同学刚从加班闻名&#xff08;但 CEO 强调既学华为狼性&#xff0c;也学华为分配&#xff09;的理想汽车离职。 经过了 6 轮面试&#xff0c;收到了小米 offer&#xff0c;但…

【合合TextIn】智能文档处理系列—电子文档解析技术全格式解析

一、引言 在当今的数字化时代&#xff0c;电子文档已成为信息存储和交流的基石。从简单的文本文件到复杂的演示文档&#xff0c;各种格式的电子文档承载着丰富的知识与信息&#xff0c;支撑着教育、科研、商业和日常生活的各个方面。随着信息量的爆炸性增长&#xff0c;如何高…

网络网络层之(2)ARP协议

网络网络层之(2)ARP协议 Author&#xff1a;Once Day Date: 2024年4月1日 漫漫长路&#xff0c;有人对你笑过嘛… 全系列文档可参考专栏&#xff1a;通信网络技术_Once-Day的博客-CSDN博客。 参考文档: 《TCP/IP详解卷一》arp(8) - Linux manual page (man7.org)彻底搞懂系…

C语言中的数据结构--链表的应用2(3)

前言 上一节我们学习了链表的应用&#xff0c;那么这一节我们继续加深一下对链表的理解&#xff0c;我们继续通过Leetcode的经典题目来了解一下链表在实际应用中的功能&#xff0c;废话不多说&#xff0c;我们正式进入今天的学习 单链表相关经典算法OJ题4&#xff1a;合并两个…

pyppeteer和requests简单应用

pyppeteer和requests简单应用 本文章只是分享pyppeteer技术。有些反扒网站可以使用pyppeteer库&#xff0c;完整代码没有分享。 获取相关开发工具软件&#xff0c;可以关注公众号&#xff1a;爬虫探索者。 发送下面图片的关键字可以获取对应软件。sql指的是Navicat。 破解教程可…

Netty之ByteBuff

1、Jdk自带ByteBuffer 1.1、ByteBuffer简介 事实上&#xff0c;jdk自1.4版本&#xff0c;就已经提供了nio的ByteBuffer&#xff0c;用于在Java程序中操作原始数据。ByteBuffer可以用来读取和写入二进制数据&#xff0c;例如网络传输的数据、文件的内容等。 ByterBuffer的部分…

【大数据】Apache Knox 概述

Apache Knox 概述 1.概述1.1 Kerberos 封装1.2 简化客户端证书的管理1.3 Apache Ranger 集成1.4 Hadoop URLs VS Knox URLs 2.自定义 Apache Knox2.1 Topology2.2 Provider2.3 Services2.4 Personalized services 3.Tips3.1 Setting up SSL3.2 常见问题3.2.1 Bulky answer3.2.2…

财务软件行业背景-易舟云

财税是每个企业的基本基石之一。财务报告讲述了公司的故事——它的利润和亏损、收益和债务、税收支出以及可用于未来增长的资产。随着信息时代的飞速发展&#xff0c;财务信息化建设日益完善&#xff0c;大量基于计算机网络的应用系统已经逐步深入财务管理领域。传统的会计录入…

过冲、振铃、非单调性

1、过冲&#xff08;Overshoot&#xff09;和振铃&#xff08;Ringing&#xff09;是电路中常见的信号失真现象&#xff0c;主要出现在开关电源、数字信号传输、通信系统以及其他涉及快速开关动作的电子设备中。它们通常与电路的瞬态响应有关&#xff0c;尤其是当电路受到阶跃输…

【学习笔记十三】EWM常见上架策略介绍

一、手工维护上架策略 系统不确定Storage type 和 bin&#xff0c;需要在创建仓库任务时或者确认仓库任务时手工输入仓位 1.后台配置-定义存储类型的类型0010 ①存储行为&#xff1a;标准仓位 ②入库规则&#xff1a;空仓未或添加至现有库存/空仓位 ③通用仓库任务&#x…

postgresql 备份恢复相关知识点整理归纳 —— 筑梦之路

概述 PG一般有两种备份方式&#xff1a;逻辑备份和物理备份 逻辑备份对于数据量大的场景下耗时较长&#xff0c;恢复也会耗时较长 物理备份拷贝文件的方式相对来说耗时较短&#xff0c;跟磁盘读写性能和网络传输性能有关 逻辑备份 pg_dump pg_dump 将表结构及数据以SQL语句…

Docker in Docker (DinD): 深入探索与实际应用

引言 在软件开发的多样化环境中&#xff0c;Docker已成为一种重要的工具&#xff0c;用于实现应用的快速部署和可靠性。Docker in Docker&#xff08;DinD&#xff09;进一步推进了这一概念&#xff0c;使开发者能在一个Docker容器中运行另一个Docker实例。本文将详细探讨DinD…

微信小程序实现预约生成二维码

业务需求&#xff1a;点击预约按钮即可生成二维码凭码入校参观~ 一.创建页面 如下是博主自己写的wxml&#xff1a; <swiper indicator-dots indicator-color"white" indicator-active-color"blue" autoplay interval"2000" circular > &…

SpringBoot - Logback 打印第三方 Jar 日志解决方案

问题描述 最近碰到一个很苦恼的问题&#xff0c;就是第三方的 Jar 在自己项目里日志可以正常输出&#xff0c;但是一旦被引用到其他项目里&#xff0c;就日志死活打不出来…… 解决方案 这是原来的配置 - logback.xml <?xml version"1.0" encoding"UTF-8…

ARM 三个小灯闪烁

.text .global _start _start: 使能GPIOE的外设时钟 LDR R0,0x50000A28 指定基地址 LDR R1,[R0] 读取r0中的数据保存到r1中 ORR R1,R1,#(0X3<<4) [4]设置为1,表示 STR R1,[R0] 将修改之后的值放回去 设置PE10,PE8为输出 LDR R0,0X50006000…