-2

有模块 A、B 和 C。模块 A 是模块 B 和 C 的依赖项。模块 A 中构建的所有 jar-s 都将分发到 Maven Central,但只有在模块 B 和 C 中的集成测试通过后。

为了实现上述目标,我决定实现一个多模块项目,其中包含一个专门用于分发的模块 D。

由于分发模块 D 有自己的特定坐标,模块 A 的 jar-s 将被重命名。

我的想法是将模块 A 中的 jar-s 作为依赖项复制到模块 D 的目标目录,并根据模块 D 坐标进行重命名。这是模块 D pom:

<parent>
    <groupId>...</groupId>
    <artifactId>parent</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<groupId>...</groupId>
<artifactId>...</artifactId>
<packaging>pom</packaging>

...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>...</groupId>
                                <artifactId>...</artifactId>
                                <version>${project.version}</version>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>${project.artifactId}-${project.version}.jar</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>...</groupId>
                                <artifactId>...</artifactId>
                                <version>${project.version}</version>
                                <type>jar</type>
                                <classifier>sources</classifier>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>${project.artifactId}-${project.version}-sources.jar</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>...</groupId>
                                <artifactId>...</artifactId>
                                <version>${project.version}</version>
                                <type>jar</type>
                                <classifier>javadoc</classifier>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>${project.artifactId}-${project.version}-javadoc.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

jar-s 从模块 A 复制到模块的 D 目标目录,但安装阶段完成后,我的本地存储库中只更新模块的 D pom。在构建日志中也没有关于安装复制的 jar-s 的记录。

同时,模块A的jar-s安装成功。

我试图将模块 D 的打包更改为 jar,但之后只安装了带有二进制文件的 jar,并且没有安装带有源代码和 javadoc 的 jar-s。

目前找到以下解决方法:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <packaging>jar</packaging>
                        <javadoc>
                            ${project.build.directory}/${project.artifactId}-${project.version}-javadoc.jar
                        </javadoc>
                        <sources>
                            ${project.build.directory}/${project.artifactId}-${project.version}-sources.jar
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

还有其他不那么笨拙的解决方案吗?

谢谢。

4

2 回答 2

1

该方法过于复杂。

你应该怎么做:

  • 建个
  • 用 B 和 C 测试 A
  • 部署一个

根据您的方法,有不同的可能性来实现这一点。例如,您可以使用deployAtEndMaven 部署插件的属性。

于 2021-05-23T17:52:35.280 回答
0

事实证明,仅仅将所需的 jar-s 复制到目标是不够的,这些 jar-s 还需要使用 build-helper-maven-plugin附加。

所以,这里是父pom(为了与问题细节一致并且为了清楚起见,稍微简化了一点):

<?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      http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.internal</groupId>
<artifactId>parent</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>A</module>
    <module>B</module>
    <module>C</module>
    <module>D</module>
</modules>

</project>

这是分布式模块 D 的 pom(也有点简化):

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.example.internal</groupId>
    <artifactId>parent</artifactId>
    <version>0.1.0-SNAPSHOT</version>
</parent>

<groupId>com.example</groupId>
<artifactId>D</artifactId>
<packaging>pom</packaging>

<properties>
    <moduleToDistributeGroupId>com.example.internal</moduleToDistributeGroupId>
    <moduleToDistributeArtifactId>A</moduleToDistributeArtifactId>
    <sourcesClassifier>sources</sourcesClassifier>
    <javadocClassifier>javadoc</javadocClassifier>
    <binaryArtifactName>${project.artifactId}-${project.version}.jar</binaryArtifactName>
    <sourcesArtifactName>${project.artifactId}-${project.version}-${sourcesClassifier}.jar</sourcesArtifactName>
    <javadocArtifactName>${project.artifactId}-${project.version}-${javadocClassifier}.jar</javadocArtifactName>
</properties>

<dependencies>
    <dependency>
        <groupId>com.example.internal</groupId>
        <artifactId>A</artifactId>
    </dependency>
    <dependency>
        <groupId>com.example.internal</groupId>
        <artifactId>B</artifactId>
        <version>${project.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.example.internal</groupId>
        <artifactId>C</artifactId>
        <version>${project.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifact>
                        </artifact>
                        <artifactItems>
                            <artifactItem>
                                <groupId>${moduleToDistributeGroupId}</groupId>
                                <artifactId>${moduleToDistributeArtifactId}</artifactId>
                                <version>${project.version}</version>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>${binaryArtifactName}</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>${moduleToDistributeGroupId}</groupId>
                                <artifactId>${moduleToDistributeArtifactId}</artifactId>
                                <version>${project.version}</version>
                                <type>jar</type>
                                <classifier>${sourcesClassifier}</classifier>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>${sourcesArtifactName}</destFileName>
                            </artifactItem>
                            <artifactItem>
                                <groupId>${moduleToDistributeGroupId}</groupId>
                                <artifactId>${moduleToDistributeArtifactId}</artifactId>
                                <version>${project.version}</version>
                                <type>jar</type>
                                <classifier>${javadocClassifier}</classifier>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}</outputDirectory>
                                <destFileName>${javadocArtifactName}</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>attach-artifacts</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>${project.build.directory}/${binaryArtifactName}</file>
                                <type>jar</type>
                            </artifact>
                            <artifact>
                                <file>${project.build.directory}/${sourcesArtifactName}</file>
                                <classifier>${sourcesClassifier}</classifier>
                                <type>jar</type>
                            </artifact>
                            <artifact>
                                <file>${project.build.directory}/${javadocArtifactName}</file>
                                <classifier>${javadocClassifier}</classifier>
                                <type>jar</type>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--other plugins-->
    </plugins>
</build>

</project>

附加工件后,所有其他插件会自动识别它们,例如 gpg、安装、部署等。

于 2021-05-24T22:19:56.720 回答