程序員你的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的核心配置

配置 說明(見名知意,先猜測)
package 掛接在maven的生命周期的package階段
shade 提供唯一的goal指令 shade
true 是否創建源碼到jar包中,方便ide直接查看到源碼
false 是否打包間接依賴
包含的子模塊或者排除的子模塊
轉換器配置
過濾器中排出某些文件

具體看上面的代碼。

dubbo-all.png

實際項目

參考dubbo,也是添加shade插件,目的是只把多模塊的class和resource統一到一個jar中統一使用。

公司保密原因,不貼出來了。

小結

如果看完之后你只能記住一句話:

maven多模塊開發可以使用shade插件對使用方輸出一個構件

maven-shade-plugin.png

原創不易,關注誠可貴,轉發價更高!轉載請注明出處,讓我們互通有無,共同進步,歡迎溝通交流。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM