开发者

How to include local jar files in Maven project [duplicate]

This question already has answers here: Can I add jars to Maven 2 build classpath without installing them? (24 answers) Closed 7 years ago.

I d开发者_如何学编程o 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?


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


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.


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.)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