我有一个多模块maven OSGi项目。我正在使用maven-assembly-plugin将不同的jar组织到一个中央文件夹中,OSGi容器将从中加载各种项目模块:
dist pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>dist</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>distro-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/bin.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>rev</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
</project>
模块罐根据我的意愿放入中央文件夹。然而,随着时间的推移,我无法真正跟踪模块依赖关系如何相互关联。例如,某个模块可能需要启动另一个模块才能正确执行。我怎么能保证在 module-B 开始之前,首先启动 module-A - 我想以某些代码处理的方式配置它执行顺序?
这是我执行此类执行顺序不正确的错误。我不认为捆绑包已安装。
Exception in thread "main" org.osgi.framework.BundleException: Unable to resolve OSGiDmHelloWorldConsumer [2](R 2.0): missing requirement [OSGiDmHelloWorldConsumer [2](R 2.0)] osgi.wiring.package; (&(osgi.wiring.package=com.bw.osgi.provider.able)(version>=1.0.0)(!(version>=2.0.0))) Unresolved requirements: [[OSGiDmHelloWorldConsumer [2](R 2.0)] osgi.wiring.package; (&(osgi.wiring.package=com.bw.osgi.provider.able)(version>=1.0.0)(!(version>=2.0.0)))]
at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4111)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2117)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)
at main.App.initialize(App.java:46)
at main.App.main(App.java:22)
以下是引发错误的类public class App {}
:
应用
public class App {
public static void main(String[] args) throws BundleException, URISyntaxException {
App app = new App();
app.initialize();
}
private void initialize() throws BundleException, URISyntaxException {
Map<String, String> map = new HashMap<String, String>();
// make sure the cache is cleaned
map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
map.put("ds.showtrace", "true");
map.put("ds.showerrors", "true");
FrameworkFactory frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
Framework framework = frameworkFactory.newFramework(map);
System.out.println("Starting OSGi Framework");
framework.init();
loadScrBundle(framework);
String baseDir = "/D:/Maven-Assembly-Plugin-MM/dist/target/dist-1.0-SNAPSHOT-bin/plugins/";
framework.getBundleContext().installBundle("file:" + baseDir + "core-1.0.jar");
framework.getBundleContext().installBundle("file:" + baseDir + "clientfile-plugin-1.0-SNAPSHOT.jar");
framework.getBundleContext().installBundle("file:" + baseDir + "dist-1.0-SNAPSHOT.jar");
List<Bundle> bundles = new ArrayList<Bundle>();
for (Bundle bundle : framework.getBundleContext().getBundles()) {
bundle.start();
bundles.add(bundle);
System.out.println("Bundle Name: " + bundle.getSymbolicName());
System.out.println("Bundle ID: " + bundle.getBundleId());
if (bundle.getRegisteredServices() != null) {
for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
System.out.println("\tRegistered service: " + serviceReference);
}
}
System.out.println("Total Bundles: " + bundles.size());
}
private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
if (url == null)
throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
System.out.println("Found declarative services implementation: " + jarPath);
framework.getBundleContext().installBundle(jarPath).start();
}
}
我该如何解决这个问题?提前谢谢大家。
更新
模块罐根据我的意愿放入中央文件夹。但是,当我尝试在调用mvn clean install之后运行项目时,除了felix模块之外的所有模块都会出现以下错误,即来自中央maven存储库的所有模块,例如 org.apache.felix。框架和 org.apache.felix.scr 在OSGi容器中运行,除了我自己写的那些。
更详细的问题
我已经更详细地发布了问题项目的一个非常简短的版本HERE, Maven-Assembly-Plugin-MM。 This, OSGi - Simple Hello World with services,是我遵循的教程。
蚀:
导入&gt;现有的Maven项目&gt; C:\ *** ***路径\ Maven的汇编插件-MM
答案 0 :(得分:1)
捆绑加载顺序在OSGi应用程序中无关紧要。
但OSGi服务可能依赖于其他服务。
您可以使用诸如Declarative Services之类的框架来轻松管理依赖关系(例如,使用SCR annotations)。
您需要以下插件:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-scr-plugin</artifactId>
<executions>
<execution>
<id>generate-scr-scrdescriptor</id>
<goals>
<goal>scr</goal>
</goals>
</execution>
</executions>
</plugin>
以下依赖项:
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
<!-- only needed at compile time, not at runtime -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr</artifactId>
<scope>runtime</scope>
</dependency>