繁体   English   中英

在 pom 中使用 maven-install-plugin 将外部库导入本地存储库

[英]Import external lib into local repository using maven-install-plugin in pom

我的问题是我想将一个外部库自动导入到用户的本地 maven 存储库中。

这是我使用 maven-install-plugin 使其工作的方法:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.5.2</version>
            <executions>
                <execution>
                    <id>install-external</id>
                    <phase>clean</phase>
                    <configuration>
                        <file>${basedir}/myjar.jar>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>mygroupid</groupId>
                        <artifactId>myartefactid</artifactId>
                        <version>myversion</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

但是在 pom 中,为了让它工作,我必须执行两个不同的命令:

mvn clean 
mvn install

如果我只是在跑步

 mvn clean install

在安装阶段无法解决我的依赖关系(除非我单独进行了 mvn clean 并且我当然没有清理本地存储库)。 只有当我单独运行“mvn clean”时,Clean 似乎才会调用插件。

我想要的是在运行“mvn install”时自动导入我的依赖项。 或者至少在单个命令中运行“mvn clean install”时。

您的解决方案不能仅使用一个 Maven 命令。

如果您使用mvn something启动 Maven,首先会解析依赖项,因此您在构建期间通过插件安装的任何内容都不会被找到。

正如 khmarbaise 已经说过的,解决方案是首先将工件放入存储库。 通常的解决方案是您的公司 Nexus/Artifactory(如果您进行认真的 Maven 开发,应该有一个)。

如果这真的不可能,您可以使用目录作为 Maven 存储库,如下所述: https : //stackoverflow.com/a/28762617/927493

这种类型的解决方案在多 pom 项目中运行良好:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
    [...]
    <modules>
        <module>dependency-installer</module>
        <module>need-dependency</module>
    </modules>
</project>

模块依赖安装程序有自己的pom并执行依赖的安装:

  <artifactId>dependency-installer</artifactId>

  <build>
    <plugins>
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <id>lib1</id>
                    <configuration>
                        <file>${project.basedir}/libs/lib1/lib1.jar</file>
                        <pomFile>${project.basedir}/libs/lib1/lib1.pom</pomFile>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>mylibGroupId</groupId>
                        <artifactId>mylibArtifactId</artifactId>
                        <version>mylibVersion</version>
                        <packaging>jar</packaging>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <phase>install</phase>                  
                </execution>
            </executions>
        </plugin>
    </plugins>
  </build>

然后模块需要依赖只依赖于库和依赖安装程序:

    </dependencies>
        <dependency>
            <groupId>lib group Id</groupId>
            <artifactId>dependency-installer</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>mylibGroupId</groupId>
            <artifactId>mylibArtifactId</artifactId>
            <version>mylibVersion</version>
        </dependency>               
    </dependencies>

Maven 将始终以正确的顺序执行构建。 因为您依赖于依赖安装程序,它将首先被清理和构建,并将依赖项放在本地 repo 上。 然后需要依赖的 maven 模块将运行,并将使用来自本地 repo 的新添加的 jar。

此解决方案比命令行版本好得多,因为它是标准 maven 构建的一部分。 不需要任何人在之前手动安装任何东西,并且在本地或 CI/CD 中运行良好。

在高级极端情况下,您甚至可以通过这种方式提供 lib 的修补版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM