【问题标题】:How to add a new jar file as a dependency to POM.xml of Java Maven web project?如何将新的 jar 文件添加为 Java Maven Web 项目的 POM.xml 的依赖项?
【发布时间】:2015-03-15 11:54:16
【问题描述】:

我想从我的 java web (Maven) 项目连接到 Lotus Notes (8.5.3) 数据库。经过一些搜索,为了通过 CORBA DIIOP 从我的 java web 项目连接到 Lotus Notes 数据库, 我需要将 NCSO.jar 作为新的依赖项添加到我的 Maven Java Web 项目中。我找到了这个链接:How to add\install to maven dependency of newly created\third party jar in pom.xml,但我仍然不知道如何使它工作。我可以从我的本地计算机上找到 NCSO.jar,但基于该链接,我需要为该文件创建自己的存储库,然后将其添加到我的 pom.xml 文件中。 这是我第一次向我的 java web 项目 (pom.xml) 添加 .jar 依赖项。在我的java web 项目中,我有EJB 部分hand Web 部分,并且每个都有pom.xml。不知道如何将此 .jar 添加到 pom.xml。

 my java IDE is built on eclipse, the current workspace structure is like this: The current workspace called: developer, it contains followings: 
 developer
    workspaces (folder)
      Java-2014prj (folder)
         .settings (folder)
         projEAR (folder) 
         projEJB (folder)
         projWeb (folder)
         src (folder)
         .project
         pom.xml

is this the correct commands? and if this is correct and no errors, what should I do after the command executed? are all POM.xml added the NCSO.jar?
c:\developer>mvn install:install-file
Dfile=c:\NCSO.jar
DgroupId=lotus
DartifactId=domino
Dversion=1.0.0
Dpackaging=.jar
DgeneratePom=true

projEAR、projEJB 和 projWeb 也都包含 pom.xml。 NCSO.jar 位于我的本地 c:/NCSO.jar 上。 所以我想我应该在我的窗口命令(黑色)屏幕上运行代码。我应该在项目顶部文件夹 c:\developer 上运行它吗?或一些子文件夹? 我不知道如何创建我的本地存储库? 有人可以为我提供更多详细信息吗?谢谢。


