“Maven多层次依赖传递的问题”

huangapple go评论57阅读模式
英文:

The problem of maven multi-layer dependency transfer

问题

多个内部包被合并并导出给客户,并包含依赖项;

举例来说,存在以下依赖树:

internal.lib1
    -> internal.lib2
        -> dependency-A
        -> dependency-B
        -> dependency-C
    -> internal.lib3
        -> dependency-D
        -> dependency-E
        -> dependency-F

只有一个JAR包交付给客户,即:internal.lib1.jar

  • 由于lib2/lib3不便部署到中央仓库。
  • 也不想每次交付三个单独的JAR包(internal.lib1.jar,internal.lib2.jar,internal.lib3.jar)。

因此,当我打包lib1时,我将lib2和lib3解压到lib1的类目录中。
并在lib1的POM中,将lib2/lib3设置为provided。
这样一来,我可以交付一个包含lib2和lib3的单一JAR(internal.lib1.jar)。

然而,lib2和lib3的依赖关系对客户不可见,因为lib2和lib3是provided。导致依赖项无法传递。

说到这里,我的核心需求是:根据上述依赖树,我只需要交付一个JAR(仅包含内部类),并传递外部依赖项。

最终,当客户引用我提供的库时,应该有如下依赖树:

customer-service
    ->internal.lib1(internal.lib2&internal.lib3)
        -> dependency-A
        -> dependency-B
        -> dependency-C
        -> dependency-D
        -> dependency-E
        -> dependency-F
英文:

Multiple internal packages are merged and exported to the customer and contain dependencies;

For example, there is the following dependency tree:

internal.lib1
    -> internal.lib2
        -> dependency-A
        -> dependency-B
        -> dependency-C
    -> internal.lib3
        -> dependency-D
        -> dependency-E
        -> dependency-F

Only one jar package is delivered to the customer, namely: internal.lib1.jar

  • Since lib2/lib3 is inconvenient to deploy to the central repository.
  • Nor want to deliver three separate jar packages each time(internal.lib1.jar,internal.lib2.jar,internal.lib3.jar).

So, when I packed lib1, I unpacked lib2 and lib3 into the classes directory of lib1.
And in the pom of lib1, set lib2/lib3 to provided.
At this point, I can deliver a single jar(internal.lib1.jar) that includes both lib2 and lib3.

However, the dependencies of lib2 and lib3 are not perceived by the client because lib2 and lib3 are provided. Causes dependencies not to be passed.

Having said that, my core needs are: Based on the dependency tree above, I need to deliver only one jar (containing only the internal classes) and pass external dependencies.

Finally, when a client references the lib I provide, it should have a dependency tree like this:

customer-service
    ->internal.lib1(internal.lib2&internal.lib3)
        -> dependency-A
        -> dependency-B
        -> dependency-C
        -> dependency-D
        -> dependency-E
        -> dependency-F

答案1

得分: 0

由于找不到合适的现有解决方案,我编写了一个 Maven 插件来执行此操作,该插件已发布到中央仓库:gpom-maven-plugin
使用方式如下:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <version>1.0</version>
    <groupId>com.example</groupId>
    <artifactId>internal.lib3</artifactId>

    <dependencies>
        <dependency>
            <version>1.0</version>
            <groupId>com.example</groupId>
            <artifactId>internal.lib2</artifactId>
        </dependency>
        <dependency>
            <version>1.0</version>
            <groupId>com.example</groupId>
            <artifactId>internal.lib3</artifactId>
        </dependency>
        <dependency>
            <version>1.0</version>
            <groupId>com.external</groupId>
            <artifactId>dependency-C</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <!--解压 lib1 和 lib2 到 lib3 的类目录-->
                    <execution>
                        <id>unpack-internal-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>com.example</includeGroupIds>
                            <excludes>**/META-INF/maven/**</excludes>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        </configuration>
                    </execution>
                    <!--将 lib3 的所有依赖项输出到指定文件(包括 lib1 和 lib2 的依赖列表,但不包括 lib1 和 lib2 本身)-->
                    <execution>
                        <id>list-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>list</goal>
                        </goals>
                        <configuration>
                            <excludeGroupIds>com.example</excludeGroupIds>
                            <outputFile>${project.build.directory}/dependencies</outputFile>
                            <silent>true</silent>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>io.github.kerwin612</groupId>
                <artifactId>gpom-maven-plugin</artifactId>
                <version>0.0.1</version>
                <executions>
                    <!--根据依赖文件生成新的 POM 文件-->
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <dependenciesPath>${project.build.directory}/dependencies</dependenciesPath>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>package-jar</id>
                        <goals>
                            <goal>jar</goal>
                        <goals>
                        <phase>package</phase>
                        <configuration>
                            <archive>
                                <!--使用特定的 POM 文件,而不是原始 POM 文件-->
                                <addMavenDescriptor>false</addMavenDescriptor>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

更多信息请查看:https://github.com/kerwin612/gpom-maven-plugin

英文:

Since I couldn't find a suitable existing solution, I wrote a maven plugin to do just that, which has been released to the central repository: gpom-maven-plugin
Use as follows:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <version>1.0</version>
    <groupId>com.example</groupId>
    <artifactId>internal.lib3</artifactId>

    <dependencies>
        <dependency>
            <version>1.0</version>
            <groupId>com.example</groupId>
            <artifactId>internal.lib2</artifactId>
        </dependency>
        <dependency>
            <version>1.0</version>
            <groupId>com.example</groupId>
            <artifactId>internal.lib3</artifactId>
        </dependency>
        <dependency>
            <version>1.0</version>
            <groupId>com.external</groupId>
            <artifactId>dependency-C</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <!--unpack lib1, and lib2 to lib3 classes-->
                    <execution>
                        <id>unpack-internal-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>com.example</includeGroupIds>
                            <excludes>**/META-INF/maven/**</excludes>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        </configuration>
                    </execution>
                    <!--output all dependencies of lib3 (including the dependency list of lib1 and lib2, but excluding lib1 and lib2 themselves) to the specified file-->
                    <execution>
                        <id>list-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>list</goal>
                        </goals>
                        <configuration>
                            <excludeGroupIds>com.example</excludeGroupIds>
                            <outputFile>${project.build.directory}/dependencies</outputFile>
                            <silent>true</silent>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>io.github.kerwin612</groupId>
                <artifactId>gpom-maven-plugin</artifactId>
                <version>0.0.1</version>
                <executions>
                    <!--generate a new pom file based on the dependency file-->
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <dependenciesPath>${project.build.directory}/dependencies</dependenciesPath>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>none</phase>
                    </execution>
                    <execution>
                        <id>package-jar</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <archive>
                                <!--use a specific pom file instead of the original pom file-->
                                <addMavenDescriptor>false</addMavenDescriptor>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

For more information please see: https://github.com/kerwin612/gpom-maven-plugin

huangapple
  • 本文由 发表于 2023年5月25日 16:38:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定