AEM 6.3中如何自动生成OSGi SCR元数据并解决OSGi R6类单元测试问题?
Hey there! I’ve dealt with this exact frustration before when testing OSGi components with R6 annotations in AEM—having to manually run the bundle:manifest goal every time after clearing IntelliJ’s cache is a total pain. Let’s go through a few solid ways to make this happen automatically:
1. Bind the manifest Goal to a Maven Test Lifecycle Phase
The most robust fix is to configure your pom.xml to run the manifest goal automatically as part of the Maven test lifecycle. This way, every time you run tests (whether via Maven command line or IntelliJ), the SCR metadata gets generated before tests execute.
Add this configuration to your maven-bundle-plugin setup in your pom.xml:
<build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>3.5.0</version> <!-- Use a version compatible with AEM 6.3 --> <executions> <execution> <id>auto-generate-scr-metadata</id> <phase>process-test-classes</phase> <goals> <goal>manifest</goal> </goals> </execution> </executions> <configuration> <instructions> <!-- Ensure your OSGi R6 annotations are processed --> <_dsannotations>*</_dsannotations> <!-- Add your other bundle instructions here --> </instructions> </configuration> </plugin> </plugins> </build>
The process-test-classes phase runs right after your test classes are compiled and before tests start—perfect timing to generate the required SCR metadata.
2. Configure IntelliJ’s Test Run Configurations
If you prefer to keep Maven config minimal or need a per-test configuration fix, you can set up IntelliJ to run the bundle:manifest goal automatically before executing your tests:
- Open Run > Edit Configurations
- Find your JUnit test configuration (or create a new one)
- In the Before launch section, click the
+button and select Run Maven Goal - Select your project module, then enter
bundle:manifestas the goal - Click OK to save the configuration
Now every time you run this test configuration, IntelliJ will first execute the manifest goal, ensuring your SCR metadata is up to date.
3. Ensure IntelliJ Syncs with Maven Properly
Sometimes IntelliJ doesn’t pick up Maven lifecycle changes automatically, so double-check these settings:
- Go to File > Settings > Build, Execution, Deployment > Build Tools > Maven > Importing
- Make sure Import Maven projects automatically is checked
- In the Maven tool window (right side of IntelliJ), click the Reimport All Maven Projects button to sync your pom.xml changes
Quick Check
Don’t forget to verify that your maven-bundle-plugin is correctly processing OSGi R6 annotations—adding <_dsannotations>*</_dsannotations> to the instructions ensures bnd generates the necessary SCR metadata for your annotated components.
内容的提问来源于stack exchange,提问作者Jdruwe




