【问题标题】:How to import a class from third party jar file in an OSGi component如何从 OSGi 组件中的第三方 jar 文件导入类
【发布时间】:2015-10-02 11:04:35
【问题描述】:

我正在使用https://eclipse.adobe.com/aem/dev-tools/ 创建一个项目。创建后,我在 Eclipse 中具有以下结构:

我希望能够在我的组件中使用 GoogleMaps API。所以我在hometest.core/pom.xml中添加了它的依赖项

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

我还在hometest.core/pom.xml_exportcontents 中添加了所有内容

        <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>

然后我将com.google.maps.model.GeocodingResult导入HelloServiceProxy.java,如下图:

我使用 mvn clean install -PautoInstallPackage 将包安装到 aem 的本地实例

但是,当我尝试将组件添加到页面时,我收到以下错误:

java.lang.Error: Unresolved compiler problem: Only a type can be 进口的。 com.google.maps.model.GeocodingResult 解析为一个包

以下是错误截图:

更新 1

我从另一个全新的 AEM 项目开始,做了以下事情:

  • 在 core/pom.xml 中添加了 maven-bundle-plugin 的配置设置,如下所示

        <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>
    
  • 像这样添加了谷歌地图依赖:

    <dependency>
    <groupId>com.google.maps</groupId>
    <artifactId>google-maps-services</artifactId>
    <version>0.1.7</version>
    <scope>compile</scope>
    </dependency>
    
  • 使用此mvn clean install -PautoInstallPackage进行部署

当我尝试向页面添加组件时出现错误:

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

【问题讨论】:

  • 我以前见过这个问题。有时,强制重新编译视图或 ui 的表面更改会有所帮助。我对它不是很了解。但可能是核心和ui包中的依赖项试图使用ui jsp编译时未注册的服务
  • @ChristopherRockwell 你是怎么解决的?
  • 您可能对我最近发布的stackoverflow.com/questions/31191299/google-client-api-in-osgi 的问题感兴趣,但我没有导出罐子,只是嵌入。所以它们只能在服务/核心包中使用。 .... 另外关于“只能导入一个类型”。我之前通过仅重新部署视图“解决”了这个问题,在另一种情况下,只是在 JSP 中保存了一个空白更改(强制重新编译)。但如果是我,我会嵌入(而不是导出)罐子,并尽可能在同一个包中使用它们,即使这不是真正的 osgi 方式
  • 你能解决这个问题吗?

标签: java maven osgi aem aem-developer-tools


【解决方案1】:

错误表明google-maps-services 包中的类不适用于hometest.core 包。很可能此时嵌入包不起作用。

您能否尝试部署一个嵌入 google-maps-services 的单独捆绑包,看看是否可行?

【讨论】:

  • 我尝试了另一个捆绑包并更新了我在问题中所做的事情。错误和以前一样..
  • 你能打开 hometest 包的 jar 内容,看看它的 manifest.mf 是否在其“Import-Package”标头中列出了 google-maps-services 包。如果它有一个正确的版本,那么检查谷歌包的导出包部分中是否存在相同的包。如果这不起作用,请尝试使用 maven-bundle-plugin 的 属性,而不是使用
  • @Anthony 你能清除 jsp 编译错误吗?
  • 抱歉,我错过了更新。我的意思是使用 mvn sling:install 或 Felix Web 控制台部署另一个包,然后重试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
相关资源
最近更新 更多