【发布时间】:2017-06-27 23:25:28
【问题描述】:
我正在开发一个 Hello World 应用程序。我的应用程序是用 Maven (3.5.0) 构建的,使用 Apache Felix 注释,并在 Apache Karaf (4.1.1) 中运行。我的应用程序由一个名为 App 的组件组成,应该立即启动。捆绑包构建成功。我可以从我的 mvn 存储库成功地将它安装到 Karaf 中。 Karaf 将捆绑包显示为“活动”。问题是我的组件(App)的构造函数和激活方法从未被调用。我需要第二双眼睛来帮助我弄清楚为什么会发生这种情况。我的 pom.xml 中缺少什么?
为了完整起见,我在我的项目中创建了一个实现 BundleActivator 的 Activator 类。然后我指示 Maven 将我的 Bundle-Activator 设置为这个新类。现在,当我在 Karaf 中安装我的包时,我可以看到我的 Activator 的日志输出。 start 方法被击中。所以我知道我的捆绑包实际上已经开始了。我只是不明白为什么我的 App 组件从未创建和激活。
这里是相关文件。
App.java
package myCompany;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
@Component(immediate=true)
public class App
{
public App()
{
System.out.println( "App constructed" );
}
@Activate
public void activate()
{
System.out.println( "App activated" );
}
}
Activator.java
package myCompany;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator{
public void start(BundleContext context) throws Exception {
System.out.println("Activator started");
}
public void stop(BundleContext context) throws Exception {
System.out.println("Activator stopped");
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myCompany</groupId>
<artifactId>myProject</artifactId>
<packaging>bundle</packaging>
<version>1.0-SNAPSHOT</version>
<name>myProject</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<version>1.9.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<version>1.24.0</version>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>3.3.0</version>
<configuration>
<instructions>
<_dsannotations>*</_dsannotations>
<_metatypeannotations>*</_metatypeannotations>
<Bundle-Activator>myCompany.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
【问题讨论】:
标签: java maven osgi apache-karaf