上图为常见的台式机,程序员,你看了有啥启发?
台式机生产线 | 我的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>package</phase> | 挂接在maven的生命周期的package阶段 || <goals><goal>shade</goal></goals> | 提供惟一的goal指令 shade || <createSourcesJar>true</createSourcesJar> | 是否创立源码到jar包中,不便ide间接查看到源码 || <promoteTransitiveDependencies>false</promoteTransitiveDependencies> | 是否打包间接依赖 || <artifactSet><includes><include> | 蕴含的子模块或者排除的子模块 || <transformers><transformer><resource> | 转换器配置 || </excludes></filter> | 过滤器中排出某些文件 |具体看下面的代码。## 理论我的项目参考dubbo,也是增加shade插件,目标是只把多模块的class和resource对立到一个jar中对立应用。公司窃密起因,不贴出来了。# 小结如果看完之后你只能记住一句话:**maven多模块开发能够应用shade插件对应用方输入一个构件**。> 原创不易,关注诚可贵,转发价更高!转载请注明出处,让咱们互通有无,共同进步,欢送沟通交流。