当前位置: 首页 > news >正文

idea 插件开发自动发布到 nexus 私服中(脚本实例)

如下脚本内容为 idea 插件开发项目中的 build.gradle.kts 文件示例,其中自定了 updatePluginsXmlToNexusuploadPluginToNexus 两个任务,一个用来自动修改 nexus 中的配置文件,一个用来自动将当前插件打包后的 zip 文件上传到 nexus 私服中。

脚本仅供参考:

import okhttp3.Credentials
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import org.dom4j.io.OutputFormat
import org.dom4j.io.SAXReader
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.w3c.dom.Element
import java.io.StringReader
import java.io.StringWriter
import java.nio.file.Files
import java.nio.file.Paths
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactoryplugins {id("java") // 使用Java插件id("org.jetbrains.kotlin.jvm") version "2.1.20-RC" // 使用Kotlin JVM插件,指定版本id("org.jetbrains.intellij.platform") version "2.4.0" // 使用IntelliJ插件,用于开发JetBrains插件,指定版本id("com.github.node-gradle.node") version "7.1.0" // Node.js插件,集成Node.js和npm到Gradle构建中
}
buildscript {repositories {mavenCentral()}dependencies {// 添加 OkHttp 依赖classpath("com.squareup.okhttp3:okhttp:4.12.0")classpath("org.dom4j:dom4j:2.1.4")}
}
repositories {// mavenCentral() // 定义Maven中央仓库作为依赖库来源mavenLocal()intellijPlatform {defaultRepositories()}maven(url = "https://plugins.gradle.org/m2/")maven(url = "https://oss.sonatype.org/content/repositories/releases/")
}dependencies {compileOnly("org.projectlombok:lombok:1.18.36") // 提供Lombok支持,仅在编译时使用annotationProcessor("org.projectlombok:lombok:1.18.36") // Lombok注解处理器intellijPlatform {// 获取在gradle.properties中配置的参数platformVersionval version = providers.gradleProperty("platformVersion") // 2023.2.6// IntellijIdeaCommunity=IC, IntellijIdeaUltimate=IUcreate(org.jetbrains.intellij.platform.gradle.IntelliJPlatformType.IntellijIdeaCommunity, version)// 使用bundledPlugin设置捆绑的插件//bundledPlugin("com.intellij.java")// 使用plugin设置当前插件依赖的其他插件//plugin("org.intellij.scala", "2024.1.4")pluginVerifier()testFramework(org.jetbrains.intellij.platform.gradle.TestFrameworkType.Platform)}
}group = "com.shanhy.plugin"
version = "1.0.0-20250520"// 文档 https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-extension.html#intellijPlatform
intellijPlatform {pluginConfiguration {id = "demo-idea-apiflow-designer"name = "demo ApiFlow Designer" // 名称// 直接使用项目级别的 version 属性version = project.version.toString()description ="该插件时一个为 IntelliJ IDEA 打造的 demo Http 接口设计器插件。它提供了一个 `.apiflow` 文件编辑器,具有可视化设计快速开发 Http 接口的能力。"// 读取 CHANGELOG.md 文件的内容changeNotes = Files.readString(Paths.get(project.projectDir.path, "CHANGELOG.md"))// 适用的idea版本ideaVersion {sinceBuild = "232" // 插件支持的最低IDE构建号untilBuild = "251.*" // 插件支持的最高IDE构建号}// 产品描述productDescriptor {// ...}// 开发者信息vendor {name = "shanhy"email = "shanhy@shanhy.com"url = "http://www.shanhy.com"}}// https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-extension.html#intellijPlatform-signingsigning {// 配置在gradle.properties中certificateChain = providers.gradleProperty("certificateChain") // 签名证书链privateKey = providers.gradleProperty("privateKey") // 签名私钥password = providers.gradleProperty("privateKeyPassword") // 签名私钥的密码}// https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-extension.html#intellijPlatform-publishingpublishing {token = providers.gradleProperty("publishToken") // 发布插件时使用的令牌}// https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-extension.html#intellijPlatform-pluginVerificationpluginVerification {// ...}
}node {version.set("20.11.1") // Node.js版本设置download.set(true) // 自动下载Node.js
}tasks {// 设置JVM兼容性版本withType<JavaCompile> {sourceCompatibility = JvmTarget.JVM_17.target // Java源代码兼容版本targetCompatibility = JvmTarget.JVM_17.target // 编译目标版本}withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {compilerOptions {jvmTarget.set(JvmTarget.JVM_17) // Kotlin编译目标版本}}val buildNpm = register<com.github.gradle.node.npm.task.NpmTask>("buildNpm") {args.set(listOf("run", "build")) // NPM任务参数,运行npm buildworkingDir.set(file("${project.projectDir}/editor-web")) // 设置工作目录}buildPlugin {// 构建插件前先执行NPM构建任务// 如果你的前端页面没有做修改,在反复进行插件开发打包测试,可以注释掉这个构建依赖,不然每次都重复构建没有修改的前端浪费时间dependsOn(buildNpm)}// 定义 Nexus 相关配置val nexusBaseUrl = providers.gradleProperty("nexusBaseUrl").getOrElse("https://nexus.shanhy.com")val nexusUsername = providers.gradleProperty("nexusUsername").getOrElse("")val nexusPassword = providers.gradleProperty("nexusPassword").getOrElse("")// 更新 plugins.xml 的任务val updatePluginsXmlToNexus = register("updatePluginsXmlToNexus") {group = "deployment"description = "更新 Nexus 上的 pluginsXml 文件版本信息"// 使用 Provider 延迟获取所有值val pluginId = providers.provider { intellijPlatform.pluginConfiguration.id.get() }val pluginVersion = providers.provider { project.version.toString() }val sinceBuild = providers.provider { intellijPlatform.pluginConfiguration.ideaVersion.sinceBuild.get() }val changelogFile = providers.provider { layout.projectDirectory.file("CHANGELOG.md").asFile }val projectName = providers.provider { project.name }outputs.upToDateWhen { false } // 总是执行更新doLast {// 在任务执行时获取所有需要的值val pluginIdValue = pluginId.get()val pluginVersionValue = pluginVersion.get()val sinceBuildValue = sinceBuild.get()val changelogFileValue = changelogFile.get()val projectNameValue = projectName.get()// 读取 CHANGELOG.md 内容val changelogContent = changelogFileValue.readText()val client = OkHttpClient.Builder().followRedirects(true).followSslRedirects(true).build()// 创建认证信息val credentials = Credentials.basic(nexusUsername, nexusPassword)// 下载现有的 plugins.xmlval tempFile = File.createTempFile("plugins", ".xml")try {client.newCall(Request.Builder().url("$nexusBaseUrl/repository/raw-public/idea-plugin/plugins.xml").build()).execute().use { response ->if (!response.isSuccessful) {val errorBody = response.body?.string() ?: "无错误详情"throw GradleException("下载文件失败: ${response.code} ${response.message}\n错误详情: $errorBody")}val contentType = response.header("Content-Type", "")if (contentType != null) {if (!contentType.contains("xml") && !contentType.contains("text/plain")) {println("警告: 响应Content-Type不是XML: $contentType")}}response.body?.byteStream()?.use { input ->tempFile.outputStream().use { output ->input.copyTo(output)}} ?: throw GradleException("下载文件失败: 响应体为空")}// 检查下载的文件if (!tempFile.exists() || tempFile.length() == 0L) {throw GradleException("下载文件失败,文件不存在或为空")}// 读取文件内容进行检查val content = tempFile.readText()// 验证下载的文件是否为有效的 XMLtry {DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(tempFile)} catch (e: Exception) {throw GradleException("下载的文件不是有效的 XML: ${e.message}\n文件内容: ${content}...")}// 读取并解析 XMLval document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(tempFile)val xpath = XPathFactory.newInstance().newXPath()// 查找对应的插件节点val pluginNode = xpath.evaluate("//plugin[@id='$pluginIdValue']", document, XPathConstants.NODE)as? Element?: throw GradleException("插件 '$pluginIdValue' 在 plugins.xml 中不存在,请先在 plugins.xml 中手动添加插件配置")// 更新 plugin 节点的属性pluginNode.setAttribute("url", "$nexusBaseUrl/repository/raw-hosted/idea-plugin/files/$projectNameValue-$pluginVersionValue.zip")pluginNode.setAttribute("version", pluginVersionValue)// 更新 idea-version 节点val ideaVersionNode = xpath.evaluate(".//idea-version", pluginNode, XPathConstants.NODE) as? ElementideaVersionNode?.setAttribute("since-build", sinceBuildValue)// 更新或创建 change-notes 节点val changeNotesNode = xpath.evaluate(".//change-notes", pluginNode, XPathConstants.NODE) as? Elementif (changeNotesNode != null) {while (changeNotesNode.hasChildNodes()) {changeNotesNode.removeChild(changeNotesNode.firstChild)}val cdataNode = document.createCDATASection(changelogContent)changeNotesNode.appendChild(cdataNode)}// 保存更新后的文件val transformer = TransformerFactory.newInstance().newTransformer()transformer.setOutputProperty(OutputKeys.INDENT, "yes")transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2")// 使用 StringWriter 先获取转换后的内容val writer = StringWriter()transformer.transform(DOMSource(document),StreamResult(writer))// 使用 dom4j 重新格式化 XML,去除xml中的多余空行和美化xml格式val xmlContent = writer.toString()val reader = SAXReader()val dom4jDocument = reader.read(StringReader(xmlContent))// 创建格式化配置val format = OutputFormat.createPrettyPrint().apply {encoding = "UTF-8"              // 设置输出编码isNewlines = true              // 每个节点换行isTrimText = true              // 是否去除元素中的文本前后空白isPadText = true               // 补齐空白以对齐标签isExpandEmptyElements = true   // <tag/> 变成 <tag></tag>isSuppressDeclaration = false  // 是否省略头部 <?xml ...?> 声明isOmitEncoding = false         // 是否省略 encoding 属性}// 将格式化后的 XML 写入字符串val stringWriter = StringWriter()val xmlWriter = org.dom4j.io.XMLWriter(stringWriter, format)xmlWriter.write(dom4jDocument)xmlWriter.close()// 将清理后的内容写入临时文件tempFile.writeText(stringWriter.toString())// 上传新文件println("正在上传新文件...")val requestBody = MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("raw.directory", "/idea-plugin").addFormDataPart("raw.asset1.filename", "plugins.xml").addFormDataPart("raw.asset1","plugins.xml","application/xml".toMediaTypeOrNull()?.let { mediaType ->tempFile.asRequestBody(mediaType)} ?: throw GradleException("无法创建请求体")).build()val uploadRequest = Request.Builder().url("$nexusBaseUrl/service/rest/v1/components?repository=raw-hosted").post(requestBody).header("Authorization", credentials).build()client.newCall(uploadRequest).execute().use { response ->if (!response.isSuccessful) {throw GradleException("上传文件失败: ${response.code} ${response.message}")}}} finally {tempFile.delete()}}}// 上传 zip 文件到 Nexus 的任务val uploadPluginToNexus = register("uploadPluginToNexus") {group = "deployment"description = "上传插件 zip 文件到 Nexus"// 声明任务的输入和输出,以支持配置缓存val zipFileName = "${project.name}-${project.version}.zip"val zipFile = layout.buildDirectory.file("distributions/$zipFileName").get().asFileinputs.file(zipFile)outputs.upToDateWhen { false } // 总是执行上传dependsOn(buildPlugin)doLast {if (!zipFile.exists()) {throw GradleException("插件 zip 文件未找到: ${zipFile.absolutePath}")}val client = okhttp3.OkHttpClient.Builder().followRedirects(true).followSslRedirects(true).build()// 创建认证信息val credentials = okhttp3.Credentials.basic(nexusUsername, nexusPassword)// 构建上传请求val requestBody = okhttp3.MultipartBody.Builder().setType(okhttp3.MultipartBody.FORM).addFormDataPart("raw.directory", "/idea-plugin/files").addFormDataPart("raw.asset1.filename", zipFileName).addFormDataPart("raw.asset1",zipFileName,"application/zip".toMediaTypeOrNull()?.let { mediaType ->zipFile.asRequestBody(mediaType)} ?: throw GradleException("无法创建请求体")).build()val uploadRequest = Request.Builder().url("$nexusBaseUrl/service/rest/v1/components?repository=raw-hosted").post(requestBody).header("Authorization", credentials).build()client.newCall(uploadRequest).execute().use { response ->if (!response.isSuccessful) {val errorBody = response.body?.string() ?: "无错误详情"throw GradleException("上传文件失败: ${response.code} ${response.message}\n错误详情: $errorBody")}println("插件文件上传成功: $zipFileName")}}}// 组合任务:部署到 Nexusregister("deployToNexus") {group = "deployment"description = "部署插件到 Nexus 仓库"dependsOn(uploadPluginToNexus, updatePluginsXmlToNexus)}
}sourceSets {main {resources {// 指定内容区的前端静态资源目录,这个决定了最终被作为资源打包到插件中srcDir("${project.projectDir}/editor-web/dist")}}
}

(END)

相关文章:

  • 随记1-LLM多轮对话的陷阱
  • LTX-Videov本地部署教程:时空扩散+多尺度渲染,重塑AI视频研究范式
  • 至此(day1-day4)代码详解(ai辅助整理)
  • Python代码加密与发布方案详解
  • 计算机图形学Games101笔记--几何
  • leetcode字符串篇【公共前缀】:14-最长公共前缀
  • NebulaGraph学习笔记-SessionPool之Session not existed
  • 常见高速电路设计与信号完整性核心概念
  • SVA 断言16.9 Sequence operations序列运算翻译笔记(12)
  • 香港科技大学(广州)智能制造理学硕士招生宣讲会——深圳大学专场
  • Nextjs App Router 开发指南
  • leetcode 找到字符串中所有字母异位词 java
  • 百度网盘加速补丁v7.14.1.6使用指南|PC不限速下载实操教程
  • 你知道mysql的索引下推么?
  • Doris高性能读能力与实时性实现原理
  • 【优秀三方库研读】在 quill 开源库中 QUILL_MAGIC_SEPARATOR 的作用是什么,解决了什么问题
  • 【Java】封装在 Java 中是怎样实现的?
  • 基于springboot的网上学校超市商城系统【附源码】
  • [Vue]组件介绍和父子组件间传值
  • 广东省省考备考(第十五天5.20)—言语(第六节课)
  • 演员辛柏青发讣告:妻子朱媛媛患癌去世
  • 外交部:中方支持俄乌直接对话谈判,支持政治解决危机
  • 林园:茅台一直是稀缺资源,股东比较有信仰,依旧看好白酒市场
  • 俄美元首通话超2小时,普京称愿与乌方共同起草和平备忘录
  • 加快推进科技服务业高质量发展,九部门联合发文
  • 浙江一家长称小学老师打孩子还威胁要从3楼扔下,当地警方已立案