3

When I run

mvn clean install

for my maven module then it compiles fine. No issues. But when I open my pom.xml file in IntelliJ and I choose to Build -> Build module then I get following issues:

Information:javac 1.8.0_144 was used to compile java sources
Information:Module "mymodule" was fully rebuilt due to project configuration/dependencies changes
Information:09.10.2017 21:16 - Compilation completed with 3 errors and 3 warnings in 23s 991ms
C:\somepath\mymodule\pom.xml
Error:Error:osgi: [mymodule] Exception: java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin not found, parent:  java.net.URLClassLoader@29453f44 urls:[] exception:java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin
Error:Error:osgi: [mymodule] Failed to load plugin org.apache.sling.bnd.models.ModelsScannerPlugin;generatePackagesHeader=true, error: java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin not found, parent:  java.net.URLClassLoader@29453f44 urls:[] exception:java.lang.ClassNotFoundException: org.apache.sling.bnd.models.ModelsScannerPlugin 
Error:Error:osgi: [mymodule] Cannot load the plugin org.apache.sling.bnd.models.ModelsScannerPlugin

This is a module with AEM code and it uses maven-sling-plugin. It works fine for other developers in the project. Because it's working when executed directly from maven I'm trying to understand what IntelliJ does in the background. But actually, my problem is those compilation issues.

From what I've found IntelliJ does not call maven when Build is done. Any ideas how can I find differences between running from IntelliJ and directly from Maven?

Maciej Dybek
  • 186
  • 8

2 Answers2

2

IntelliJ Build Error Screenshot

What happens here is that the ModelScanner plugin can't be found using the current ClassLoader. The reason for this can be that you are using IntelliJ IDEA Ultimate which comes with a OSGI plugin already pre-installed called 'Osmorc'. If this OSGI plugin is active it will determine the classloader to be used for building OSGI related projects.

So simply de-activating this Osmorc plugin in IntelliJ should allow your build to revert to the classloader from the ModelScannerPlugin mentioned in the configuration of your the maven-bundle-plugin in your projects POM.xml file which should solve the problem.

IntelliJ Osmorc Plugin

If this still results in a similar Maven build error, then make sure to add a Maven dependency 'org.apache.sling.bnd.model' to your maven-bundle-plugin in your POM.xml file.

<!-- Apache Felix Bundle Plugin -->
<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>3.3.0</version>
    <inherited>true</inherited>
    <extensions>true</extensions>
    <executions>
        <!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs -->
        <execution>
            <id>scr-metadata</id>
            <goals>
                <goal>manifest</goal>
            </goals>
            <configuration>
                <supportIncrementalBuild>true</supportIncrementalBuild>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <exportScr>true</exportScr>
        <instructions>
            <!-- Enable processing of OSGI DS component annotations -->
            <_dsannotations>*</_dsannotations>
            <!-- Enable processing of OSGI metatype annotations -->
            <_metatypeannotations>*</_metatypeannotations>
            <_plugin>org.apache.sling.bnd.models.ModelsScannerPlugin;generatePackagesHeader=true</_plugin>
        </instructions>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.bnd.models</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</plugin>
  • Cheers! That was causing me some grief. In my case, it wasn't the Osmorc plugin but I had two other OSGi plugins installed. Disabling them allowed me to trigger tests directly from the IDE. – toniedzwiedz Oct 23 '20 at 14:54
0

could you please check your core pom file. it should contain a plugin section like this:

<plugin> <!-- Enable registration of Sling Models classes via bnd plugin --> org.apache.sling.bnd.models.ModelsScannerPlugin, <!-- Allow the processing of SCR annotations via a bnd plugin --> org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin;destdir=${project.build.outputDirectory} </plugin>

but if you created a project using aem archetype the tag looks like' <_plugin>

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54