简体   繁体   English

在解决依赖关系之前,使用Maven安装第三方依赖关系

[英]Install a 3rd-party dependency with maven before dependencies are resolved

I would like to deploy a maven project that depends on a 3rd-party jar, located in a "lib/" directory that is distributed with my project source. 我想部署一个依赖于第三方jar的maven项目,该jar位于与项目源一起分发的“ lib /”目录中。

The usual way to do this (as explained in other answers ) is to have the user install the jar into a local maven repository before building the project, by typing a command such as mvn install:install-file at the shell. 通常的方式做到这一点(如解释 其他 的答案 )是让用户安装jar到本地Maven仓库建设项目,通过键入命令,如前mvn install:install-file在shell。

This manual solution won't do for deployment, however (because requiring users to manually install dependencies is so 1998... ). 但是,此手动解决方案不适用于部署(因为要求用户手动安装依赖项为1998 ... )。 So I thought I'd specify an install-file goal in my pom.xml : 所以我想我应该在pom.xml指定一个install-file目标:

<dependency>
  <groupId>edu.my.id</groupId>
  <artifactId>myartifact</artifactId>
  <version>1.0</version>
</dependency>

... ...

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
  <execution>
    <id>install-jar-lib</id>
    <phase>validate</phase>
    <goals>
      <goal>install-file</goal>
    </goals>
    <configuration>
      <groupId>edu.my.id</groupId>
      <artifactId>myartifact</artifactId>
      <version>1.0</version>
      <packaging>jar</packaging>
      <file>${project.basedir}/lib/myartifact.jar</file>
      <generatePom>true</generatePom>
    </configuration>
  </execution>
</executions>

The problem is that maven attempts to resolve dependencies before install-file runs, even when I specify it to run as early as the validate phase. 问题在于,即使我将其指定为早于validate阶段运行,maven也会尝试在install-file运行之前解决依赖关系。 It thus complains that it cannot find the dependency. 因此,它抱怨无法找到依赖关系。

This question has been asked before , and a few unpleasantly complicated solutions have been offered such as these: 之前已经 这个问题,并且提供了一些令人不愉快的复杂解决方案,例如:

  1. Run maven-install-plugin during the clean phase and require users to run mvn clean before building the artifact. clean阶段运行maven-install-plugin ,并要求用户在构建构件之前运行mvn clean
  2. Set up multiple modules, as described here . 设置多个模块,描述在这里
  3. Use <systemPath> to load the jar as a system library. 使用<systemPath>将jar加载为系统库。 (This doesn't meet my requirements, because I am using maven-dependency-plugin to copy dependency jars into the deployed application directory, which ignores system libraries.) (这不符合我的要求,因为我正在使用maven-dependency-plugin将依赖项jar复制到已部署的应用程序目录中,该目录忽略了系统库。)
  4. Write a custom plugin. 编写一个自定义插件。

None of these satisfy me. 这些都不满足我。 This seems like a routine task any build system encounters. 这似乎是任何构建系统遇到的日常任务。 There must be a simpler way to install dependencies with maven. 必须有更简单的方法来使用maven安装依赖项。

The solution that worked for me was to use both install-file and <systemPath> . 为我工作的解决方案是使用这两种 install-file <systemPath>

<dependency>
  <groupId>edu.my.id</groupId>
  <artifactId>myartifact</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/myartifact.jar</systemPath>
</dependency>

... ...

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
  <execution>
    <id>install-jar-lib</id>
    <phase>validate</phase>
    <goals>
      <goal>install-file</goal>
    </goals>
    <configuration>
      <groupId>edu.my.id</groupId>
      <artifactId>myartifact</artifactId>
      <version>1.0</version>
      <packaging>jar</packaging>
      <file>${project.basedir}/lib/myartifact.jar</file>
      <generatePom>true</generatePom>
    </configuration>
  </execution>
</executions>

This way, when Maven compiles the project, it resolves the 3rd-party dependency as a system library. 这样,当Maven编译项目时,它将第三方依赖性解析为系统库。 But it also installs the library into the local maven repository, so that maven-dependency-plugin 's copy-dependencies goal is able to find it. 但是,它还将库安装到本地maven存储库中,以便maven-dependency-plugincopy-dependencies目标能够找到它。

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

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