Recently I encountered a project with a complex model package structure and looked into how best to support this. I found documentation at WCM.io that indicated this.
<!-- Export all non-internal packages by default -->
Export-Package: !*.impl.*,!*.internal.*,\ io.wcm.samples.*
Replace io.wcm.samples.* with the topmost package from your project.
That got me wondering what the advantage was of using syntax like this vs. simply dropping package-info.json files into all of the folders that contained models.
Additionally in the WCM.io documentation it mentions that for bnd plugin implementations Sling-Model-Packages should be moved into bnd configuration.
<configuration>
<bnd>
Sling-Initial-Content: SLING-INF/app-root;overwrite:=true;ignoreImportProviders:=xml;path:=/apps/wcm-io/wcm/commons
Sling-Model-Packages: io.wcm.wcm.commons
Sling-Namespaces: wcmio=http://wcm.io/ns
Import-Package: \
<!-- For build compatibility with Java 11 -->\
javax.annotation;version="[0.0,2)",\
*
</bnd>
</configuration>
Similarly why use this syntax vs just using the ModelScanner?
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Hi @Preston ,
Using package-info.json versus Export-Package for defining models to export in OSGi bundles offers different approaches with varying advantages. Let's explore each and also delve into the usage of Sling-Model-Packages versus ModelScanner.
Granular Control:
Explicit Declaration:
Modularity:
Simplicity:
Coarse-Grained Control:
Convention-Based:
Explicit Configuration:
Performance Optimization:
Compatibility:
Automatic Registration:
Dynamic Discovery:
Simplicity:
Here's a simple example demonstrating how you can use ModelScanner in Java code:
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Inject;
@Model(adaptables = Resource.class)
public class MyModel {
@Inject
private String title;
public String getTitle() {
return title;
}
}
In this example, MyModel is annotated with @Model, indicating that it's a Sling model. The ModelScanner will automatically detect and register this model based on the annotations and adaptability defined.
Hi @Preston ,
Using package-info.json versus Export-Package for defining models to export in OSGi bundles offers different approaches with varying advantages. Let's explore each and also delve into the usage of Sling-Model-Packages versus ModelScanner.
Granular Control:
Explicit Declaration:
Modularity:
Simplicity:
Coarse-Grained Control:
Convention-Based:
Explicit Configuration:
Performance Optimization:
Compatibility:
Automatic Registration:
Dynamic Discovery:
Simplicity:
Here's a simple example demonstrating how you can use ModelScanner in Java code:
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Inject;
@Model(adaptables = Resource.class)
public class MyModel {
@Inject
private String title;
public String getTitle() {
return title;
}
}
In this example, MyModel is annotated with @Model, indicating that it's a Sling model. The ModelScanner will automatically detect and register this model based on the annotations and adaptability defined.
Views
Likes
Replies