龙空技术网

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

李福春 687

前言:

现在兄弟们对“apachecxfjar包合并”都比较注重,同学们都想要了解一些“apachecxfjar包合并”的相关内容。那么小编也在网上汇集了一些有关“apachecxfjar包合并””的相关内容,希望你们能喜欢,我们快快来学习一下吧!

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

台式机生产线我的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最低maven3jdk7最低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="; xmlns:xsi=";         xsi:schemaLocation=" ;>    <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直接依赖,即包含的当前工程中的模块pluginshade

shade的核心配置

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

具体看上面的代码。

实际项目

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

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

小结

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

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

我会持续分享Java软件编程知识和程序员发展职业之路!原创不易,关注诚可贵,转发价更高!转载请注明出处,让我们互通有无,共同进步,欢迎沟通交流。

标签: #apachecxfjar包合并