【问题标题】:Adding a system dependency to Maven向 Maven 添加系统依赖项
【发布时间】:2015-08-01 13:28:09
【问题描述】:

我正在通过 Maven 使用 Apache Spark,我正在尝试通过包含第 3 方 jar 并尝试在其中使用一些方法来修改源代码。

使用编译 Spark 项目时出现以下错误

mvn -Dhadoop.version=2.2.0 -Dscala-2.11 -DskipTests clean package
not found: object edu
[ERROR] import edu.xxx.cs.aggr._

我修改了ResultTask.scala 以包含导入语句。因此,maven 无法找到我尝试使用的 jar 并将其与项目链接。

我在 pom.xml 文件中添加了一个依赖项,例如:

<dependency>
    <groupId>edu.xxx.cs</groupId>
    <artifactId>aggr</artifactId>
    <version>0.99</version>
    <scope>system</scope>
    <systemPath>${basedir}/aggr.jar</systemPath>
</dependency>

我要链接的 jar 文件与 spark pom.xml 文件位于同一目录中。我将此依赖项添加到 pom.xml。我将它插入到 pom.xml 文件中的两个现有依赖项之间。我不确定出了什么问题,但我现在只想链接 jar,以便我可以使用其中的方法。我也不确定是否应该使用特定于 groupId、artifactId 和 version 的任何内容。 edu.xxx.cs.aggr 是包含其他源文件和包的根包。我将不胜感激任何帮助。

更新

我用过

mvn install:install-file -Dfile=<path-to-file> -DgroupId=edu.xxx.cs -DartifactId=aggr -Dversion=0.99 -Dpackaging=jar to install the jar to the .m2 repo. I checked the repo to see if it was installed, and it was.

我把 pom.xml 中的依赖改成

<dependency>
        <groupId>edu.purdue.cs</groupId>
        <artifactId>aggr</artifactId>
        <version>0.99</version>
</dependency>

我仍然遇到同样的错误。

【问题讨论】:

  • 尝试手动将其添加到您的本地 maven 存储库的位置 /edu/xxx/cs/aggr/0.99/aggr-0.99.jar
  • 这是错误的方法,不能保证它会一直有效。 Maven 提供其原生解决方案,而不是手动更改其工件库。
  • 如果你在 zip 工具中打开那个 aggr.jar 文件,我想知道里面真的有一个目录“edu”吗?
  • 我在 eclipse 上创建的一个简单的 java 项目中使用了这个 jar。导入语句 edu.xxx.cs.aggr 不会在该项目中产生任何错误。

标签: java maven apache-spark


【解决方案1】:

这就是我将系统依赖项添加到我的 maven pom.xml 的方式。在项目根路径中,我创建了 lib 目录,并在那里放置了我的 jar 文件。

<dependency>
    <groupId>com.sshx</groupId>
    <artifactId>sshx</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/sshxcute-1.0.jar</systemPath>
</dependency>

如果您仍然遇到同样的问题,请尝试通过从 jar 文件位置发出以下命令来手动添加依赖项

mvn install:install-file -Dfile=sshxcute-1.0.jar -DgroupId=com.sshx -DartifactId=sshx -Dversion=1.0 -Dpackaging=jar

此命令会将 jar 作为依赖项添加到您的 .m2 存储库中,您需要按如下方式更改 pom.xml 依赖项:

<dependency>
    <groupId>com.sshx</groupId>
    <artifactId>sshx</artifactId>
    <version>1.0</version>
</dependency>

完成后,从命令提示符发出mvn clean install 命令并构建您的应用程序。

但是,另一种选择是创建本地存储库。请参阅此线程: How to include local jar files in Maven project

【讨论】:

  • 我手动将 jar 文件添加到我的 .m2 存储库中。现在,它在正确的目录下,并且 jar 在那里。但是,我仍然收到此错误
  • 手动是否意味着发出 mvn install 命令?如上所述,您是否更新了 pom.xml 文件中的依赖项?
  • 我使用 mvn install:install-file -Dfile= -DgroupId=edu.xxx.cs -DartifactId=aggr -Dversion=0.99 -Dpackaging=jar 我改变了依赖到 edu.xxx.csaggr0.99
  • 尝试从 IDE 刷新您的项目一次或发出 mvn clean install 命令并尝试
  • 我在 IDE 上没有此设置。我正在通过 sublime 进行更改并使用 mvn 进行构建。所以,我应该只使用包含 pom.xml 的目录中的 mvn clean install?
【解决方案2】:

尝试将文件名包含在变量中。据我所知,您可能不会在 systemPath 参数中混合变量和路径。

【讨论】:

    【解决方案3】:

    您可以使用以下命令将您的 Jar 安装到本地 maven 存储库:

    mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
    

    【讨论】:

    • 我手动将 jar 添加到本地 repo。我创建了相应的目录结构 /edu/xxx/cs/aggr/0.99/aggr-0.99.jar。我再次编译,我仍然得到同样的错误
    • 不要尝试手动创建它。只需使用命令它会自动为您创建它。我认为这个命令会用 jar 创建一些 2-3 个支持文件。
    • 我就是这么做的。我使用 mvn install:install-file -Dfile= -DgroupId=edu.xxx.cs -DartifactId=aggr -Dversion=0.99 -Dpackaging=jar
    • 安装jar后不要忘记刷新项目。或尝试mvn clean
    • 当我构建项目时,我已经通过执行 mvn -Dhadoop.version=2.2.0 -Dscala-2.11 -DskipTests clean package 使用了 clean。我在构建它之前尝试了 mvn clean,我仍然收到错误。
    【解决方案4】:

    必须有几个选项,但唯一的方法。 方式取决于 Maven 的实际工作方式。它适用于依赖库。 因此,要成功编译,您需要为您的 JAR 指定有效的存储库。 您可以为此使用本地存储库。要在此处添加 JAR,您可以为该 JAR 调用“mvn install”命令。 作为替代方案,您可以在 POM 文件中指定用于 Maven 的本地存储库的位置。

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 2011-02-05
      • 2020-01-20
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多