Expand my Community achievements bar.

SOLVED

AEM - Maven build cannot resolve dependencies when imported

Avatar

Level 4

Hi all,

I am trying to add a 3rd party library to our AEM java project. This project is build in Maven. My Maven build runs successfully, but when I import it in AEM, it crashes and cannot find dependencies that come with said 3rd party.

My question is about my build. I included the 3rd party library as a dependency,

<dependency>

<groupId>com.fasterxml.jackson.dataformat</groupId>

<artifactId>jackson-dataformat-xml</artifactId>

<version>2.9.0</version>

</dependency>

and even add the .jar file as an <Embed-Dependency> package in the pom with <embed-transitive> set.

<Embed-Transitive>true</Embed-Transitive>

<Embed-Dependency>jackson-dataformat-xml;scope=compile|runtime<Embed-Dependency>

This embeds the 3rd party .jar file into my AEM bundle build.

1468742_pastedImage_2.png

1468743_pastedImage_4.png

When I import this package and install into AEM, my bundle doesn't start and shows the below error, which basically says that all the dependencies for my 3rd party library cannot be resolved.

I attempted to fix this by adding the extra dependencies under <Import-Package> right below the <Embed-Dependency>

<Import-Package>

com.fasterxml.jackson.*,

org.codehaus.stax2.*,

</Import-Package>

1468719_pastedImage_0.png

After investigating in the bundles I saw that these bundles are indeed being loaded and even show up in the Maven Manifest MF, but AEM doesn't count them as "Imported Packages", despite them showing up under "Import-Package". They are all clearly there under the Import-Package, correct version and all but AEM does not resolve any of them.

1468720_pastedImage_1.png

Is there something else AEM needs to run these correctly? I didn't set a version that they should load but, it seems to me that since they are showing up with the correct version AEM is looking for under "Import-Package" that they would be there somewhere.

Any ideas for Maven + AEM builds would definitely help!

Thanks

Brendan

1 Accepted Solution

Avatar

Correct answer by
Level 4

This was fixed for me by also adding it under the <export-package> part of the POM.

In total I had to to 3 things to embed a 3rd party library.

1. Add standard Maven Dependency

<dependency>

<groupId>com.fasterxml.jackson.dataformat</groupId>

<artifactId>jackson-dataformat-xml</artifactId>

<version>2.9.0</version>

</dependency>

2. Embed Dependency and Import Package

<Embed-Transitive>true</Embed-Transitive>

<Embed-Dependency>jackson-dataformat-xml;scope=compile|runtime</Embed-Dependency>

<Import-Package>

com.fasterxml.jackson.*,

org.codehaus.stax2.*,

javax.inject*;version=0.0.0,*;

</Import-Package>

3. Export packages that are also in import-package

<Export-Package>com.fasterxml.jackson.*, org.codehaus.stax2.*</Export-Package>

This works and was found by me just following a few examples. While this works, I'm still a bit unsure of why it works.

Does anybody know why I have to export something if I import it? What is the difference between "import and export" that AEM can tell?

View solution in original post

6 Replies

Avatar

Correct answer by
Level 4

This was fixed for me by also adding it under the <export-package> part of the POM.

In total I had to to 3 things to embed a 3rd party library.

1. Add standard Maven Dependency

<dependency>

<groupId>com.fasterxml.jackson.dataformat</groupId>

<artifactId>jackson-dataformat-xml</artifactId>

<version>2.9.0</version>

</dependency>

2. Embed Dependency and Import Package

<Embed-Transitive>true</Embed-Transitive>

<Embed-Dependency>jackson-dataformat-xml;scope=compile|runtime</Embed-Dependency>

<Import-Package>

com.fasterxml.jackson.*,

org.codehaus.stax2.*,

javax.inject*;version=0.0.0,*;

</Import-Package>

3. Export packages that are also in import-package

<Export-Package>com.fasterxml.jackson.*, org.codehaus.stax2.*</Export-Package>

This works and was found by me just following a few examples. While this works, I'm still a bit unsure of why it works.

Does anybody know why I have to export something if I import it? What is the difference between "import and export" that AEM can tell?

Avatar

Level 2

Hi @brendanf9753525 ,

 

Iam facing similar issue with below external dependency.

 

<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.2.0.jre11</version>
</dependency>

 i tried as you advised but nothing worked , can you help me here.

 

Thanks,

Durga Kavali

 

 

Avatar

Community Advisor

@brendanf9753525 - You might find this link useful for your query - 

https://blog.christianposta.com/osgi/understanding-how-osgi-bundles-get-resolved-part-i/

 

Referencing from the above blog - 

A trivial explanation of a bundle having its dependencies resolved could go like this: if a bundle imports (Import-Package) a specific package, that package must be made available by another bundle's exports (Export-Package). If bundle A has Import-Package: org.apache.foo then there must be a bundle deployed that has an Export-Package: org.apache.foo

Avatar

Level 7

Need to create manifest.mf file manually... And jar Jackson u downloaded manually? Or

just need to add dependency in pom. Xml then embed tag with import and export tag......

Maven will do all downloading the jar file and deploying in aem when we do mvn clean isntall -PautoInstallPackage

Avatar

Level 3

Jackson dependencies are part of OSGi and should not be added as embed dependency. Please, correct me if I am wrong.

I had a similar problem and figure out that there is already imported jackson dependency in intelliJ and I was overriding it with added jackson version.

Please check External Dependencies in your IDE:

Screenshot 2019-10-20 at 18.12.58.png

Please make sure that all of those are defined with the same version. In my case, I have changed all jackson dependencies in pom.xml on 2.8.11 and it all worked fine without embedding.

Avatar

Level 7

had similar issue not with the jackson but with com.opencsv......