Include jar file in Maven project

6

How do I include a single .jar in a Maven project?

Note:

The jar I need to include has dependency on 3 other jar files.

The jars in question are from the project Brunette

Attempt # 1

I created a lib directory and put the jar there.

I added the dependency in the POM:

<dependency>
  <groupId>abc</groupId>
  <artifactId>abc</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/morena7.jar</systemPath>
</dependency> 

When building:

  

Some problems were encountered while building the effective model for   MyPackage: MyProject: jar: 1.0 'dependencies.dependency.systemPath' for   abc: abc: jar should not point at files within the project directory,   $ {basedir} /lib/morena7.jar will be unresolvable by dependent projects   @ line 30, column 25

At runtime, java.lang.NoClassDefFoundError occurs

    
asked by anonymous 02.07.2014 / 16:18

1 answer

7

There are several ways to resolve this.

Keeping your repository

The ideal is to install the jars in a repository of your company / house. For this you need a server with Artifactory or Nexus .

The advantage of having your own repository is that you can use it to manage the versions of your projects as well.

Another advantage is that it caches the central repository and its environment gets faster.

Dependencies with scope system

You can also point to the dependency path of type system . Consider the following example:

<dependency>
  <groupId>javax.sql</groupId>
  <artifactId>jdbc-stdext</artifactId>
  <version>2.0</version>
  <scope>system</scope>
  <systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>

These dependencies may also be within the project. Use the ${basedir} variable to indicate the project home directory.

The problem with this approach is that you need to keep the Jars in the repository, which is not very suitable.

See the documentation for more details.

Note: Maven has added a restriction on the use of libs within the project, as can be seen in the error edited in the question. You should then use a directory outside the project.

Installing dependencies in the local repository

Another solution is to use the install plugin to install the jars in a local repository. This can be done with the mvn install:install-file command and the appropriate parameters. See documentation for more details.

Example:

mvn install:install-file -Dfile=morena7.jar -DgroupId=sk.gnome \
    -DartifactId=morena -Dversion=7.0 -Dpackaging=jar

Please note that the above data was invented and will only work in the environment in which it was installed.

The problem with this approach is that the installation process needs to be repeated in each environment, that is, on each development machine and on the Continuous Integration server, if any.

    
02.07.2014 / 17:54