Maven: best way of linking custom external JAR to my project?

326,726

Solution 1

I think you should use mvn install:install-file to populate your local repository with the library jars then you should change the scope from system to compile.

If you are starting with maven I suggest to use maven directly not IDE plugins as it adds an extra layer of complexity.

As for the error, do you put the required jars on your classpath? If you are using types from the library, you need to have access to it in the runtime as well. This has nothing to do with maven itself.

I don't understand why you want to put the library to source control - it is for sources code not binary jars.

Solution 2

You can create an In Project Repository, so you don't have to run mvn install:install-file every time you work on a new computer

<repository>
    <id>in-project</id>
    <name>In Project Repo</name>
    <url>file://${project.basedir}/libs</url>
</repository>

<dependency>
    <groupId>dropbox</groupId>
    <artifactId>dropbox-sdk</artifactId>
    <version>1.3.1</version>
</dependency>

/groupId/artifactId/version/artifactId-verion.jar

detail read this blog post

https://web.archive.org/web/20121026021311/charlie.cu.cc/2012/06/how-add-external-libraries-maven

Solution 3

This can be easily achieved by using the <scope> element nested inside <dependency> element.

For example:

 <dependencies>
   <dependency>
     <groupId>ldapjdk</groupId>
     <artifactId>ldapjdk</artifactId>
     <scope>system</scope>
     <version>1.0</version>
     <systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
   </dependency>
 </dependencies>

Reference: http://www.tutorialspoint.com/maven/maven_external_dependencies.htm

Solution 4

The Maven manual says to do this:

mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar

Solution 5

update We have since just installed our own Nexus server, much easier and cleaner.

