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

1次阅读

共计 4250 个字符,预计需要花费 11 分钟才能阅读完成。

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

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

尽管不能齐全对应的上,我拿开源的 dubbo 形容一下我的问题。

dubbo 开发者:
dubbo 的开源我的项目采纳 maven 多模块开发的,外部模块分的十分细。

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

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,也是增加 shade 插件,目标是只把多模块的 class 和 resource 对立到一个 jar 中对立应用。

公司窃密起因,不贴出来了。

小结

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

maven 多模块开发能够应用 shade 插件对应用方输入一个构件

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

正文完
 0