如何配置POM以在FlexFX项目中签名发布Maven Archetype而非模板项目
配置Maven Archetype模块实现仅发布生成的Archetype
我之前也碰到过类似的场景,核心思路就是要明确区分「模板项目」和「最终要发布的Archetype产物」的构建行为,通过POM配置精准控制哪些模块需要部署、哪些只参与本地构建。下面是具体的配置方案:
1. 顶层archetypes模块的POM配置
顶层模块作为聚合父POM(packaging为pom),只负责管理子模块和统一插件版本,本身不需要发布到仓库。可以直接在这里配置跳过自身的部署行为:
<project> <groupId>com.your.flexfx</groupId> <artifactId>flexfx-archetypes</artifactId> <packaging>pom</packaging> <!-- 继承FlexFX主项目的版本、依赖管理等 --> <parent> <groupId>com.your.flexfx</groupId> <artifactId>flexfx-parent</artifactId> <version>${project.version}</version> </parent> <modules> <module>multi-module</module> </modules> <build> <pluginManagement> <plugins> <!-- 统一管理archetype插件版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <version>3.2.1</version> </plugin> <!-- 顶层模块跳过部署 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>3.1.0</version> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
2. 子模块(比如multi-module)的POM配置
这个模块是生成Archetype的核心载体,它的packaging依然是pom,主要负责从模板项目(parent、integration-test等)生成Archetype,并控制只发布生成的产物。
2.1 配置Archetype生成插件
绑定archetype:create-from-project目标到package阶段,构建时自动从模板项目生成Archetype,并排除不必要的文件:
<project> <parent> <groupId>com.your.flexfx</groupId> <artifactId>flexfx-archetypes</artifactId> <version>${project.version}</version> </parent> <artifactId>flexfx-archetype-multi-module</artifactId> <packaging>pom</packaging> <modules> <!-- 声明模板项目的子模块,用于本地验证模板结构 --> <module>parent</module> <module>integration-test</module> </modules> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <executions> <execution> <id>generate-archetype</id> <phase>package</phase> <goals> <goal>create-from-project</goal> </goals> <configuration> <sourceDirectory>${project.basedir}</sourceDirectory> <!-- 排除IDE配置、临时文件等不需要加入Archetype的内容 --> <excludes> <exclude>**/.idea/**</exclude> <exclude>**/*.iml</exclude> <exclude>target/**</exclude> </excludes> <!-- 指定生成的Archetype元数据 --> <archetypeGroupId>com.your.flexfx</archetypeGroupId> <archetypeArtifactId>flexfx-archetype-multi-module</archetypeArtifactId> <archetypeVersion>${project.version}</archetypeVersion> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
2.2 配置仅发布Archetype产物
生成的Archetype会存放在target/generated-sources/archetype目录,我们需要让Maven只部署这个目录下的项目,而原模板项目本身跳过部署。这里推荐使用maven-invoker-plugin来调用生成的Archetype项目的deploy命令:
<!-- 在multi-module的build/plugins下添加 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-invoker-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <id>deploy-generated-archetype</id> <phase>deploy</phase> <goals> <goal>run</goal> </goals> <configuration> <pomFile>target/generated-sources/archetype/pom.xml</pomFile> <goals> <goal>deploy</goal> </goals> <!-- 传递当前项目的版本、仓库配置等参数 --> <properties> <project.version>${project.version}</project.version> <distributionManagement.repository.id>${distributionManagement.repository.id}</distributionManagement.repository.id> <distributionManagement.repository.url>${distributionManagement.repository.url}</distributionManagement.repository.url> </properties> </configuration> </execution> </executions> </plugin> <!-- 让multi-module本身和模板子模块跳过部署 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin>
3. 签名配置注意事项
如果需要对Archetype进行签名(比如用maven-gpg-plugin),确保签名逻辑只作用于生成的Archetype项目:
- 可以在生成的Archetype项目的POM(模板里的
pom.xml)中添加签名插件配置; - 或者在
maven-invoker-plugin的配置中传递签名相关参数,让生成的Archetype在deploy时执行签名。
4. 验证效果
执行mvn clean deploy后,你会发现:
- 顶层
archetypes模块、multi-module模板模块以及它的子模块(parent、integration-test)都不会被部署到仓库; - 只有
target/generated-sources/archetype目录下生成的Archetype会被签名并发布。
内容的提问来源于stack exchange,提问作者D-Dᴙum
相关产品推荐
相关产品推荐

