无法在maven项目中添加本地jar文件

时间:2016-03-20 17:31:32

标签: java maven intellij-idea

我正在尝试重新打包log4j,以便我可以在Android上使用它。为此,我必须使用openbeans库,并将每个java.beans替换为com.googlecode.openbeans

显然这还不够,因为在重新打包通过maven log4j之前,我要在项目中加入openbeans-1.0.jar

所以我找到了this方法。

我通过以下命令安装了openbeans:

mvn install:install-file -Dfile=/home/luca/openbeans-1.0.jar -DgroupId=com.googlecode -DartifactId=openbeans -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true

我检查过正确执行命令检查.jar是否存在于以下路径中(并且确实如此):

~/.m2/repository/com/googlecode/openbeans/1.0/openbeans-1.0.jar

然后我编辑了pom.xml文件添加:

<dependency>
  <groupId>com.googlecode</groupId>
  <artifactId>openbeans</artifactId>
  <version>1.0</version>
</dependency>

但如果我尝试mvn package,则会返回此错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.2:run (rmdir_tests_output) on project log4j: Execution rmdir_tests_output of goal org.apache.maven.plugins:maven-antrun-plugin:1.2:run failed: Plugin org.apache.maven.plugins:maven-antrun-plugin:1.2 or one of its dependencies could not be resolved: Failure to find com.googlecode:openbeans:jar:1.0 in http://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 -> [Help 1]

就像install-file没有效果一样。

我也尝试使用this方法,使用以下代码(显然我复制了项目根目录中的.jar):

<dependency>
  <groupId>com.googlecode</groupId>
  <artifactId>openbeans</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/openbeans-1.0.jar</systemPath>
</dependency>

这里返回的错误是:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.1:compile (default-compile) on project log4j: Compilation failure
[ERROR] /home/luca/apache-log4j-1.2.17/src/main/java/org/apache/log4j/config/PropertyGetter.java:[31,21] error: package com.googlecode does not exist

我在做什么错了?

1 个答案:

答案 0 :(得分:3)

即使我仍然不明白为什么,我发现执行:

mvn install:install-file -Dfile=/home/luca/openbeans-1.0.jar -DgroupId=com.googlecode.openbeans -DartifactId=openbeans -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true

在pom中使用:

<dependency>
  <groupId>com.googlecode.openbeans</groupId>
  <artifactId>openbeans</artifactId>
  <version>1.0</version>
</dependency>

编译代码时没有错误。但正如我所说,仍然想知道为什么。