【发布时间】:2017-10-12 02:58:19
【问题描述】:
TL;DR
Maven 在存储库 file://<homeDirectory>/.m2/repository/ 中找不到 <importedGroupid>:<importedArtifactid>:jar:1.0。包其实在<homeDirectory>/.m2/repository/<importedGroupid>/<importedArtifactid>/1.0/
我不认为了解 Maven,但我能想到的两个潜在原因是:
- 它会与
:和/混淆(即 UNIX 风格的路径分隔符与:在 Maven 中的含义),或者 - 如果
:和/实际上被解释为相同的,则它搜索的路径包括另一个子目录级别jar,它不存在于实际目录结构中。
详细说明
我试图在 Maven 中本地导入一个包(因为该包没有在线部署)。 stackoverflow上的各种答案(例如this和this)建议在包含包的jar上运行mvn install,就像这样(假设jar在/usr/share/java中,是1.0版等):
mvn install:install-file -Dfile=/usr/share/java/<importedArtifactid>-1.0-SNAPSHOT.jar -DgroupId=<importedGroupid> -DartifactId=<importedArtifactid> -Dversion=1.0 -Dpackaging=JAR -DgeneratePom=true
并在当前包的pom.xml 中添加:
<repository>
<id>repository</id>
<url>file://${user.home}/.m2/repository/</url>
</repository>
mvn install 命令将包部署到<homeDirectory>/.m2/repository/。到目前为止,这工作正常:
<prompt> $ mvn install:install-file -Dfile=/usr/share/java/<importedArtifactid>-1.0-SNAPSHOT.jar -DgroupId=<importedGroupid> -DartifactId=<importedArtifactid> -Dversion=1.0 -Dpackaging=JAR -DgeneratePom=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building <currentDirectoryProject> 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ <currentDirectoryProjectArtifactid> ---
[INFO] Installing /usr/share/java/economicsl-1.0-SNAPSHOT.jar to <homeDirectory>/.m2/repository/<importedGroupid>/<importedArtifactid>/1.0/<importedArtifactid>-1.0.JAR
[INFO] Installing /tmp/mvninstall256012398997457078.pom to <homeDirectory>/.m2/repository/<importedGroupid>/<importedArtifactid>/1.0/<importedArtifactid>-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.489 s
[INFO] Finished at: 2017-05-11T19:11:12+01:00
[INFO] Final Memory: 9M/292M
[INFO] ------------------------------------------------------------------------
<prompt> $
包也出现在<homeDirectory>/.m2/repository/
<prompt> $ ls `<homeDirectory>/.m2/repository/<importedGroupid>/<importedArtifactid>/1.0/`
<importedArtifactid>-1.0.JAR <importedArtifactid>-1.0.jar.lastUpdated <importedArtifactid>-1.0.pom _remote.repositories
<prompt> $
但是,构建尝试导入包的项目失败:
<prompt> $ mvn package -U
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building <currentDirectoryProject> 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: file://<homeDirectory>/.m2/repository/<importedGroupid>/<importedArtifactid>/1.0/<importedArtifactid>-1.0.jar
Downloading: https://repo.maven.apache.org/maven2/<importedGroupid>/<importedArtifactid>/1.0/<importedArtifactid>-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.730 s
[INFO] Finished at: 2017-05-12T17:59:43+01:00
[INFO] Final Memory: 13M/292M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project <currentDirectoryProjectArtifactid>: Could not resolve dependencies for project <currentDirectoryProjectGroupid>:<currentDirectoryProjectArtifactid>:jar:1.0-SNAPSHOT: Could not find artifact <importedGroupid>:<importedArtifactid>:jar:1.0 in repository (file://<homeDirectory>/.m2/repository/) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[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 read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
<prompt> $
如果/usr/share/java 中的jar 的绝对路径在pom.xml 中定义,则项目成功构建,并显示有关不使用绝对路径的警告,但执行失败,因为它这次没有再次找到包。
Maven 版本是 Apache Maven 3.3.9。
占位符
-
<currentDirectoryProject>应该导入其他包的项目。所有终端命令都从该项目的根目录执行(这是它的pom.xml所在的位置)。 -
<currentDirectoryProjectArtifactid>应该导入其他包的项目的 ArtifactID。 -
<currentDirectoryProjectGroupid>应该导入其他包的项目的GroupID。 -
<importedArtifactid>要导入的项目的ArtifactID -
<importedGroupid>待导入项目的GroupID -
<homeDirectory>用户的主目录,即/home/<userName> -
<prompt>终端提示
【问题讨论】:
-
刚刚注意到你有一个大写的文件扩展名“JAR”,而 maven 正在寻找小写的“jar”。如果您的文件系统区分大小写,那么这很重要。
-
@gogstad 是的,你完全正确。 (因此,
mvn install必须使用-Dpackaging=jar调用,而不是使用-Dpackaging=JAR来解决此问题。)如果您可以将此作为答案重新发布,我会接受。
标签: java maven repository maven-3 pom.xml