OSGi环境下AEM项目google-maps-services包装Bundle依赖嵌入失败问题排查
Alright, let's break down what's going wrong with your OSGi bundle setup for Google Maps Services in AEM, and fix it step by step. The core issues here are conflicting BND configurations, incorrect handling of import/embed rules, and unnecessary forced dependencies.
Key Problems in Your Current Setup
- Overridden Import Packages: You explicitly set
Import-Package: javax.annotation;version=0.0.0which overrides BND's automatic dependency calculation. This means all other required packages (likeokhttp3,okio, etc.) aren't being imported, causing the bundle to fail resolving dependencies. - Conflict Between Export Rules: You're using both
Export-Packageand-exportcontents, which can lead to unexpected package exports or conflicts. - Unnecessary Embedding: Some dependencies (like
okhttp/okio) might already be provided by AEM's core bundles, so embedding them creates redundancy or version conflicts. - Forced Optional Dependency: The
appengine-apiis an optional dependency for Google Maps Services (only needed for Google App Engine environments), but your current setup treats it as a required import.
Step-by-Step Fixes
1. Correct the Import-Package Configuration
First, fix the import rules to let BND automatically handle most dependencies while explicitly defining critical ones (with version ranges) and marking optional dependencies as such:
Import-Package: javax.annotation;version="[0.0.0,2.0.0)", com.google.appengine.api;resolution:=optional, *
- The
*tells BND to auto-import all other required packages. resolution:=optionalmakes the App Engine dependency non-mandatory, so the bundle can start without it in AEM.
2. Refine Embed-Dependency to Only Include Necessities
Only embed dependencies that aren't already provided by AEM. For AEM 6.5+, okhttp and okio are typically available via core bundles, so we can skip embedding them. We'll keep embedding google-maps-services and opencensus-api (since AEM doesn't provide this by default):
Embed-Dependency: google-maps-services,opencensus-api;inline=true
inline=trueensures the embedded JARs' classes are merged directly into your bundle, avoiding issues with nested JARs in OSGi.
3. Clean Up Export-Package Configuration
Remove the conflicting -exportcontents rule and explicitly define your exports with a version (following OSGi best practices):
Export-Package: com.google.maps.*;version="0.18.0"
This ensures only the Google Maps Services packages are exported, not any embedded dependency packages.
4. Full Corrected BND Configuration
Putting it all together, your BND config should look like this:
Bundle-Category: thirdparty Import-Package: javax.annotation;version="[0.0.0,2.0.0)", com.google.appengine.api;resolution:=optional, * Export-Package: com.google.maps.*;version="0.18.0" Embed-Dependency: google-maps-services,opencensus-api;inline=true -snapshot: ${tstamp;yyyyMMddHHmmssSSS}
5. Update Maven Dependencies
Mark optional dependencies as such in your POM to avoid unnecessary transitive dependencies:
<dependency> <groupId>com.google.appengine</groupId> <artifactId>appengine-api-1.0-sdk</artifactId> <version>1.9.88</version> <optional>true</optional> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.14.4</version> <optional>true</optional> </dependency> <dependency> <groupId>com.squareup.okio</groupId> <artifactId>okio</artifactId> <version>1.17.2</version> <optional>true</optional> </dependency>
Final Checks
After rebuilding and deploying the bundle:
- Check the OSGi console (
/system/console/bundles) to verify the bundle is active. - If any dependencies are still missing, check if AEM provides them (use the OSGi console's "Packages" tab to search) or adjust the
Embed-Dependencylist accordingly.
内容的提问来源于stack exchange,提问作者Thomas

