程序员你的maven多模块项目如何对外输出为一个构件?

image.png

上图为常见的台式机,程序员,你看了有啥启发?

台式机生产线 我的maven代码工程 xxx
显示器 xxx-web
主机 xxx-app
键盘 xxx-domian
鼠标 xxx-infrastration
台式机 xxx-all.jar

虽然不能完全对应的上,我拿开源的dubbo描述一下我的问题。

dubbo开发者:
dubbo的开源项目采用maven多模块开发的,内部模块分的非常细。

充分利用了台式电脑的分模块设计思想。

image.png

dubbo使用者:
我只需要引入一个dubbo-all的依赖即可使用dubbo;

好比台式机的用户,我只需要一个可使用的台式机,按照使用手册来即可,内部的东西我不想知道;

只需要引入坐标:

 <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
   		<version>2.7.0</version>
      <optional>true</optional>
    </dependency>

背景

最近的业务开发工作碰到过一个类似的问题。

问题 回答
where are we?现状 公共组件程序员开发采用多模块开发一个组件,业务程序员希望只引用一个组件
where are we go?目的 多模块开发一个公共组件,业务项目只需要引入一个模块
how we go there?实现路径 maven-shade-plugin

实现路径

shade

shade提供了一个把你的maven多模块构件和构件的依赖打包为一个超级jar包的能力。

它绑定到了maven生命周期的package阶段,提供了唯一的mavn的goal指令shade:shade

它的系统运行环境要求是:

运行需求 说明
maven3 最低maven3
jdk7 最低jdk7
内存和磁盘 无最低空间需求

用法如下:

<project>
 <build>
  <!-- To define the plugin version in your parent POM -->
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
     <version>3.2.4</version>
    </plugin>
   </plugins>
  </pluginManagement>
  <!-- To use the plugin goals in your POM or parent POM -->
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version><configuration>
     <!-- put your configurations here -->
    </configuration>
    <executions>
     <execution>
      <phase>package</phase>
      <goals>
       <goal>shade</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>

常见配置属性:

ApacheLicenseResourceTransformer

防止证书重复

ApacheNoticeResourceTransformer

准备合并通知

AppendingTransformer

作为资源添加

ComponentsXmlResourceTransformer

聚合components.xml 从

DontIncludeResourceTransformer

排除资源文件

IncludeResourceTransformer

包含的资源文件

ManifestResourceTransformer

manifest的条目

ServicesResourceTransformer

合并meta-info/services 资源

XmlAppendingTransformer

添加xml内容作为一个xml资源

dubbo

主要看dubbo-all模块的配置:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-parent</artifactId>
        <version>${revision}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>dubbo</artifactId>
    <packaging>jar</packaging>
    <name>dubbo-all</name>
    <description>The all in one project of dubbo</description>
    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-config-api</artifactId>
            <version>${project.version}</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createSourcesJar>true</createSourcesJar>
                            <promoteTransitiveDependencies>false</promoteTransitiveDependencies>
                            <artifactSet>
                                <includes>
                                    <include>com.alibaba:hessian-lite</include>
																		<include>org.apache.dubbo:dubbo-config-api</include>
                                </includes>
                            </artifactSet>
                            <transformers>
                                <!-- dubbo-common beginning -->
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>
                                        META-INF/dubbo/internal/org.apache.dubbo.common.compiler.Compiler
                                    </resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>
                                        META-INF/dubbo/internal/org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory
                                    </resource>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <artifact>org.apache.dubbo:dubbo</artifact>
                                    <excludes>
                                        <!-- These two line is optional, it can remove some warn log -->
                                        <exclude>com/**</exclude>
                                        <exclude>org/**</exclude>
                                        <!-- This one is required -->
                                        <exclude>META-INF/dubbo/**</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

为控制代码占用太多内容,上面贴的pom配置为删除了大量相同或者类似的节点。
下面拆解一下它的结构:

核心节点 说明
dependency 直接依赖,即包含的当前工程中的模块
plugin shade

shade的核心配置

配置 说明(见名知意,先猜测)
phase 挂接在maven的生命周期的package阶段
goal 提供唯一的goal指令 shade
createSourcesJar 是否创建源码到jar包中,方便ide直接查看到源码
promoteTransitiveDependencies 是否打包间接依赖
artifactSet-includes-include 包含的子模块或者排除的子模块
transformers-transformer-resource 转换器配置
excludes>-filter 过滤器中排出某些文件

具体看上面的代码。

dubbo-all.png

实际项目

参考dubbo,也是添加shade插件,目的是只把多模块的class和resource统一到一个jar中统一使用。

公司保密原因,不贴出来了。

小结

如果看完之后你只能记住一句话:

maven多模块开发可以使用shade插件对使用方输出一个构件

maven-shade-plugin.png

原创不易,关注诚可贵,转发价更高!转载请注明出处,让我们互通有无,共同进步,欢迎沟通交流。

posted @ 2020-09-05 01:26  李福春  阅读(476)  评论(0编辑  收藏  举报