Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

What's the advantage of using package-info.json vs. Export-Packages to define models to export Models?

Avatar

Level 4

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? 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

Advantages of package-info.json:

  1. Granular Control:

    • With package-info.json, you have fine-grained control over which packages contain models and should be exported.
  2. Explicit Declaration:

    • It explicitly defines the packages containing models, making it easier to understand and maintain the export configuration.
  3. Modularity:

    • You can distribute model definitions across different packages, promoting modularity and organization in complex projects.

Advantages of Export-Package:

  1. Simplicity:

    • Export-Package offers a simpler syntax for specifying which packages to export, especially for straightforward projects with fewer model packages.
  2. Coarse-Grained Control:

    • It's suitable for exporting entire packages or excluding certain patterns, providing flexibility in export configurations.
  3. Convention-Based:

    • It follows OSGi conventions, making it easier for developers familiar with OSGi to understand and work with.

Advantages of Sling-Model-Packages:

  1. Explicit Configuration:

    • Sling-Model-Packages explicitly declares the packages containing Sling models, ensuring clarity and precision in the setup.
  2. Performance Optimization:

    • It helps in optimizing performance by specifying only the relevant packages to scan for Sling models.
  3. Compatibility:

    • Aligns well with Sling's architecture and best practices, ensuring compatibility and smooth integration with Sling-based projects.

Advantages of ModelScanner:

  1. Automatic Registration:

    • ModelScanner automatically detects and registers models based on conventions or annotations, reducing manual configuration efforts.
  2. Dynamic Discovery:

    • It enables dynamic discovery of models during runtime, facilitating flexibility and extensibility in larger projects.
  3. Simplicity:

    • It simplifies the setup process by handling model registration automatically, suitable for projects where explicit configuration is not required.

Example Java Code:

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.

View solution in original post

2 Replies

Avatar

Community Advisor

@Preston Interesting, I tried searching for this but didn't find a solid answer. I will look into this.

 

Please let us know if you find an answer for this.

Avatar

Correct answer by
Community Advisor

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.

Advantages of package-info.json:

  1. Granular Control:

    • With package-info.json, you have fine-grained control over which packages contain models and should be exported.
  2. Explicit Declaration:

    • It explicitly defines the packages containing models, making it easier to understand and maintain the export configuration.
  3. Modularity:

    • You can distribute model definitions across different packages, promoting modularity and organization in complex projects.

Advantages of Export-Package:

  1. Simplicity:

    • Export-Package offers a simpler syntax for specifying which packages to export, especially for straightforward projects with fewer model packages.
  2. Coarse-Grained Control:

    • It's suitable for exporting entire packages or excluding certain patterns, providing flexibility in export configurations.
  3. Convention-Based:

    • It follows OSGi conventions, making it easier for developers familiar with OSGi to understand and work with.

Advantages of Sling-Model-Packages:

  1. Explicit Configuration:

    • Sling-Model-Packages explicitly declares the packages containing Sling models, ensuring clarity and precision in the setup.
  2. Performance Optimization:

    • It helps in optimizing performance by specifying only the relevant packages to scan for Sling models.
  3. Compatibility:

    • Aligns well with Sling's architecture and best practices, ensuring compatibility and smooth integration with Sling-based projects.

Advantages of ModelScanner:

  1. Automatic Registration:

    • ModelScanner automatically detects and registers models based on conventions or annotations, reducing manual configuration efforts.
  2. Dynamic Discovery:

    • It enables dynamic discovery of models during runtime, facilitating flexibility and extensibility in larger projects.
  3. Simplicity:

    • It simplifies the setup process by handling model registration automatically, suitable for projects where explicit configuration is not required.

Example Java Code:

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.