无法在 com.itextpdf:itext7-core:pom:RELEASE 收集依赖项

sas*_*uke 1 java itext maven

我正在尝试使用 itextpdf 库创建 pdf 报告。正如官方文档中所述,我在 pom.xml 中添加了以下依赖项:

<properties>
   <itext.version>RELEASE</itext.version>
</properties>
<dependencies>
    <!-- add all iText 7 modules -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>${itext.version}</version>
        <type>pom</type>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

为了确保一切都整洁干净,我删除了 .m2/repository 目录$rm -R .m2/repository,然后在我的项目主目录中运行$mvn clean compile。然后我收到以下错误:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project business-project: Could not resolve dependencies for project myproject:business-project:jar:1.1-SNAPSHOT: Failed to collect dependencies at com.itextpdf:itext7-core:pom:RELEASE
    at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.getDependencies (LifecycleDependencyResolver.java:269)
    at org.apache.maven.lifecycle.internal.LifecycleDependencyResolver.resolveProjectDependencies (LifecycleDependencyResolver.java:147)
    at org.apache.maven.lifecycle.internal.MojoExecutor.ensureDependenciesAreResolved (MojoExecutor.java:248)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:202)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.project.DependencyResolutionException: Could not resolve dependencies for project myproject:business-project:jar:1.1-SNAPSHOT: Failed to collect dependencies at com.itextpdf:itext7-core:pom:RELEASE
Run Code Online (Sandbox Code Playgroud)

这是以下的输出$mvn dependency:tree

[INFO] -------------------< myproject:business-project >--------------------
[INFO] Building Business Layer 1.1-SNAPSHOT                        [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for myproject 1.1-SNAPSHOT:
[INFO] 
[INFO] my project ......................................... SUCCESS [  0.921 s]
[INFO] DB .......................................... SUCCESS [  0.108 s]
[INFO] Business Layer .............................. FAILURE [  0.234 s]
[INFO] Web Application ............................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.662 s
[INFO] Finished at: 2020-02-21T20:07:06+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project business-project: Could not resolve dependencies for project myproject:business-project:jar:1.1-SNAPSHOT: Failed to collect dependencies at com.itextpdf:itext7-core:pom:RELEASE: Failed to read artifact descriptor for com.itextpdf:itext7-core:pom:RELEASE: Failed to resolve version for com.itextpdf:itext7-core:pom:RELEASE: Could not find metadata com.itextpdf:itext7-core/maven-metadata.xml in local (/Users/MyComputer/.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
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :business-project
Run Code Online (Sandbox Code Playgroud)

运行$mvn -v我得到:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3/libexec
Java version: 1.8.0_101, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre
Default locale: de_DE, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.3", arch: "x86_64", family: "mac"
Run Code Online (Sandbox Code Playgroud)

我的 Nexus 服务器在我的 settings.xml 中配置:

<mirrors>

        <mirror>
                                               <id>public</id>
                                               <mirrorOf>*</mirrorOf>
                                               <name>Central Repositories of internal repository manager Nexus</name>
                                                <url>http://nexus-server:8081/nexus/content/repositories/public</url>
                               </mirror>
    </mirrors>
Run Code Online (Sandbox Code Playgroud)

请问我该如何解决这个问题?谢谢。

khm*_*ise 6

我不明白你到底想做什么,但根据给定的信息,我想你正在尝试将 pom 文件导入为所谓的BOM(物料清单)。您遇到的问题是,您试图将类型定义pom为依赖项,但这将不起作用,因为它不是您可以放在类路径上的内容。此外,您通过属性定义的给定版本根本不存在于中央存储库中。

dependencyManagement诀窍是像这样通过:

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.1.10</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

之后,您现在可以使用该 BOM 文件中定义的依赖项,如下所示:

<dependencies>
  <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>barcodes</artifactId>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

该 bom 中的所有依赖项现在可以通过定义它们来简单地使用,而无需由 .bom 文件中的 bom 文件控制的版本dependencyManagement。这是一种通过 bom 文件在单个位置管理依赖项的优雅方法。你只需要改变bom文件的版本就可以了。

  • ArtifactId `itext7-core` 仅以 `.pom` 形式存在,正是因为 @khmarbaise 100% 正确回答了。赞成。(我是 iText Software 的 QA 工程师) (2认同)