简体   繁体   中英

How to import a class from third party jar file in an OSGi component

I am using https://eclipse.adobe.com/aem/dev-tools/ to create a project. Once created, I have the following structure in eclipse:

在此输入图像描述

I want to be able to use the GoogleMaps API in my component. So I add the dependency for it in hometest.core/pom.xml

<dependency>
    <groupId>com.google.maps</groupId>
    <artifactId>google-maps-services</artifactId>
    <version>0.1.7</version>
    <scope>compile</scope>
</dependency>

I've also added everything to the _exportcontents in hometest.core/pom.xml

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                    <Embed-Directory>OSGI-INF/lib</Embed-Directory>
                    <_exportcontents>
                                    *
                    </_exportcontents>
                </instructions>
            </configuration>
        </plugin>

I then import com.google.maps.model.GeocodingResult into HelloServiceProxy.java as shown below:

在此输入图像描述

I install the package to local instance of aem using mvn clean install -PautoInstallPackage

However, when I try to add the component to the page I get the following error:

java.lang.Error: Unresolved compilation problem: Only a type can be imported. com.google.maps.model.GeocodingResult resolves to a package

Below is screenshot of the error:

在此输入图像描述

Update 1

I started with another brand new AEM project and did the following things:

  • in core/pom.xml added configuration settings for maven-bundle-plugin like this

      <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency> <Embed-Directory>OSGI-INF/lib</Embed-Directory> <_exportcontents> * </_exportcontents> </instructions> </configuration> </plugin> 
  • Added google maps dependency like this:

     <dependency> <groupId>com.google.maps</groupId> <artifactId>google-maps-services</artifactId> <version>0.1.7</version> <scope>compile</scope> </dependency> 
  • deployed with this mvn clean install -PautoInstallPackage

When I try to add component to the page I get errors:

java.lang.Error: Unresolved compilation problems: 
    Only a type can be imported. com.google.maps.model.GeocodingResult resolves to a package
    Only a type can be imported. org.demo.anothertest.core.HelloService resolves to a package
    HelloService cannot be resolved to a type
    HelloService cannot be resolved to a type

The error indicates that the classes from google-maps-services bundle are not available to the hometest.core bundle. It might very well be that embedding bundles does not work at this point.

Can you try deploying a separate bundle which embeds google-maps-services and see if that works?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM