0

How can I add an external jar file as a dependency for a Maven project in IntelliJ IDEA? Because when I add it in the dependency list and try to compile with Maven, I got an error that that dependency couldn't be found.

3 Answers3

1

Ideally you should deploy the JAR to your repository using mvn deploy:deploy-file.

If that's not possible, you can set the dependencies scope to system and then include a systemPath in the dependency which gives that path to the jar. This is explained in POM Reference - dependencies and comes with a warning that any artifact that depends on the artifact with the system scope dependency will also expect to find the jar via the systemPath.

Nick Holt
  • 33,455
  • 4
  • 52
  • 58
1

You can either

  • define a system/local dependency like this:

    <dependency>
      <groupId>example</groupId>
      <artifactId>example</artifactId>
      <version>1.0.0</version>
      <scope>system</scope>
      <systemPath>lib/example-1.0.0.jar</systemPath>
    </dependency>
    

As Gimby pointed out, be aware that system dependencies are expected to 'just be there', so they will not be packaged and deployed with your artifact. See this question for reference.

  • install the artifact into your local repo:

    mvn install:install-file -Dfile=<path-to-file> \
                         -DgroupId=<myGroup> \
                         -DartifactId=<myArtifactId> \
                         -Dversion=<myVersion> \
                         -Dpackaging=<myPackaging> \
                         -DlocalRepositoryPath=<path-to-my-repo>
    
Community
  • 1
  • 1
kostja
  • 60,521
  • 48
  • 179
  • 224
  • 2
    Regarding the system scope advice: mandatory reading in http://stackoverflow.com/questions/3642023/having-a-3rd-party-jar-included-in-maven-shaded-jar-without-adding-it-to-local-r – Gimby Jan 08 '14 at 10:46
0

Step 1: Configure the maven-install-plugin with the goal install-file in your pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
            <phase>clean</phase>
            <configuration>
                <repositoryLayout>default</repositoryLayout>
                <groupId>com.amazonservices.mws</groupId>
                <artifactId>mws-client</artifactId>
                <version>1.0</version>
                <file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
            </configuration>
            <goals>
                <goal>install-file</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Make sure to edit the file path based on your actual file path (recommended is to place these external non-maven jars inside some folder, let's say lib, and place this lib folder inside your project so as to use project-specific relative path and avoid adding system specific absolute path.

If you have multiple external jars, just repeat the <execution> for other jars within the same maven-install-plugin.

Step 2: Once you have configured the maven-install-plugin as shown above in your pom.xml file, you have to use these jars in your pom.xml as usual:

    <dependency>
        <groupId>com.amazonservices.mws</groupId>
        <artifactId>mws-client</artifactId>
        <version>1.0</version>
    </dependency>

Note that the maven-install-plugin only copies your external jars to your local .m2 maven repository. That's it. It doesn't automatically include these jars as maven dependencies to your project.

It's a minor point, but sometimes easy to miss.

Amit
  • 33,847
  • 91
  • 226
  • 299