簡體   English   中英

Apache Maven Assembly Plugin無法與OSGi捆綁包一起使用

[英]Apache Maven Assembly Plugin not working with OSGi bundles

我有一個Maven OSGi多模塊項目。 當OSGi從各個項目模塊中選擇模塊jar時,該項目將運行良好。 (請參見下面的1.1.B)

但是,使用第二種方法時,每當我嘗試使用通過maven-assembly-plugin存放在中央文件夾(D:/ parent / provider / target / modules)中的分發包時, bundle.getRegisteredServices() (請參見下面的1.1.A)都將返回null。 版本:2.6

framework.getBundleContext().installBundle("file:D:/parent/provider/target/modules/OSGiDmHelloWorldProvider-1.0.jar");
framework.getBundleContext().installBundle("file:D:/parent/provider/target/modules/OSGiDmHelloWorldConsumer-1.0.jar");

使用第二種方法查看下面的1.1.C以獲得控制台輸出。

1.1.A

if (bundle.getRegisteredServices() != null) {
    for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
        System.out.println("\tRegistered service: " + serviceReference);
}

為什么我不能使用第二種方法訪問捆綁軟件?

GitHub上

我在GitHub HERE上有一個SSCCE。 參加主要班將顯示我的困境。

謝謝大家。

1.1.B

package main;

import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

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

        framework.getBundleContext().installBundle("file:D:/parent/provider/target/OSGiDmHelloWorldProvider-1.0.jar");
        framework.getBundleContext().installBundle("file:D:/parent/consumer/target/OSGiDmHelloWorldConsumer-1.0.jar");

        for (Bundle bundle : framework.getBundleContext().getBundles()) {
            bundle.start();
            System.out.println("Bundle: " + bundle.getSymbolicName());
            if (bundle.getRegisteredServices() != null) {
                for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
                    System.out.println("\tRegistered service: " + serviceReference);
            }
        }
    }

    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();
    }
}

1.1.C

Starting OSGi Framework
Found declarative services implementation: file:/C:/Users/Revilo/.m2/repository/org/apache/felix/org.apache.felix.scr/1.6.2/org.apache.felix.scr-1.6.2.jar
INFO : org.apache.felix.scr (1):  Version = 1.6.2
DEBUG: Starting ComponentActorThread
Bundle: org.apache.felix.framework
    Registered service: [org.osgi.service.resolver.Resolver]
    Registered service: [org.osgi.service.packageadmin.PackageAdmin]
    Registered service: [org.osgi.service.startlevel.StartLevel]
Bundle: org.apache.felix.scr
    Registered service: [org.apache.felix.scr.ScrService]
    Registered service: [org.osgi.service.cm.ManagedService]
    Registered service: [org.apache.felix.scr.impl.ScrGogoCommand]
Bundle: null
Bundle: null

我不得不做很多事情來使您的樣本重復這個問題。

首先,您的反應堆訂單在父母中是錯誤的。 這就是為什么您必須一直進行mvn安裝的原因。

<modules>
    <module>OSGiDmHelloWorldProvider</module>
    <module>OSGiDmHelloWorldConsumer</module>
    <module>main</module>
    <module>dist</module>
</modules>

接下來,如果您在父級中定義了一個依賴項(例如JUnit),則不需要在子級中重新定義它。

接下來,通常將父標簽放在pom的頂部。

我看不出有一個讓您的子模塊與父模塊具有不同版本的原因,因此我刪除了標簽,使它們都具有來自父模塊的1.0-SNAPSHOT

接下來,您在OSGiDmHelloWorldProvider依賴項中具有錯誤的組ID(應為rev)。

    <dependency>
        <groupId>rev</groupId>
        <artifactId>OSGiDmHelloWorldProvider</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

在主模塊中,您有一個不在反應堆中的依賴項。 我猜這只是對樣本的疏忽。

    <dependency>
        <groupId>rev</groupId>
        <artifactId>core</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

畢竟, mvn clean package -DskipTests=true可以正常工作。

您的Main類中有一個硬編碼的字符串,顯然對我不起作用。 (您可能還想看看免費的IDEA社區而不是Eclipse!)

String baseDir = "D:/standAloneDev/java/workingDir/Sample Projects/Eclipse/Gen/OSGiDmHelloWorld/dist/target/dist-1.0-SNAPSHOT-bin/plugins/";

您應該將此相對。 例如

File baseDir = new File("dist/target/dist-1.0-SNAPSHOT-bin/plugins/");
String baseDirPath = baseDir.getAbsolutePath();

loadScrBundle(framework);

File provider = new File(baseDirPath, "OSGiDmHelloWorldProvider-1.0-SNAPSHOT.jar");
File consumer = new File(baseDirPath, "OSGiDmHelloWorldConsumer-1.0-SNAPSHOT.jar");

framework.getBundleContext().installBundle(provider.toURI().toString());
framework.getBundleContext().installBundle(consumer.toURI().toString());

無論如何,得到它之后,我在bundle.getSymbolicName()上注意到了以下javadoc。

Returns the symbolic name of this bundle as specified by its Bundle-SymbolicName manifest header. The bundle symbolic name should be based on the reverse domain name naming convention like that used for java packages.

因此,在org.apache.felix.scr-1.6.2.jar的MANIFEST.MF中

Bundle-Name: Apache Felix Declarative Services
Bundle-SymbolicName: org.apache.felix.scr

您沒有這個,因為您沒有創建清單並將其添加到jar中。

您需要添加一個執行階段,並告訴jar插件使用清單:

        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <executions>
                <execution>
                    <id>bundle-manifest</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>manifest</goal>
                    </goals>
                </execution>
            </executions>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>OSGiDmHelloWorldProvider</Bundle-SymbolicName>
                    <Export-Package>com.bw.osgi.provider.able</Export-Package>
                    <Bundle-Activator>com.bw.osgi.provider.ProviderActivator</Bundle-Activator>
                    <Bundle-Vendor>Baptiste Wicht</Bundle-Vendor>
                </instructions>
            </configuration>
        </plugin>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM