1

I am trying to create a pdf report using itextpdf library. As it is instructed in the official documentation, I did add the following dependency in my 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>

To make sure everything is neat and clean, I deleted my .m2/repository directory $rm -R .m2/repository and did run afterwwards $mvn clean compile in my project home directory. I got then the following error :

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

This the output of $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

Running $mvn -v I get :

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"

My nexus server configured in my 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>

How can I fix this problem please? Thank you.

sasuke
  • 145
  • 8
  • Can you post the output of `mvn -v`, and `mvn dependency:tree`? – PiRocks Feb 21 '20 at 18:58
  • @PiRocks done! :) – sasuke Feb 21 '20 at 19:16
  • Are you sure that you are connected to the internet? Have you tried backing up your `.m2` and then deleting it? – PiRocks Feb 21 '20 at 19:20
  • Have you also considered using there repo: ``` itext-releases iText Repository - releases https://repo.itextsupport.com/releases ``` – PiRocks Feb 21 '20 at 19:23
  • @PiRocks Yes I am connected to the internet and did backup my .m2 as you said. One thing to mention is that the server I am connecting to is a local nexus server. Please check the post. – sasuke Feb 21 '20 at 19:26
  • 1
    See my answer. The version you have defined via property does simply not exist....etc. – khmarbaise Feb 21 '20 at 19:26

1 Answers1

3

I don't understand exactly what you are trying to do but based on the given information I suppose you are trying to import the pom file as a so called BOM (Bill Of Material). The issue you have is that you are trying to define a pom type as a dependency which will not work cause it is nothing what you can put on the classpath. Furthermore the given version, which you have defined via the property does simply not exist in Central repository.

The trick is to go via dependencyManagement like this:

<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>

Afterwards you can use now the dependencies which have been defined in that BOM file as this:

<dependencies>
  <dependency>
      <groupId>com.itextpdf</groupId>
      <artifactId>barcodes</artifactId>
  </dependency>
</dependencies>

All of the dependencies in that bom can now simply being used by defining them without the version which is controlled by the bom file in the dependencyManagement. This is an elegant way to manage dependencies at a single location via the bom file. You only need to change the version of the bom file and that's it.

khmarbaise
  • 81,692
  • 23
  • 160
  • 199
  • I am getting this error : Project build error: Non-resolvable import POM: Failure to find com.itextpdf:itext7-core:pom:7.1.10 in http://nexus-server:8081/nexus/content/repositories/public was cached in the local repository, resolution will not be reattempted until the update interval of public has elapsed or updates are forced – sasuke Feb 21 '20 at 19:39
  • On the other hand I don't want to import as a BOM as what you mentioned, I want to include as a .jar. – sasuke Feb 21 '20 at 19:39
  • 1
    There are two things here. First your nexus server seemed to be configured wrong...Furthermore I would try to build via `mvn clean package -U` and furthermore you have not read the docs nor read my explanations. This artifact is not a jar it's a pom only...and can't be used as a jar....the other artifacts like the one from my example (barcodes) is one... – khmarbaise Feb 21 '20 at 19:58
  • 2
    ArtifactId `itext7-core` only exists as a `.pom`, and exactly for the reason that @khmarbaise has 100% correctly answered. Upvoting. (I am QA Engineer at iText Software) – Amedee Van Gasse Feb 21 '20 at 22:32
  • @AmedeeVanGasse If you are one of them (iText Software) you should check the docs https://itextpdf.com/en/resources/installation-guides/installing-itext-7-java cause the example shows a version `RELEASE` which is not allowed anymore for a very long time. The following `which will ensure you install the current release versions of the iText 7 modules` is simply wrong and will produce WARNINGs in builds`. Furthermore the explanation on the page about "Adding all Modules" is simply wrong. As I described using a BOM has to be done as I described. This user guide should be fixed. – khmarbaise Feb 21 '20 at 22:54
  • I would have also reconsidered to name the artifact something like `itext7-bom` instead of `itext7-core` to make it more clear what it is intended for ... – khmarbaise Feb 21 '20 at 22:57
  • @khmarbaise `RELEASE` is a metaversion that resolves to the latest release number, when using *Maven 2*. It also means that the documentation doesn't have to be changed each time a new version is released. However, that metaversion has been dropped in *Maven 3*. It also means that for every new release, that page on the website needs to be updated too. I will report this as a content bug for our web team. – Amedee Van Gasse Feb 22 '20 at 00:35
  • Changing the artifact name of an already released artifact could of course introduce other issues, nevertheless, duly noted. – Amedee Van Gasse Feb 22 '20 at 00:37
  • See also https://stackoverflow.com/questions/30571/how-do-i-tell-maven-to-use-the-latest-version-of-a-dependency/1172371#1172371 – Amedee Van Gasse Feb 22 '20 at 00:48
  • @AmedeeVanGasse Of course you can not change already released artifacts (releases are immutable) but it would have been a good idea to do that in advance earlier. But it might be done for the next major release? – khmarbaise Feb 22 '20 at 11:31
  • I'll pass the message. – Amedee Van Gasse Feb 23 '20 at 14:58
  • 1
    Also, 'iText Core' is a product name, so that will most likely not change. *If* a change is made, then `itext-core-bom` as artifactname is more likely. – Amedee Van Gasse Feb 23 '20 at 15:01
  • Or, you know what? You can submit a pull request on the GitHub repo! – Amedee Van Gasse Feb 23 '20 at 15:01
  • All technical content will soon (ish) move away from the main website to a separate knowledge base (probably something something Confluence) but I don't know thw time frame so I don't know if the web team is going to update the current page on the website or if they are going to put it right in the knowledge base. – Amedee Van Gasse Feb 27 '20 at 21:13
  • @khmarbaise I tried with the `pom.xml` snippets you posted and then I get errors: `[ERROR] [ERROR] Some problems were encountered while processing the POMs: [ERROR] 'dependencies.dependency.version' for com.itextpdf:kernel:jar is missing. @ line 51, column 17`. So a version number is still required even when you put the BOM in `dependencyManagement`. – Amedee Van Gasse Mar 02 '20 at 14:15
  • And that essentially makes the `dependencyManagement` redundant. – Amedee Van Gasse Mar 02 '20 at 14:17
  • Just for the record: I know that you are an Apache Maven contributor, so I will assume that you know better than I do, I'm just copy/pasting what I see when I run the `mvn` command. – Amedee Van Gasse Mar 02 '20 at 14:48
  • @khmarbaise your answer here inspired me to ask a related question on the Code Review Stack Exchange, you may want to take a look: https://codereview.stackexchange.com/questions/238437/add-junit4-as-a-test-dependency-to-a-maven-project – Amedee Van Gasse Mar 11 '20 at 14:17