At our company we had some jars that we some jars that were common but were not hosted in any maven repositories, nor did we want to have them in local storage. We created a very simple mvn (public) repo on Github (but you can host it on any server or locally):
note that this is only ideal for managing a few rarely chaning jar files

  1. Create repo on GitHub:
    https://github.com/<user_name>/mvn-repo/

  2. Add Repository in pom.xml
    (Make note that the full path raw file will be a bit different than the repo name)

    <repository>
        <id>project-common</id>
        <name>Project Common</name>
        <url>https://github.com/<user_name>/mvn-repo/raw/master/</url>
    </repository>
    
  3. Add dependency to host (Github or private server)
    a. All you need to know is that files are stored in the pattern mentioned by @glitch
    /groupId/artifactId/version/artifactId-version.jar
    b. On your host create the folders to match this pattern.
    i.e if you have a jar file named service-sdk-0.0.1.jar, create the folder service-sdk/service-sdk/0.0.1/ and place the jar file service-sdk-0.0.1.jar into it.
    c. Test it by trying to download the jar from a browser (in our case: https://github.com/<user_name>/mvn-repo/raw/master/service-sdk/service-sdk/0.0.1/service-sdk-0.0.1.jar

  4. Add dependency to your pom.xml file:

    <dependency>
        <groupId>service-sdk</groupId>
        <artifactId>service-sdk</artifactId>
        <version>0.0.1</version>
    </dependency>
    
  5. Enjoy

Share:
326,726

Related videos on Youtube

Alexandr Kurilin
Author by

Alexandr Kurilin

Updated on May 04, 2022

Comments

  • Alexandr Kurilin
    Alexandr Kurilin about 2 years

    It's my first couple of days learning Maven and I'm still struggling with the basics. I have an external .jar file (not available in the public repos) that I need to reference in my project and I'm trying to figure out what my best option is.

    It's a small scale project without a central repository for libraries, so it has to be either a local repository (somehow added to source control, don't know if it's supposed to work that way?) or the .jar needs to be stored on disk outside of any formal repository.

    1) What's my best option for adding the .jar file to my project's references with maven given that I want both the project and the library to be in source control?

    2) I still can't seem to have Eclipse see the dependency. I manually added it to the section of the pom, and it shows up fine in the Dependencies list in m2eclipse. mvn compile and mvn package both succeed, but running the program results in:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
            LibraryStuff cannot be resolved to a type
    

    This is after editing the POM as:

    <dependency>
      <groupId>stuff</groupId>
      <artifactId>library</artifactId>
      <version>1.0</version>
      <systemPath>${lib.location}/MyLibrary.jar</systemPath>
      <scope>system</scope>
    </dependency>
    

    Should I be executing mvn install:install-file even thought I already have the pom.xml edited as above?

    Thanks!

  • Doug T.
    Doug T. over 11 years
    for more on mvn install::install-file: mkyong.com/maven/…
  • Jignesh Gohel
    Jignesh Gohel about 10 years
    Using the above solution shown a warning while doing "mvn clean package" - The POM for project <dependency_name> is missing, no dependency information available.
  • Antonio Sesto
    Antonio Sesto over 9 years
    That's the best solution for those cases where you need to add one or just few jar files. Thanks.
  • Charlie Wu
    Charlie Wu over 9 years
    this command install the lib into your maven repo. The downside of this is if you try to work on a project in a different computer, you have to run this again.
  • mauryat
    mauryat over 9 years
    Or, you can add local dependencies directly as in stackoverflow.com/a/22300875/640378
  • 3xCh1_23
    3xCh1_23 over 7 years
    Thanks, it saved a day!
  • Loc Phan
    Loc Phan over 7 years
    I had to use file:///${project.basedir}/libs (3 forwarded slashes) instead of file://${project.basedir}/libs
  • Maxime T
    Maxime T about 7 years
    I've been lookin for this for years now. Thank you.
  • Isen Ng
    Isen Ng about 7 years
    Using mvn install::install-file on your local repository would mean that anyone who clones your source code would have to do this manual step as well. Otherwise, the build is broken out-of-the box
  • Isen Ng
    Isen Ng about 7 years
    If the jar being installed, isn't an maven compiled jar, you will also need to add a new pom file to define the metadata. To save yourself all of these manual trouble, I would recommend to use mvn install:install-file and then copy the entire directory structure from your local repository to your in-project repository.
  • Charlie Wu
    Charlie Wu about 7 years
    nice, that's most scalable solution I've found, thanks
  • dnang
    dnang over 6 years
    Beware that when you create folder structure /groupId/artifactId/version/artifactId-verion.jar, don't make the mistake of using pakage name as groupId; instead it should be divided in a folder tree. E.g. for the groupId org.json the path should look like org/json/2.0/json-2.0.jar, and not org.json/2.0/json-2.0.jar
  • cruxi
    cruxi over 6 years
    If I do, do other users who pull my project have to do the same in Eclipse?
  • osk
    osk over 6 years
    How do u know the versions of the jars and of the maven install plugin?
  • craftsmannadeem
    craftsmannadeem over 6 years
    versions of the jars are just made up, maven install plugin versions is the latest
  • Cardin
    Cardin over 5 years
    This method works very well when you only have the Eclipse Embedded Maven, and you lack the install plugin (offline system), so you can't run install goal on the depend-ed project. Ofc having an airgapped system and having all plugins but the install plugin, is quite a rare situation.
  • Ajay Kumar
    Ajay Kumar over 5 years
    Cleanest and easiest.
  • Prachi
    Prachi almost 5 years
    that won't add the jar to the war file though.
  • Dalc
    Dalc almost 5 years
    I was looking for a solution like this one, simply because when a new developer arrives, this simplify the installation procedure. When you don't have an artifactory server, its way easier to have the custom dependency in your project. Also when you use a CI, this solution avoid to manually install the jar on the CI server.
  • Oliver
    Oliver over 4 years
    2019 Original URL doesnt work, use this: web.archive.org/web/20121026021311/charlie.cu.cc/2012/06/…
  • Naveen Kumar
    Naveen Kumar over 4 years
    how to add local dependency path which is outside ${project.basedir} folder, like I want ${basedir}\src\lib\ldapjdk.jar path 1 level up in other folder
  • mithun_ghose
    mithun_ghose about 4 years
    How about multiple jars??
  • garg10may
    garg10may about 4 years
    I am getting error - "The POM for … is missing, no dependency information available” even though it exists in Maven Repository
  • Jonas_Hess
    Jonas_Hess almost 4 years
    Note that instead of constructing the libs directory structure manually, you can also do this: mvn install:install-file -Dfile=dropbox-sdk-1.3.1.jar -DgroupId=dropbox -DartifactId=dropbox-sdk -Dversion=1.3.1 -Dpackaging=jar -DlocalRepositoryPath=lib -DcreateChecksum=true This will create the structure for you and also create the checksum metadata to avoid the Maven warning.
  • Danyal Sandeelo
    Danyal Sandeelo over 2 years
    <configuration> <includeSystemScope>true</includeSystemScope> </configuration> this is the main thing i believe
  • fleed
    fleed over 2 years
    This worked for me, had a jar library that is not available in a repository but only locally within a lib folder. This setup resolved the issue I was having and new machines/developers avoid the install:install issue.
  • ka3ak
    ka3ak over 2 years
    I get could not resolve depedencies... for the corresponding jars It looks like the timing is wrong Does it really install the jars before it tries to resolve the dependencies? However I ran 'mvn clean package'