Maven: How to include jars in Eclipse, which are not available in repository?
Asked Answered
D

5

13

I have copied the JARs into src\main\webapp\WEB-INF\lib.
I use eclipse. If I add the jars one-by-one to Project-> Java Build Path-> Add jars, then I do Project-> Maven-> Update Project Configuration, they are removed by Maven. And Eclipse shows errors, which contain " xxx cannot be resolved".

Env:
Eclipse Java EE IDE for Web Developers.
Version: Indigo Service Release 1
Build id: 20110916-0149

m2e - Maven Integration for Eclipse 1.0.100.20110804-1717

Note: I don't want to create my own Maven repository. It will be just used once.
How should I proceed ?

Dihedral answered 6/10, 2011 at 10:16 Comment(0)
S
18

Not all libraries can be found in a public Maven repository, for example your own libraries or proprietary libraries. Anyway first you can search the Maven Repository, maybe some of them are there.

In case they are really not there, and you don't want to install Nexus or Artifactory, you can choose one of these two approaches:

  • install the jars in your local repository cache, no need to create a special repository. See the instructions on mkyong.

  • another approach is using system dependencies, you just put a path to reach the Jar in the filesystem. It's a less recommended approach, but if you really want it will work. Here is the official documentation.

Sayles answered 6/10, 2011 at 10:24 Comment(5)
Actually , it is an very old project with too many JARs.I donot wnat to install JARs in my local reposity.Dihedral
You can use system dependencies then. I've added it to the answer.Sayles
I really donot like approach 2. First, one jar to one denpendcy;Second , too many JARs that I need manually add dependency one-by-one. So , I must adopt approach 1 to install Local repository one-by-one if have no better approaches. Thanks!Dihedral
As always, the solution to "too much manual labor" is "get the computer to do it". Write a script that updates your config file with the list of .jar files. ;-)Obregon
Chris Nava , How about one dependency coordination , groupId , artifactId and version?Dihedral
F
3

Add this an a Systems Dependency.


In case you want to add this (this uses jquantlib as example) to the maven local repo use:

mvn install:install-file -Dfile=./jquantlib-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib -Dversion=0.2.4 -Dpackaging=jar

In order to do this I use a batch file

@echo off

cd lib

CMD /C "mvn install:install-file -Dfile=./jquantlib-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib -Dversion=0.2.4 -Dpackaging=jar"
CMD /C "mvn install:install-file -Dfile=./jquantlib-helpers-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib-helpers -Dversion=0.2.4 -Dpackaging=jar"
CMD /C "mvn install:install-file -Dfile=./jquantlib-samples-0.2.4-ubber.jar -DgroupId=org.jquantlib -DartifactId=jquantlib-ubber -Dversion=0.2.4 -Dpackaging=jar"
CMD /C "mvn install:install-file -Dfile=./jquantlib-samples-0.2.4.jar -DgroupId=org.jquantlib -DartifactId=jquantlib-samples -Dversion=0.2.4 -Dpackaging=jar"

CMD /C "mvn install:install-file -Dfile=./ta-lib-0.4.0.jar -DgroupId=com.tictactec -DartifactId=ta-lib -Dversion=0.4.0 -Dpackaging=jar"

You can use a similar script file on other systems.

Frijol answered 3/12, 2011 at 8:16 Comment(0)
O
0

I believe the system dependency approach shouldn't be used unless you don't have any other choice, and that's because you're loosing the whole 'build portability thing' here. Of course you can store your jars in your source control system together with your project's source files, but I don't think its a good approach neither...

Using only install:install-file is not good enough - this would indeed deploy the jars in the proper format into your local repository, but what happens when you'll move to another computer and start to build your project there? You will need to make this once more.

So, If you don't want to install nexus/artifactory (which is the best solution, I believe), you probably should create an another repository (just in a file system on some of your servers), and deploy the jars there (you can use mvn install:install-file as was suggested here, and then just copy the whole tree). Now you can configure apache web server and access the directory with all your jars via http. I don't believe its better then nexus/artifactory approach, but it can be a little be easier to do if you're familiar with apache web server. In order to get your maven aware about this new repository you'll need to edit the %MAVEN_HOME%\conf\settings.xml file

Ornament answered 3/12, 2011 at 8:41 Comment(0)
T
0

There are atleast three approaches in which 3rd party JARs can be added to Maven projects.

  1. Install manually using mvn install command
  2. Adding the location of jar file in pom dependency with the the following tag system
  3. Creating a 'dummy' maven repository pointing to jar location.

While approach 1 and 2 has been suggested above, I will focus on third approach which I find more cleaner and does not require any mvn command and works out of box from any IDE.

Step 1: Add the location of local 'dummy' repository in pom.xml

<repositories>
    <repository>
        <id>repo</id>
        <name>repo</name>
        <url>file:${project.basedir}/src/main/resources/lib</url>
    </repository>
</repositories>

Here the 'dummy' repository location is the 'lib' folder of my project directory

Step 2 : Add the jar dependency into your pom.xml

    <dependency>
        <groupId>com.cloudera.impala</groupId>
        <artifactId>impala-frontend</artifactId>
        <version>0.1-SNAPSHOT</version>
    </dependency>

choose any groupId but make sure that artifactId and version is of the format <artifactId>-<version>.jar ( Name of 3rd party jar)

Step 3 : Create the folder structure as per the groupId,artifactId and version mentioned in the Step 2 in your local 'dummy' repository. So in this case the folder struction would be /src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/

Place your jar in the version folder and build your project. You will get the following output which treats your 'dummy' repository to be the provider of your 3rd party jar.

[INFO] Downloading from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/maven-metadata.xml
[INFO] Downloading from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.pom
[WARNING] The POM for com.cloudera.impala:impala-frontend:jar:0.1-SNAPSHOT is missing, no dependency information available
[INFO] Downloading from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar
[WARNING] Could not validate integrity of download from file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar: Checksum validation failed, no checksums available
[WARNING] Checksum validation failed, no checksums available from repo for file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar
[INFO] Downloaded from repo: file:C:\Users\skumar\eclipse-workspace\chdQueryBuilder/src/main/resources/lib/com/cloudera/impala/impala-frontend/0.1-SNAPSHOT/impala-frontend-0.1-SNAPSHOT.jar (7.0 MB at 79 MB/s)
[INFO] 

Toole answered 3/5, 2020 at 20:59 Comment(0)
D
-1

To add external JAR files not in the local repository simply right click on your main source folder and from the build path menu select: "configure build path", then navigate to the libraries tab and click "add external JAR files". next, locate the JAR (or zip) file you would like to add as a library and click ok.

Congratulations, you have now successfully added an external JAR (or zip) to your build path and you can now import any classes from that JAR file in your project without throwing an errorPicture How-To :) http://hostthenpost.org/uploads/541be8420657320c74489ff8d456ad08.png

Distracted answered 11/4, 2016 at 18:48 Comment(1)
That only works in the IDE. If you try to install the Maven project it fails.Marry

© 2022 - 2024 — McMap. All rights reserved.