Maven:如何在Eclipse中包含jar,这在存储库中不可用?

时间:2023-01-18 11:19:34

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

我已经将jar拷贝到src\ webapp\WEB-INF\lib中。我使用eclipse。如果我将jar逐个添加到项目—> Java构建路径—>添加jar,那么我将执行项目—> Maven—>更新项目配置,它们将被Maven删除。Eclipse显示错误,其中包含“xxx无法解析”。

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 ?

注意:我不想创建自己的Maven存储库。只使用一次。我该如何继续?

4 个解决方案

#1


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.

并非所有库都可以在公共Maven存储库中找到,例如您自己的库或私有库。无论如何,首先你可以搜索Maven存储库,也许其中有些已经存在。

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:

如果他们真的不在,你不想安装Nexus或Artifactory,你可以选择以下两种方法之一:

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

    在本地存储库缓存中安装jar,不需要创建特殊的存储库。参见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.

    另一种方法是使用系统依赖项,您只需在文件系统中放置一个到达Jar的路径。这是一个不太推荐的方法,但是如果你真的想要它,它就会起作用。这是官方文件。

#2


2  

Add this an a Systems Dependency.

添加一个系统依赖项。


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

如果您想将这个(例如使用jquantlib)添加到maven本地repo使用:

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.

您可以在其他系统上使用类似的脚本文件。

#3


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

我认为,除非您别无选择,否则不应该使用系统依赖方法,这是因为您正在失去这里的“构建可移植性”。当然,您可以将jar存储在源代码控制系统中,并与项目的源文件一起存储,但我认为这不是一个好的方法。

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.

仅使用install:install-file还不够好——这确实会将jar以适当的格式部署到本地存储库中,但是当您移动到另一台计算机并开始在那里构建项目时,会发生什么情况呢?你需要再做一次。

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

所以,如果你不想安装nexus / artifactory(这是最好的解决方案,我相信),您应该创建一个另一个存储库(就在你的一些文件系统服务器),和部署jar(您可以使用mvn安装:安装文件在这里建议,然后复制整棵树)。现在,您可以配置apache web服务器,并通过http访问包含所有jar的目录。我不认为它比nexus/artifactory方法更好,但如果您熟悉apache web服务器,那么它可能更容易实现。为了让maven了解这个新存储库,您需要编辑%MAVEN_HOME%\conf\设置。xml文件

#4


0  

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.

要添加本地存储库之外的外部JAR文件,只需右键单击主源文件夹并从build path菜单中选择:“配置构建路径”,然后导航到libraries选项卡并单击“添加外部JAR文件”。接下来,找到要作为库添加的JAR(或zip)文件,并单击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

祝贺您,您现在已经成功地将一个外部JAR(或zip)添加到您的构建路径中,您现在可以从该JAR文件中导入任何类,而不需要向您抛出一个错误图:)http://hostthenpost.org/uploads/541be8420657320c74489ff8d456ad08.png。

#1


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.

并非所有库都可以在公共Maven存储库中找到,例如您自己的库或私有库。无论如何,首先你可以搜索Maven存储库,也许其中有些已经存在。

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:

如果他们真的不在,你不想安装Nexus或Artifactory,你可以选择以下两种方法之一:

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

    在本地存储库缓存中安装jar,不需要创建特殊的存储库。参见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.

    另一种方法是使用系统依赖项,您只需在文件系统中放置一个到达Jar的路径。这是一个不太推荐的方法,但是如果你真的想要它,它就会起作用。这是官方文件。

#2


2  

Add this an a Systems Dependency.

添加一个系统依赖项。


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

如果您想将这个(例如使用jquantlib)添加到maven本地repo使用:

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.

您可以在其他系统上使用类似的脚本文件。

#3


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

我认为,除非您别无选择,否则不应该使用系统依赖方法,这是因为您正在失去这里的“构建可移植性”。当然,您可以将jar存储在源代码控制系统中,并与项目的源文件一起存储,但我认为这不是一个好的方法。

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.

仅使用install:install-file还不够好——这确实会将jar以适当的格式部署到本地存储库中,但是当您移动到另一台计算机并开始在那里构建项目时,会发生什么情况呢?你需要再做一次。

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

所以,如果你不想安装nexus / artifactory(这是最好的解决方案,我相信),您应该创建一个另一个存储库(就在你的一些文件系统服务器),和部署jar(您可以使用mvn安装:安装文件在这里建议,然后复制整棵树)。现在,您可以配置apache web服务器,并通过http访问包含所有jar的目录。我不认为它比nexus/artifactory方法更好,但如果您熟悉apache web服务器,那么它可能更容易实现。为了让maven了解这个新存储库,您需要编辑%MAVEN_HOME%\conf\设置。xml文件

#4


0  

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.

要添加本地存储库之外的外部JAR文件,只需右键单击主源文件夹并从build path菜单中选择:“配置构建路径”,然后导航到libraries选项卡并单击“添加外部JAR文件”。接下来,找到要作为库添加的JAR(或zip)文件,并单击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

祝贺您,您现在已经成功地将一个外部JAR(或zip)添加到您的构建路径中,您现在可以从该JAR文件中导入任何类,而不需要向您抛出一个错误图:)http://hostthenpost.org/uploads/541be8420657320c74489ff8d456ad08.png。