ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Maven插件汇总(二):central-publishing、spotless-maven-plugin、maven-gpg-plugin、

Maven插件汇总(二):central-publishing、spotless-maven-plugin、maven-gpg-plugin、 多年前写过一篇高效使用Maven插件这7年也历经过多个不同公司和项目接触及使用过其他十几个插件能在公司线上业务里使用的插件肯定有学习的必要和价值。汇总记录。central-publishing-maven-plugin将项目发布到Maven Central仓库需在Maven Central创建一个账号。注册成功后生成用户Token并将其添加到settings.xml文件中。serversserveridcentral/idusernameadmin/usernamepasswordroot/password/server/servers在发布组件之前必须选择一个命名空间groupId可使用反向域名的方式来作为命名空间如com.example。下载并安装GPG然后生成密钥对gpg --gen-key gpg--keyserverkeyserver.ubuntu.com --send-keys公钥pom.xml文件配置plugingroupIdorg.sonatype.central/groupIdartifactIdcentral-publishing-maven-plugin/artifactIdversion0.9.0/versionextensionstrue/extensionsconfigurationpublishingServerIdcentral/publishingServerId/configuration/plugin执行以下命令mvn clean deploy -P release。发布成功后登录[Maven Central]并确认发布。spotless-maven-plugin一个支持多种编程语言的代码格式化工具广泛应用于GitHub上的开源项目。可通过Maven和Gradle插件形式集成帮助开发者自动或手动格式化代码确保代码风格一致性。支持语言Java、Groovy、Kotlin、Scala、C/C、Python等并提供多种格式化方式如google-java-format、eclipse、prettier等。使用Spotless开发者可确保代码风格的一致性提高代码的可读性和维护性。plugingroupIdcom.diffplug.spotless/groupIdartifactIdspotless-maven-plugin/artifactIdversion${spotless.version}/versionconfigurationjavagoogleJavaFormat/eclipsefile${maven.multiModuleProjectDirectory}/resources/spotless_formatter.xml/file/eclipselicenseHeaderfile${maven.multiModuleProjectDirectory}/dev-support/license-header/file/licenseHeaderimportOrder/removeUnusedImports/formatAnnotations//java/configurationexecutionsexecutionphasevalidate/phaseconfigurationskiptrue/skip/configuration/execution/executions/plugin在resources目录下创建spotless_formatter.xml文件用于指定Eclipse代码格式化的设置。使用以下命令检查和格式化代码# 检查代码格式mvn spotless:check# 自动格式化代码mvn spotless:apply执行mvn spotless:check命令插件会自动扫描Java文件并检查格式是否符合规范。控制台会显示扫描文件数和违规数量。若未配置将报错如下[ERROR] No plugin found for prefix spotless in the current project and in the plugin groups [org.springframework.boot, com.google.cloud.tools, org.jacoco, org.pitest, org.cyclonedx, org.flywaydb, io.github.git-commit-id, org.jooq, org.jetbrains.kotlin, org.liquibase, org.graalvm.buildtools, org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/tssh/.m2/repository), central (https://repo.maven.apache.org/maven2)] - [Help 1]只要发现一处格式违规构建就会失败日志中能看到具体问题。执行mvn spotless:apply可自动修复所有违规。修复后对比远程分支的代码差异。通过Eclipse Formatter Profile可以定义更灵活的格式规则兼容多种IDE和格式化插件。spotless_formatter.xml文件示例profilesversion21profilekindCodeFormatterProfilenamebaeldung-styleversion21settingidorg.eclipse.jdt.core.formatter.tabulation.charvaluespace/settingidorg.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentationsvaluetrue/settingidorg.eclipse.jdt.core.formatter.indentation.sizevalue4/settingidorg.eclipse.jdt.core.formatter.enabling_tagvalueformatter:on/settingidorg.eclipse.jdt.core.formatter.disabling_tagvalueformatter:off//profile/profiles可参考Google官方示例文件。license-header文本文件示例/** * Copyright (c) AWS Ltd 2026. All rights reserved */使用Spotless进行代码格式化时会为所有代码文件添加这段头信息。注意在头文件后面留一个空行以确保licenseHeader与Java代码的package之间有足够的间隔以保持代码的美观性。Spotless还能执行静态分析并自动优化代码在指定代码风格后可添加以下扩展步骤importOrder/removeUnusedImports/formatAnnotations/每个步骤会处理特定规则检查文件是否包含正确的版权头和确保导入语句有序且无冗余强制类型注解与字段声明同行相比于手动执行spotless:check和spotless:apply还可将将这些目标绑定到特定Maven阶段。Maven阶段是构建生命周期的预定义节点按顺序执行任务。如绑定到Maven的compile阶段在执行mvn install时代码会自动进行格式化。executionsexecutiongoalsgoalcheck/goal/goalsphasepackage/phase/execution/executionsmaven-gpg-pluginMaven官方发布的插件使用GnuPG对项目的所有附加工件进行签名提供两个主要目标gpg:sign和gpg:sign-and-deploy-file。使用Maven GPG插件之前首先需要确保命令行下的gpg是可用的还需要事先配置默认密钥。plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-gpg-plugin/artifactIdversion${maven-gpg-plugin.version}/versionconfiguration!-- 阻止gpg使用pinentry程序 --gpgArgumentsarg--pinentry-mode/argargloopback/arg/gpgArguments/configurationexecutionsexecutionidsign-artifacts/idphaseverify/phasegoalsgoalsign/goal/goalsconfigurationkeyname${gpg.keyname}/keynamepassphraseServerId${gpg.keyname}/passphraseServerId/configuration/execution/executions/plugin签名并部署项目工件的命令mvn clean deploy -Dgpg.passphraseyourpassphrase未提供-Dgpg.passphrase参数mvn verify -Dgpg.passphrasethephrase运行时会要求输入密码。对于已经发布但没有被签名的文件可使用gpg:sign-and-deploy-file目标gpg:sign-and-deploy-file -DpomFiletarget/myapp-1.0.pom -Dfiletarget/myapp-1.0.jar -Durlhttp://oss.sonatype.org/service/local/staging/deploy/maven2/ -DrepositoryIdsonatype_oss此命令会签名指定的POM及相关文件并将其部署到Maven仓库中。在项目发布时进行GPG签名是必要的但对日常SNAPSHOT构件进行签名没有多大意义反而会比较耗时。可在settings.xml或POM中创建一个Profile仅在项目发布时运行GPG插件。将GPG插件与Maven发布插件结合使用时mvn release:perform -Darguments-Dgpg.passphrasethephrase发布插件分叉Maven和当前Maven会话的系统属性很遗憾不会自动传播到分叉的Maven会话。您可以将密码以明文或加密文本的形式放在本地settings.xml中而不是在命令行上指定密码。settingsserversserveridyour.keyname/idpassphraseclear or encrypted text/passphrase/server/serversprofilesprofileidmy_profile_id/idactivationactiveByDefaulttrue/activeByDefault/activationpropertiesgpg.keynameyour.keyname/gpg.keyname/properties/profileprofiles/settings由于Maven Release Plugin的一个已知Bugrelease:perform执行过程中签名可能会导致进程永久挂起。为避免这种情况需要为Maven Release Plugin提供mavenExecutorId配置。plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-release-plugin/artifactIdversion2.0/versionconfigurationtagBasehttp://192.168.1.103/app/tags//tagBasebranchBasehttps://192.168.1.103/app/branches/branchBase/configuration/pluginjacoco-maven-pluginplugingroupIdorg.jacoco/groupIdartifactIdjacoco-maven-plugin/artifactIdversion0.8.14/versionexecutionsexecutiongoalsgoalprepare-agent/goal/goals/executionexecutionidreport/idphaseprepare-package/phasegoalsgoalreport/goal/goals/execution/executions/plugingmavenplus-plugin!-- Groovy/Spock compiler --plugingroupIdorg.codehaus.gmavenplus/groupIdartifactIdgmavenplus-plugin/artifactIdversion/versionexecutionsexecutiongoalsgoaladdSources/goalgoaladdTestSources/goalgoalgenerateStubs/goalgoalcompile/goalgoalgenerateTestStubs/goalgoalcompileTests/goalgoalremoveStubs/goalgoalremoveTestStubs/goal/goals/execution/executions/pluginexec-maven-plugin!-- Exec: CDK app entry point --plugingroupIdorg.codehaus.mojo/groupIdartifactIdexec-maven-plugin/artifactIdversion${exec-maven-plugin.version}/versionconfigurationmainClass${main.class}/mainClass/configuration/pluginquarkus-maven-plugin!-- Quarkus Maven plugin --plugingroupId${quarkus.platform.group-id}/groupIdartifactIdquarkus-maven-plugin/artifactIdversion${quarkus.platform.version}/versionextensionstrue/extensionsexecutionsexecutiongoalsgoalbuild/goalgoalgenerate-code/goalgoalgenerate-code-tests/goal/goals/execution/executionsdependencies!-- Required workaround for Quarkus 3.31 with Testcontainers on classpath. Testcontainers bundles JUnit 4 classes that reference org.junit.rules.TestRule. Quarkus 3.31 uses JUnit Platform 6.x which removed JUnit 4 from its plugin realm, causing build-time augmentation to fail. Version-specific to Quarkus 3.38.x. --dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/version/dependency/dependencies/plugindocker-maven-pluginSpotify开源GitHub2.6K Star555 Fork、纯Java实现、Docker发布插件可惜项目已于22年3月21日归档已不再维护。dockerfile-maven同样也是Spotify开源GitHub2.7K Star474 Fork、JavaGroovy实现、Docker构建插件可惜项目已于22年3月21日归档已不再维护。cobertura-maven-pluginCobertura是一个优秀的开源测试覆盖率统计工具。pom.xml配置文件示例运行命令生成报告mvn cobertura:cobertura
返回列表