2
votes

I'd like to setup Sling Models in our sling based project (actually it is an AEM project). I followed the instructions at http://sling.apache.org/documentation/bundles/models.html but unfortunately the adapTo method on a resource returns null, so I think I made some mistake in my OSGI configuration setup. Unfortunately I don't get any log file error, so now I need some help :) We work with the maven-bundle-plugin which I configured the following way:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
            <instructions>
                    <!-- we have to export for the classes to be visible -->
                    <Export-Package>
                            com.company.project.*;version=${project.version}
                    </Export-Package>
                    <Import-Package>
                            org.apache.felix.scr;version="[1.6,2)",
                            org.apache.sling.api;version="[2.1,3)",
                            org.apache.sling.api.request;version="[2.1,3)",
                            org.apache.sling.commons.scheduler;version="[2.1,3)",
                            *                                                        
                    </Import-Package>
                    <Private-Package>
                            org.apache.sling.models.*
                    </Private-Package>
                    <Include-Resource>
                            {maven-resources}
                    </Include-Resource>
                    <Sling-Model-Packages>
                            com.company.project.models.componentgroup
                    </Sling-Model-Packages>
            </instructions>
    </configuration>
</plugin>

The Model is implemented as an interface:

package com.company.project.models.componentgroup;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;

import javax.inject.Inject;


@Model(adaptables = Resource.class)
public interface MyModel {

    @Inject
    @Default(values = "My Property Value")
    String getMyProperty();

}

I got a Controller class in which a SlingResource adapts to the Model, but returns null:

MyModel model = resource.adaptTo(MyModel.class);

I'm really thankful for any help.

1

1 Answers

2
votes

I managed to get it work by installing the bundels org.apache.sling.models.api and org.apache.sling.models.impl manually with the install function in the OSGI Felix console and removing

<Private-Package>
    org.apache.sling.models.*
</Private-Package>

It works with interfaces and classes, like expected :)

So my error is in the maven-bundle-plugin configuration, because the bundles aren't visible with Private-Package.

Where do you suggest to add the sling.models in the maven-bundle-plugin configuration?