Installing 3rd party JARs in Maven project

When working in Maven projects, sometimes we need to use some libs that aren’t found in any Maven repository. A common example of this situation is Oracle JDBC Driver.

We have at least three ways to solve this kind of situation:

1) Using <scope /> and <systemPath />

You just need to have the jar file in your project (or in any well known path) and include the Maven dependency informing as a system dependency.

For example, considering you have the ojdbc6.jar in a folder called lib:

<dependency>
  <groupId>com.oracle</groupId>
  <artifactId>ojdbc</artifactId>
  <version>6</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
</dependency>

2) Installing JAR via command line

You install the jar into your repository through command line and just refer the dependency inside your pom.xml like any other one.

Example:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

3) Installing JAR via Maven plugin

I think this is not so common, but at least is an interesting option (actually, I used it just once). You can use the maven-install-plugin (the same used in command line) to install the jar during some Maven phase defined in the configuration. Besides that, you just need to put the normal dependency like any other one.

In the example below, we’re installing the odjbc6.jar during the clean phase:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-install-plugin</artifactId>
      <executions>
        <execution>
          <id>ojdbc6</id>
          <phase>clean</phase>
          <goals>
            <goal>install-file</goal>
          </goals>
          <configuration>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>6</version>
            <file>${basedir}/lib/ojdbc6.jar</file>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

References:

Advertisements

Let me your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s