31

I do not want to install a few jars into a Maven repository (both local/remote). In particular I have a few jar files located in

c:\work\projects\myapp\src\main\webapp\WEB-INF\lib\test.jar

c:\work\projects\myapp\src\main\webapp\WEB-INF\lib\test2.jar

How to include them into my project when open/edit with NetBeans?

JeanValjean
  • 17,172
  • 23
  • 113
  • 157
cometta
  • 35,071
  • 77
  • 215
  • 324
  • 5
    I wish I could destroy, burn, send to hell all these questions promoting using `system` scoped dependencies. I keep writing [against this practice](http://stackoverflow.com/questions/3642023/having-a-3rd-party-jar-included-in-maven-shaded-jar-without-adding-it-to-local-re/3643308#3643308) but sadly, it doesn't help... – Pascal Thivent Sep 22 '10 at 05:06

4 Answers4

67

Although it works to use the systemPath reference, it is better to create a local repository. And fortunately, it is easy to do.

Creating a local repository holding jars not available in a public repository

NOTE: I use Eclipse, so some of the instructions are specific to Eclipse. Most are easily generalizable.


Assumptions

  • The jar was created by Maven in another project with the following...

    <groupId>com.foo</groupId>
    <artifactId>test</artifactId>
    <version>0.1.1</version>
    <packaging>jar</packaging>
    

In Project (that wants to access the jars)

  • Create repo directory just off the project base directory
  • For each jar to be accessed locally...
    • add directories for each level of the groupID (ex. /repo/com/foo)
    • add jar name (aka artifactId) without the version (ex. /repo/com/foo/test)
    • add directory for the version of the jar (ex. /repo/com/foo/test/0.1.1)
    • put the jar in that directory (ex. /repo/com/foo/test/0.1.1/test-0.1.1.jar)

In pom.xml (for the project that wants to access the jars)

  • Define the local repository

    <repositories>
      <repository>
        <id>data-local</id>
        <name>data</name>
        <url>file://${project.basedir}/repo</url>
      </repository>
    </repositories>
    
  • Add the dependency on the local jar. From our example above, this would be...

    <dependency>
      <groupId>com.foo</groupId>
      <artifactId>test</artifactId>
      <version>0.1.1</version>
    </dependency>
    

Rebuild

  • Rt click pom.xml -> Run as -> Maven build
E L Rayle
  • 1,131
  • 2
  • 9
  • 11
  • What do you need to do if you only want to give access to the jars in a sub module? I followed the instructions, and added the repository definition to my sub module pom, but maven seems to ignore this repository when building. – Niel de Wet Sep 17 '15 at 06:44
  • Damn, my packaging was `pom` which was not letting it import. Making it `jar` followed by `mvn clean install -U` makes it available to add as maven ``. – prayagupa Sep 30 '16 at 23:19
  • This is the correct answer; it follows the Maven ideology, and works like a charm. – Jonathan E. Landrum Feb 13 '19 at 18:24
  • 2
    um, for Maven 3, there are a few more things that have to be done, like renaming jar to include version number, and adding a pom file to each of these directories. luckily someone already wrote about it here https://gist.github.com/timmolderez/92bea7cc90201cd3273a07cf21d119eb – hello_earth Oct 01 '19 at 09:43
  • this approach works with jar-with-dependencies, while the system approach does not. – Joe C Oct 19 '19 at 22:19
  • 1
    @hello_earth although with Maven 3 the jar needs the version number as suffix in its name, there is **no need** for additional pom(s)! – Reto Apr 16 '23 at 17:37
26

Have you considered adding those two JARs as system dependencies? e.g.,

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>sun.jdk</groupId>
      <artifactId>tools</artifactId>
      <version>1.5.0</version>
      <scope>system</scope>
      <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>
  </dependencies>
  ...
</project>

Just a word of note, this is NOT recommended and should be used very sparingly, if ever.

Eric
  • 6,563
  • 5
  • 42
  • 66
The Alchemist
  • 3,397
  • 21
  • 22
  • 1
    @Pascal Thivent: I up-voted your comment despite your statement that I should get a -10. :) I totally agree with you. I wrote my answer under the assumption that @cometta has his/her reasons for using it. Personally, I hate it when someone goes "but you shouldn't do that!" when I ask a question. I'll update my answer to mention that's it's highly unrecommended. – The Alchemist Sep 22 '10 at 13:02
  • 1
    Maybe the last part of my comment was not necessary (note that I actually didn't downvote your answer). At least, it was not a call for punishment but more a scream of frustration :) I will repost a more friendly version. – Pascal Thivent Sep 22 '10 at 13:29
  • 10
    This is really an **evil** practice strongly [discouraged](http://docs.codehaus.org/display/MAVENUSER/Dependency+Scopes). Every time someone use this, God kills a Maven developer. – Pascal Thivent Sep 22 '10 at 13:47
  • 3
    I do not share the fervor with which you do not recommend this practice. In the context of a source-controlled build, lack of access to a local maven repo, and a team project, this is the only sane solution to providing a dependency not available in public repos... – jfernand Jun 11 '14 at 15:54
  • @jfernand - *"... this is the only sane solution ..."*, Actually, it is not the only sane solution. See the other answers. – Stephen C Nov 12 '15 at 22:34
  • this data-local approach works with jar-with-dependencies, while the system approach does not – Joe C Oct 19 '19 at 22:19
14

In the past, I've done this by creating a "local" repository directory tree in the project itself, and referring to it in the POM by declaring a local repository with a project relative path.

But that is a hack. (Maybe not so hacky - per @Pascal's comment. I'm still a bit of a Maven novice, despite using it for a year or so.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 3
    That's a muuuuuuuuuuuuuuuuuch better *solution* than the evil system scope **hack**. People keep using the system scope (and recommending it) without understanding how it hurts... This is depressing. – Pascal Thivent Sep 22 '10 at 04:56
  • Nice to hear this worked for you. Could you perhaps update your answer to explain how I could make this technique work for me? – Jason R. Coombs Jan 15 '16 at 21:01
7

None of the other answers worked for me. I had to run a slightly different command...

mvn deploy:deploy-file -Durl=file:///path/to/yourproject/repo/ -Dfile=mylib-1.0.jar -DgroupId=com.example -DartifactId=mylib -Dpackaging=jar -Dversion=1.0

See complete steps in this article: https://devcenter.heroku.com/articles/local-maven-dependencies

Eric
  • 6,563
  • 5
  • 42
  • 66
  • 1
    If you have a pom.xml file for your local jar (e.g., because you built it yourself) then you can use `-DpomFile=` to point at that. The `groupId`, `artifactId`, `version` and `packaging` arguments are then picked up from the pom file. See https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html. – snark Feb 24 '16 at 14:01