编辑 - 我的结果基于 axiopisty 下面的说明

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] PARENT project of MARKETPROJ
[INFO] EJB project of MARKETPROJ
[INFO] WAR project of MARKETPROJ
[INFO] EAR project of MARKETPROJ
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-install-plugin/2.4/maven-install-plugin-2.4.pom
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:mave
n-install-plugin:2.4: Plugin org.apache.maven.plugins:maven-install-plugin:2.4 o
r one of its dependencies could not be resolved: Failed to read artifact descrip
tor for org.apache.maven.plugins:maven-install-plugin:jar:2.4
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-deploy-plugin/2.7/maven-deploy-plugin-2.7.pom
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:mave
n-deploy-plugin:2.7: Plugin org.apache.maven.plugins:maven-deploy-plugin:2.7 or
one of its dependencies could not be resolved: Failed to read artifact descripto
r for org.apache.maven.plugins:maven-deploy-plugin:jar:2.7
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-site-plugin/3.3/maven-site-plugin-3.3.pom
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:mave
n-site-plugin:3.3: Plugin org.apache.maven.plugins:maven-site-plugin:3.3 or one
of its dependencies could not be resolved: Failed to read artifact descriptor fo
r org.apache.maven.plugins:maven-site-plugin:jar:3.3
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:mave
n-antrun-plugin:1.3: Plugin org.apache.maven.plugins:maven-antrun-plugin:1.3 or
one of its dependencies could not be resolved: Failed to read artifact descripto
r for org.apache.maven.plugins:maven-antrun-plugin:jar:1.3
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:mave
n-assembly-plugin:2.2-beta-5: Plugin org.apache.maven.plugins:maven-assembly-plu
gin:2.2-beta-5 or one of its dependencies could not be resolved: Failed to read
artifact descriptor for org.apache.maven.plugins:maven-assembly-plugin:jar:2.2-b
eta-5
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-dependency-plugin/2.8/maven-dependency-plugin-2.8.pom
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:mave
n-dependency-plugin:2.8: Plugin org.apache.maven.plugins:maven-dependency-plugin
:2.8 or one of its dependencies could not be resolved: Failed to read artifact d
escriptor for org.apache.maven.plugins:maven-dependency-plugin:jar:2.8
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-release-plugin/2.3.2/maven-release-plugin-2.3.2.pom
[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:mave
n-release-plugin:2.3.2: Plugin org.apache.maven.plugins:maven-release-plugin:2.3
.2 or one of its dependencies could not be resolved: Failed to read artifact des
criptor for org.apache.maven.plugins:maven-release-plugin:jar:2.3.2
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metada
ta.xml
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven
-metadata.xml
[WARNING] Could not transfer metadata org.apache.maven.plugins/maven-metadata.xm
l from/to central (https://repo.maven.apache.org/maven2): Failed to transfer fil
e: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.
xml. Return code is: 501 , ReasonPhrase:Not Implemented.
[WARNING] Could not transfer metadata org.codehaus.mojo/maven-metadata.xml from/
to central (https://repo.maven.apache.org/maven2): Failed to transfer file: http
s://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml. Return co
de is: 501 , ReasonPhrase:Not Implemented.
[WARNING] Failure to transfer org.apache.maven.plugins/maven-metadata.xml from h
ttps://repo.maven.apache.org/maven2 was cached in the local repository, resoluti
on will not be reattempted until the update interval of central has elapsed or u
pdates are forced. Original error: Could not transfer metadata org.apache.maven.
plugins/maven-metadata.xml from/to central (https://repo.maven.apache.org/maven2
): Failed to transfer file: https://repo.maven.apache.org/maven2/org/apache/mave
n/plugins/maven-metadata.xml. Return code is: 501 , ReasonPhrase:Not Implemented
.
[WARNING] Failure to transfer org.codehaus.mojo/maven-metadata.xml from https://
repo.maven.apache.org/maven2 was cached in the local repository, resolution will
 not be reattempted until the update interval of central has elapsed or updates
are forced. Original error: Could not transfer metadata org.codehaus.mojo/maven-
metadata.xml from/to central (https://repo.maven.apache.org/maven2): Failed to t
ransfer file: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metad
ata.xml. Return code is: 501 , ReasonPhrase:Not Implemented.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] PARENT project of MARKETPROJ ............................. SKIPPED
[INFO] EJB project of MARKETPROJ ................................ SKIPPED
[INFO] WAR project of MARKETPROJ ................................ SKIPPED
[INFO] EAR project of MARKETPROJ ................................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.624 s
[INFO] Finished at: 2015-01-18T13:30:32-05:00
[INFO] Final Memory: 9M/244M
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'deploy' in the current project and in the pl
ugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the rep
ositories [local (C:\Users\jsmith\.m2\repository), central (https://repo.maven.apa
che.org/maven2)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundF
orPrefixException

********************************
**EDIT again:**
 I just checked my pom.xml again, and found we have these codes 
  <!-- local file-based repository on the team's shared drive -->
    <repository>
        <id>MARKETPROJ-repo-local</id>
        <name>MARKETPROJ File Based Internal Repository</name>
        <url>file://\\mark\mark_shrdata\MARKETPROJ\maven2\repo</url>
    </repository>

【问题讨论】:

  • 如果 NCSO.jar 是本地文件 jar 访问:stackoverflow.com/questions/4955635/…
  • 是的,它是一个本地文件,我将 NCSO.jar 复制到我的本地 C:/NCSO.jar 下,然后我将代码作为您的链接运行:但我得到了 BUILD FAILURE,带有 [ERROR ] 你指定的目标需要一个项目来执行但是这个目录C:\apcke-maven-3.2.3\bin 中没有POM。请确认您从正确的目录调用了 Maven。我是否必须在此目录中创建一个虚拟 pom?
  • 试图将 POM.xml 复制到该文件夹​​ C:\apcke-maven-3.2.3\bin,然后再次运行代码,再次失败。
  • 您应该让您的项目本地存储库驻留在您的项目根目录中。那么 pom.xml 的 repository 部分中指定的 url 应该是一个相对 url。
  • 如果您想尝试使用 maven 以外的工具,您可以查看 apache buildr。 buildr.apache.org/quick_start.html#dependencies

标签: java eclipse maven jar


【解决方案1】:

如果您的依赖项在公共存储库中不可用,则需要将依赖项安装到本地 maven 存储库中。您可能很想将其安装到您的 ~/.m2/repository 中,这是该问题的其他答案所述的方法。如果你这样做,构建将在该机器上为你正常工作。但这不是最好的解决方案。事实上,这是一个糟糕的解决方案,因为项目构建依赖于这个手动安装的工件。如果您删除 ~/.m2/repository 目录,您的构建将失败,因为该工件将不再位于存储库中。

如果您必须在本地安装依赖项,最好将其安装到项目本地存储库中,并将整个存储库提交到您的版本控制系统中。这样,如果您与其他人(包括您自己在另一台机器上)共享您的项目,该构建仍然可以为其他人工作,他们也不必在本地安装依赖项。

详细解释请看这里:https://devcenter.heroku.com/articles/local-maven-dependencies

例如,如果您的项目如下所示:

yourproject
+- pom.xml
+- src

然后你可以像这样创建一个项目本地存储库:

yourproject
+- pom.xml
+- src
+- repo

然后使用此命令将依赖项安装到项目本地存储库中:

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

所以你的项目现在看起来像这样:

yourproject
+- pom.xml
+- src
+- repo
   +- nsco
      +- ncso
         +- maven-metadata.xml
         +- ...
         +- 1.0
            +- ncso-1.0.jar
            +- ncso-1.0.pom
            +- ...

然后告诉你的项目 pom.xml 文件你的项目本地仓库:

<repositories>
    <!--other repositories if any-->
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>

并将您的项目本地依赖项也添加到 pom.xml:

<dependency>
    <groupId>ncso</groupId>
    <artifactId>ncso</artifactId>
    <version>1.0</version>
</dependency>

编辑 - 基于 cmets 中的问题。

假设你已经将需要部署到你项目本地maven仓库的依赖暂时存放在C:盘根目录下,并且你的项目也存放在C:盘根目录下,那么你的C:驱动器看起来像这样:

C:/NCSO.JAR
C:/yourproject
C:/yourproject/pom.xml
C:/yourproject/src
C:/yourproject/repo

还假设您从 C: 驱动器的根目录中的命令提示符中运行以下命令,那么这将是您将用于将依赖项安装到项目本地存储库中的命令:

C:\> mvn deploy:deploy-file -Durl=file:///C:/yourproject/repo/ -Dfile=NCSO.jar -DgroupId=ncso -DartifactId=ncso -Dpackaging=jar -Dversion=1.0

成功运行此命令后,您可以删除文件C:/NCSO.jar。不再需要它,因为它已安装(复制)到您的项目本地存储库中。此时,您还需要将 C:/yourproject/repo 及其所有数据添加到您的版本控制系统中,以便 repo 文件夹和其中的所有数据成为您项目的永久部分。

【讨论】:

  • 非常感谢您提供的详细信息,实际上,这是我的担忧,因为我们同时在这个 java web 项目上有一个大型 java 团队,我必须确保我添加的依赖项适用于其他开发人员在我在 Eclipse 中提交代码后,在您的代码中,-Durl=file:///path/to/yourproject/repo/,如何将其应用于我的情况?我什么时候应该将我的代码提交给 svn?我应该在你提到的最后一步之后提交吗?在我提交 svn 之前,我必须小心避免系统崩溃并让它为自己工作。
  • 这个命令:mvn deploy:deploy-file -Durl=file.....,我应该在哪个目录运行这些代码?我应该在“开发者”路径下运行它(如 c:\developer>mvn deploye:deploy-file -Durl=file... ...)还是其他地方?
  • 请参阅我对原始答案所做的编辑。我在这些 cmets 中包含了回答您的问题的具体细节。在您看到答案有效并且是您正在寻找的解决方案后,请不要忘记对答案进行投票并将其标记为已接受的答案。干杯。
  • 请参考我原帖中的 EDIT 以查看我的结果,根据您的指示部署似乎还有更多问题,如果我的运行环境有任何问题,请告诉我。跨度>
  • 请同时参考我的第二次编辑,我在我的 pom.xml 中找到了 repo,所以似乎我无法将 ncso.jar 部署到我的本地 repo,我可以物理复制 NCSO。 jar 到该共享驱动器(文件夹),而不是使用部署方法将 NSCO.jar 移动到我的本地 repo 文件夹?
【解决方案2】:

使用 cmd 提示符或终端,您可以按照链接建议的方式安装 jar,以便在您的 .m2 存储库中创建它,然后将以下内容添加到您的 pom.xml 中

在 cmd 提示符或终端中:

mvn install:install-file -Dfile="path-to-jar-file" -DgroupId=com.project.ncso -DartifactId=ncso -Dversion=1.0.0 -Dpackaging=jar

在你的 pom.xml 里面的项目目录中添加以下内容:

<dependency>
        <groupId>com.project.ncso</groupId>
        <artifactId>ncso</artifactId>
        <version>1.0.0</version>
    </dependency>

【讨论】:

  • 请阅读我之前的回复,它不起作用。有什么想法吗?
  • 它不起作用是什么意思?你的项目在哪里?你的项目目录中应该有一个 pom.xml
  • 项目目录与包含您的 java web 项目的目录相同。您不应该从 C:\apcke-maven-3.2.3\bin 运行项目,这是 maven 本身的二进制位置
  • 项目基于eclipse构建,当前工作空间“developer:”包含.settings文件夹、EAR文件夹、EJB文件夹、Web文件夹、src文件夹和pom.xml。这些在主文件夹上是平行的,但是如果我打开 Web 文件夹或 EJB 文件夹或 EAR 文件夹,它们也都有 POM.xml。所以我不知道在哪里以及如何在窗口命令窗口上运行代码?我应该在顶层文件夹中运行它吗?或一些子文件夹?你能告诉我具体如何运行这些代码吗?
  • 我已经更新了我的原始帖子,如果您需要更多信息,请参考它,让我知道,谢谢
猜你喜欢
  • 2012-10-22
  • 2015-01-16
  • 1970-01-01
  • 2014-12-17
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
相关资源
最近更新 更多