简体   繁体   中英

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.

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.

This manual solution won't do for deployment, however (because requiring users to manually install dependencies is so 1998... ). So I thought I'd specify an install-file goal in my pom.xml :

<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. 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.
  2. Set up multiple modules, as described here .
  3. Use <systemPath> to load the jar as a system library. (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.)
  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.

The solution that worked for me was to use both install-file and <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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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