Programmer How does your maven multi-module project externally output as a component?

The picture above is a common desktop computer, programmers, what inspires you?

Desktop production line my maven code works xxx
monitor xxx-web
host xxx-app
keyboard xxx-domian
mouse xxx-infrastration
Desktop xxx-all.jar 

Although it can't completely correspond, I will describe my problem with the open source dubbo.

dubbo developers:
dubbo's open source project is developed with maven multi-module, and the internal modules are very finely divided.

Make full use of the sub-module design idea of ​​the desktop computer.

dubbo users:
I only need to introduce a dubbo-all dependency to use dubbo;

Just like a desktop user, I only need a usable desktop, just follow the user manual, I don't want to know what's inside;

Just introduce the coordinates:

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

background

I ran into a similar problem in my recent business development work.

question answer
where are we? Common component programmers use multiple modules to develop a component, and business programmers hope to reference only one component
where are we go? Purpose Multi-module development of a common component, business projects only need to introduce one module
how we go there? Implementation path maven-shade-plugin

Realize

shade

shade provides the ability to package your maven multi-module components and their dependencies into a super jar.

It is bound to the package phase of the maven life cycle and provides the only mavn goal instruction shade:shade

Its system operating environment requirements are:

Operational requirements illustrate
maven3 Minimum maven3
jdk7 Minimum jdk7
memory and disk No minimum space requirement

The usage is as follows:

<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>

Common configuration properties:

ApacheLicenseResourceTransformer

Prevent duplicate certificates

ApacheNoticeResourceTransformer

Prepare for merge notification

AppendingTransformer

Added as resource

ComponentsXmlResourceTransformer

polymerization components.xml From

DontIncludeResourceTransformer

Exclude resource files

IncludeResourceTransformer

Included resource files

ManifestResourceTransformer

manifest 's entry

ServicesResourceTransformer

merge meta-info/services resource

XmlAppendingTransformer

Add to xml content as a xml resource



dubbo

Mainly look at the configuration of the dubbo-all module:

<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>

In order to control the code to take up too much content, the above posted pom Configured to delete a large number of identical or similar nodes.
Let's break down its structure:





| core node | illustrate |
| --- | --- |
| dependency | Direct dependencies, i.e. the included modules in the current project |
| plugin | shade |




shade core configuration




| configure | Explanation (see name and meaning, guess first) |
| --- | --- |
| <phase>package</phase> | hooked on maven of life cycle package stage |
| <goals><goal>shade</goal></goals> | provide the only goal instruction shade |
| <createSourcesJar>true</createSourcesJar> | Whether to create source code to jar In the bag, convenient ide View directly to the source code |
| <promoteTransitiveDependencies>false</promoteTransitiveDependencies> | Whether to package indirect dependencies |
| <artifactSet><includes><include> | Included submodules or excluded submodules |
|  <transformers><transformer><resource> | Converter configuration |
|  </excludes></filter> | Filter out certain files |




See the code above for details.

![dubbo-all.png](/img/bVbMrxj)

## actual project


refer to dubbo,also add shade Plugin, the purpose is to only put the multi-module class and resource unify into one jar used uniformly.

For reasons of confidentiality of the company, it will not be posted.




# summary


If you can only remember one sentence after reading it:

**maven Multi-module development can use shade The plugin outputs a widget to the consumer**. 

![maven-shade-plugin.png](/img/bVbMrxk)



> Originality is not easy, attention is sincere and valuable, and the forwarding price is higher! Please indicate the source when reprinting, let us communicate with each other and make progress together. Welcome to communicate.

Tags: Java

Posted by Sander on Wed, 18 May 2022 19:03:20 +0300