如何在Maven项目中包含本地jar文件[重复]

时间:2022-04-10 15:07:04

This question already has an answer here:

这个问题在这里已有答案:

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

我不想在Maven存储库(本地/远程)中安装几个jar。特别是我有几个jar文件

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?

使用NetBeans打开/编辑时如何将它们包含到我的项目中?

4 个解决方案

#1


21  

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

您是否考虑将这两个JAR添加为系统依赖项?例如。,

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

请注意,这是不推荐的,如果有的话,应该非常谨慎地使用。

#2


38  

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

创建一个本地存储库,其中包含公共存储库中不可用的jar文件

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

注意:我使用Eclipse,因此一些指令特定于Eclipse。大多数都很容易推广。


Assumptions

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

    这个罐子是由Maven在另一个项目中创建的,具有以下内容......

    <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
  • 在项目基目录下创建repo目录
  • For each jar to be accessed locally...
    • add directories for each level of the groupID (ex. /repo/com/foo)
    • 为groupID的每个级别添加目录(例如/ repo / com / foo)
    • add jar name (aka artifactId) without the version (ex. /repo/com/foo/test)
    • 添加jar名称(aka artifactId)没有版本(例如/ repo / com / foo / test)
    • add directory for the version of the jar (ex. /repo/com/foo/test/0.1.1)
    • 添加jar版本的目录(例如/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)
    • 将jar放在该目录中(例如/repo/com/foo/test/0.1.1/test-0.1.1.jar)
  • 对于每个要在本地访问的jar ...为groupID的每个级别添加目录(例如/ repo / com / foo)在没有版本的情况下添加jar名称(aka artifactId)(例如/ repo / com / foo / test)添加jar版本的目录(例如/repo/com/foo/test/0.1.1)将jar放在该目录中(例如/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...

    添加对本地jar的依赖。从上面的例子来看,这将是......

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

Rebuild

  • Rt click pom.xml -> Run as -> Maven build
  • Rt单击pom.xml - >运行为 - > Maven构建

#3


13  

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.

在过去,我通过在项目本身中创建“本地”存储库目录树来完成此操作,并通过声明具有项目相对路径的本地存储库在POM中引用它。

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

但这是一个黑客。 (也许不是那么hacky - 根据@Pascal的评论。尽管使用了一年左右,我仍然是一个Maven新手。)

#4


6  

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

请参阅本文中的完整步骤:https://devcenter.heroku.com/articles/local-maven-dependencies

#1


21  

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

您是否考虑将这两个JAR添加为系统依赖项?例如。,

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

请注意,这是不推荐的,如果有的话,应该非常谨慎地使用。

#2


38  

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

创建一个本地存储库,其中包含公共存储库中不可用的jar文件

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

注意:我使用Eclipse,因此一些指令特定于Eclipse。大多数都很容易推广。


Assumptions

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

    这个罐子是由Maven在另一个项目中创建的,具有以下内容......

    <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
  • 在项目基目录下创建repo目录
  • For each jar to be accessed locally...
    • add directories for each level of the groupID (ex. /repo/com/foo)
    • 为groupID的每个级别添加目录(例如/ repo / com / foo)
    • add jar name (aka artifactId) without the version (ex. /repo/com/foo/test)
    • 添加jar名称(aka artifactId)没有版本(例如/ repo / com / foo / test)
    • add directory for the version of the jar (ex. /repo/com/foo/test/0.1.1)
    • 添加jar版本的目录(例如/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)
    • 将jar放在该目录中(例如/repo/com/foo/test/0.1.1/test-0.1.1.jar)
  • 对于每个要在本地访问的jar ...为groupID的每个级别添加目录(例如/ repo / com / foo)在没有版本的情况下添加jar名称(aka artifactId)(例如/ repo / com / foo / test)添加jar版本的目录(例如/repo/com/foo/test/0.1.1)将jar放在该目录中(例如/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...

    添加对本地jar的依赖。从上面的例子来看,这将是......

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

Rebuild

  • Rt click pom.xml -> Run as -> Maven build
  • Rt单击pom.xml - >运行为 - > Maven构建

#3


13  

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.

在过去,我通过在项目本身中创建“本地”存储库目录树来完成此操作,并通过声明具有项目相对路径的本地存储库在POM中引用它。

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

但这是一个黑客。 (也许不是那么hacky - 根据@Pascal的评论。尽管使用了一年左右,我仍然是一个Maven新手。)

#4


6  

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

请参阅本文中的完整步骤:https://devcenter.heroku.com/articles/local-maven-dependencies