0

I'm having a slight problem with incorporating the appropriate maven dependencies into my project for sling models.

When I deploy my bundle, I get the following import that can't be resolved:

org.apache.sling.models.annotations,version=[1.1,2) -- Cannot be resolved

I believe I have included this with the following dependencies:

<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.models.api</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.models.impl</artifactId>
</dependency>

I tried using:

<Embed-Transitive>true</Embed-Transitive>
<Import-Package>*</Import-Package>

in my bundle compile instructions, but this has just resulted in a ton of other dependencies not being resolved.

Surely I've gone down the garden path here somewhere. Any help would be greatly appreciated.

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
Bayani Portier
  • 660
  • 8
  • 18

2 Answers2

1

The org.apache.sling.models.api V1.0.2 bundle does export the following packages:

javax.inject,version=0.0.0
org.apache.sling.models.annotations,version=1.1.0
org.apache.sling.models.annotations.injectorspecific,version=1.0.0
org.apache.sling.models.spi,version=1.0.0
org.apache.sling.models.spi.injectorspecific,version=1.0.0

So if that bundle is active in your Sling instance, the org.apache.sling.models.annotations,version=[1.1,2) import should resolve.

Note that adding bundles to your maven dependencies might not be sufficient to install them in the running instance, what matters is whether the models.api bundle is active as seen from /system/console/bundles

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
  • Thanks Bertrand. Spot on, the api bundle wasn't loaded. I've marked the bundle I'm using Sling Models in to be tagged with *,org.apache.sling.models.api in the configuration instructions. Any tips for what else I might be missing? – Bayani Portier Jul 23 '14 at 13:48
  • Saying `Import-Package: *, org.apache.sling.models.api` is surely redundant, since the models.api package will be covered by the preceding wildcard. – Neil Bartlett Jul 23 '14 at 18:00
  • Whether the bundle is loaded in your Sling instance or not has nothing to do with pom dependencies in general - you'll need to load the bundle via the usual mechanisms. If the Sling installer is active the simplest might be to add the bundle to an install folder in the content repository. – Bertrand Delacretaz Jul 25 '14 at 07:30
1

Using Embed-Transitive is almost always a terrible idea. It traverses the entire transitive dependency hierarchy in Maven and pulls each one of those JARs into your own JAR. As a result you inherit all the package dependencies of all that crap you have dragged in.

When you have a bundle such as yours that requires an import -- in this case org.apache.sling.models.annotations -- the best solution is to find another bundle already available that exports the same package.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77