基于一种特殊情况: 需要开发通用代码,但底层依赖的jar有不同版本,使用一次maven 命令编译来同时生成多个fat jar。

测试代码结构:
在这里插入图片描述
log4j-v1/log4j-v2 有一个同名类被maintest引用。

maintest pom.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<project
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://maven.apache.org/POM/4.0.0"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>agent</artifactId>
        <groupId>org.as4</groupId>
        <version>0.1</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <groupId>multiVersiontest</groupId>
    <artifactId>maintest</artifactId>
    <version>0.1</version>
    <name>multiVersiontest</name>
    <description>multiVersiontest</description>
    <properties>
        <java.version>8</java.version>
        <commons-collections4.version>4.4</commons-collections4.version>
        <commons-io.version>2.11.0</commons-io.version>
        <commons-lang.version>2.6</commons-lang.version>
        <commons-lang3.version>3.12.0</commons-lang3.version>
        <commons-math3.version>3.6.1</commons-math3.version>
    </properties>
    <profiles>
        <profile>
            <id>log-v1</id>
            <properties>
                <profiles.active>log-v1</profiles.active>
            </properties>

            <activation>
                 <activeByDefault>false</activeByDefault>
                <property>
                    <name>all</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>3.2.1</version>
                        <executions>

                            <execution>
                                 <id>log4j-v1</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>

                                </goals>
                                <configuration>
                                <artifactSet>
                                    <excludes>
                                        <exclude>org.as4.learning:log4j-v2</exclude>
                                    </excludes>
                                </artifactSet>
                                    <transformers>
                                        <transformer
                                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <mainClass>org.as4.learning.multiversionTest.LogTestMain</mainClass>
                                        </transformer>
                                    </transformers>
                                    <outputFile>${project.build.outputDirectory}/../multiversion-log4j-v1.jar
                                    </outputFile>
                                </configuration>
                            </execution>
                        </executions>

                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>log-v2</id>
            <properties>
                <profiles.active>log-v2</profiles.active>
            </properties>

            <activation>
                 <property>
                    <name>all</name>
                    <value>true</value>
                </property>
                <activeByDefault>false</activeByDefault>
            </activation>
            <build>
                <plugins>
                 
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>3.2.1</version>
                        <executions>
                            <execution>
                                <id>log4j-v2</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <artifactSet>
                                        <excludes>
                                            <exclude>org.as4.learning:log4j-v1</exclude>
                                        </excludes>
                                    </artifactSet>
                                    <transformers>
                                        <transformer
                                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <mainClass>org.as4.learning.multiversionTest.LogTestMain</mainClass>
                                        </transformer>
                                    </transformers>
                                    <outputFile>${project.build.outputDirectory}/../multiversion-log4j-v2.jar
                                    </outputFile>

                                </configuration>
                            </execution>
                        </executions>

                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <dependencies>
        <dependency>
            <groupId>org.as4.learning</groupId>
            <artifactId>log4j-v1</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.as4.learning</groupId>
            <artifactId>log4j-v2</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>
</project>

mvn命令 : mvn clean package -P log-v1,log-v2

执行结果验证
在这里插入图片描述
成功!

更多推荐