如何将Maven多模块项目作为单个构件部署到Nexus?
Maven多模块项目合并构件部署到Nexus的配置方案
问题背景
现有Maven多模块项目结构:
- myService-parent | - myService | - myService-openApiDocs | - myService-eventsDefinition
期望部署到Nexus后,所有产物合并到myService构件下,结构如下:
Maven-repo |- myService | | - <version> | | - maven-metadata.xml | | - <my service artifact> | | - myService.pom | | - myService.jar | | - myService-openApiDocs.zip | | - myService-eventsDefinition.json
但实际部署后,每个模块会生成独立的构件,不符合预期。
配置方案
核心思路:让父模块和附属子模块不单独部署,将myService-openApiDocs、myService-eventsDefinition的产物作为myService的附加构件(带classifier)一起部署。
1. 父模块myService-parent配置
将父模块设为pom类型,并禁用其部署:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.myCompany</groupId> <artifactId>myService-parent</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>myService</module> <module>myService-openApiDocs</module> <module>myService-eventsDefinition</module> </modules> <build> <plugins> <!-- 父模块不部署到仓库 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
2. 主模块myService配置
引入附属模块依赖,复制其产物并作为附加构件绑定到主构件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.myCompany</groupId> <artifactId>myService-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>myService</artifactId> <packaging>jar</packaging> <dependencies> <!-- 依赖附属模块的产物 --> <dependency> <groupId>com.myCompany</groupId> <artifactId>myService-openApiDocs</artifactId> <version>${project.version}</version> <type>zip</type> </dependency> <dependency> <groupId>com.myCompany</groupId> <artifactId>myService-eventsDefinition</artifactId> <version>${project.version}</version> <type>json</type> </dependency> </dependencies> <build> <plugins> <!-- 复制附属模块的构建产物到当前模块的target目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <id>copy-attachments</id> <phase>package</phase> <goals> <goal>copy</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>com.myCompany</groupId> <artifactId>myService-openApiDocs</artifactId> <version>${project.version}</version> <type>zip</type> <outputDirectory>${project.build.directory}</outputDirectory> <destFileName>myService-openApiDocs.zip</destFileName> </artifactItem> <artifactItem> <groupId>com.myCompany</groupId> <artifactId>myService-eventsDefinition</artifactId> <version>${project.version}</version> <type>json</type> <outputDirectory>${project.build.directory}</outputDirectory> <destFileName>myService-eventsDefinition.json</destFileName> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <!-- 将复制的文件作为附加构件,与主构件一起部署 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>attach-artifacts</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>${project.build.directory}/myService-openApiDocs.zip</file> <classifier>open-api-docs</classifier> <type>zip</type> </artifact> <artifact> <file>${project.build.directory}/myService-eventsDefinition.json</file> <classifier>event-definitions</classifier> <type>json</type> </artifact> </artifacts> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
3. 附属模块配置(禁用独立部署)
myService-openApiDocs模块
配置打包为zip,并禁用部署:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.myCompany</groupId> <artifactId>myService-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>myService-openApiDocs</artifactId> <packaging>zip</packaging> <build> <plugins> <!-- 禁止该模块单独部署 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <!-- 生成zip包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.6.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <descriptorRefs> <descriptorRef>bin</descriptorRef> </descriptorRefs> <finalName>myService-openApiDocs</finalName> <appendAssemblyId>false</appendAssemblyId> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
myService-eventsDefinition模块
配置生成json文件,并禁用部署:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>com.myCompany</groupId> <artifactId>myService-parent</artifactId> <version>1.0.0-SNAPSHOT</version> </parent> <artifactId>myService-eventsDefinition</artifactId> <packaging>json</packaging> <build> <plugins> <!-- 禁止该模块单独部署 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <!-- 复制json文件到target目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>copy-json</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>events-definition.json</include> </includes> <targetPath>.</targetPath> <filtering>false</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> <!-- 重命名为目标文件名 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>rename-json</id> <phase>package</phase> <goals> <goal>rename-artifact</goal> </goals> <configuration> <outputFile>${project.build.directory}/myService-eventsDefinition.json</outputFile> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
执行部署
在父模块根目录执行命令:
mvn clean deploy
执行完成后,Nexus中只会生成myService构件,包含主jar、pom,以及带open-api-docs、event-definitions分类器的附加产物,maven-metadata.xml也会自动生成对应条目。
内容的提问来源于stack exchange,提问作者user19451321
相关产品推荐
相关产品推荐

