Include jar file in Maven project

Asked

Viewed 5,016 times

6

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

Obs:

The jar I need to include has dependency on another 3 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 on 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 mePackage: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

Running java.lang.Noclassdeffounderror occurs

1 answer

7


There are several ways to solve this.

Keeping your repository

The ideal is to install the jars in a repository of your company/home. 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 the 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 can also be inside the project. Use the variable ${basedir} to indicate the base directory of the project.

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

See the documentation for more details.

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

Installing dependencies in the local repository

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

Example:

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

Note that the above data has been 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.

  • edited my question with the attempt I made following your tip.

  • @Nilsonuehara You must be using one of the newer versions of Maven. I just noticed that they added this restriction. However you can put the files in a folder outside the project and specify the absolute path. Another alternative is to install the jar in your local repository using any groupid and artifactId. I will update the response with this.

  • 1

    I have come to the conclusion that the best thing to do is to install the Nexus or even Artifactory, as it will facilitate the maintenance work.

  • @Nilsonuehara If you have this option, it’s really the best way!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.