简体   繁体   English

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

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

My problem is that I want to have an external lib imported into the local maven repository of the user automatically.我的问题是我想将一个外部库自动导入到用户的本地 maven 存储库中。

Here is how I get it working using maven-install-plugin:这是我使用 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>

But with this in pom, in order to have it working I have to execute in two different commands:但是在 pom 中,为了让它工作,我必须执行两个不同的命令:

mvn clean 
mvn install

If I am just runing如果我只是在跑步

 mvn clean install

It's failling not resolving my dependency during the install phase (except if I have done a mvn clean alone and I havn't clean the local repository of course).在安装阶段无法解决我的依赖关系(除非我单独进行了 mvn clean 并且我当然没有清理本地存储库)。 Clean seems to call the pluging only if I run 'mvn clean' alone.只有当我单独运行“mvn clean”时,Clean 似乎才会调用插件。

What I would like is to have my dependency automatically imported when runing 'mvn install'.我想要的是在运行“mvn install”时自动导入我的依赖项。 Or at least while runing 'mvn clean install' in a single command.或者至少在单个命令中运行“mvn clean install”时。

Your solution cannot work with just one Maven command.您的解决方案不能仅使用一个 Maven 命令。

If you start Maven with mvn something , the dependencies are resolved first , so anything you install by a plugin during the build won't be found.如果您使用mvn something启动 Maven,首先会解析依赖项,因此您在构建期间通过插件安装的任何内容都不会被找到。

The solution, as khmarbaise already said, is to put the artifact into a repository first.正如 khmarbaise 已经说过的,解决方案是首先将工件放入存储库。 The usual solution for that is your company Nexus/Artifactory (if you do serious Maven depevelopment, there should be one).通常的解决方案是您的公司 Nexus/Artifactory(如果您进行认真的 Maven 开发,应该有一个)。

If this is really not possible, you can use a directory as Maven repository as described here: https://stackoverflow.com/a/28762617/927493如果这真的不可能,您可以使用目录作为 Maven 存储库,如下所述: https : //stackoverflow.com/a/28762617/927493

This type of solution work well in a multiple pom project:这种类型的解决方案在多 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>

The module dependency-installer has its own pom and perform the installation of the dependency:模块依赖安装程序有自己的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>

Then the module need-dependency just depend on the lib and the dependencies-installer:然后模块需要依赖只依赖于库和依赖安装程序:

    </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 will always execute build in correct order. Maven 将始终以正确的顺序执行构建。 Because you depends on dependency-installer it will be cleaned and built first and will put the dependency on the local repo.因为您依赖于依赖安装程序,它将首先被清理和构建,并将依赖项放在本地 repo 上。 Then the maven module needing the dependency will run and will use the freshly added jar from the local repo.然后需要依赖的 maven 模块将运行,并将使用来自本地 repo 的新添加的 jar。

This solution is much better than the command line version because it is part of a standard maven build.此解决方案比命令行版本好得多,因为它是标准 maven 构建的一部分。 Doesn't require anybody to install anything manually before and work perfectly well locally or in CI/CD.不需要任何人在之前手动安装任何东西,并且在本地或 CI/CD 中运行良好。

In advanced corner case you can even provide a patched version of a lib this way.在高级极端情况下,您甚至可以通过这种方式提供 lib 的修补版本。

暂无
暂无

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

相关问题 如何使用maven-install-plugin将外部库(jar)+源(zip)添加到pom.xml? - how to add external library (jar) + sources (zip) to pom.xml using maven-install-plugin? maven-install-plugin如何与pom一起配置以一次安装一些jar - how maven-install-plugin work with pom configure to install some jars one time mvn安装本地jar错误:无法执行目标org.apache.maven.plugins:maven-install-plugin…(是目录)-&gt; [帮助1] - Error with mvn install local jar: Failed to execute goal org.apache.maven.plugins:maven-install-plugin…(Is a directory) -> [Help 1] 插件 org.apache.maven.plugins:maven-install-plugin:2.4 或其依赖项之一无法解析 - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved 在Maven本地存储库中安装的外部jar上无法解析类导入 - class import not resolved on external jars installed in maven local repository 无法执行目标...maven-install-plugin...无法安装工件...(拒绝访问) - Failed to execute goal ... maven-install-plugin ... Failed to install artifact ... (Access is denied) 无法读取工件 org.apache.maven.plugins:maven-install-plugin:maven-plugin:2.4:runtime 的生命周期映射元数据 - Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins:maven-install-plugin:maven-plugin:2.4:runtime 将Maven本地.jar导入为lib - Import Maven local .jar as a lib 我可以将没有pom文件的zip文件安装到Maven本地存储库吗 - Can I install a zip file without pom file to maven local repository Maven:无法检索pluginorg.apache.maven.plugins:maven-install-plugin:2.4或无法解决其依赖项之一 - Maven: Failed to retrieve pluginorg.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM