Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅在Eclipse中将带有捆绑包的外部JAR添加到项目中_Java_Eclipse_Osgi - Fatal编程技术网

Java 仅在Eclipse中将带有捆绑包的外部JAR添加到项目中

Java 仅在Eclipse中将带有捆绑包的外部JAR添加到项目中,java,eclipse,osgi,Java,Eclipse,Osgi,有没有一种方法可以添加带有捆绑包的外部JAR,而无需编辑目标平台,只需为当前项目定义这些JAR。我在全局定义错误的插件时遇到了一些错误 作为参考,我使用了这个答案:我曾经尝试使用目标平台来实现这一点。但是eclipse PDE非常混乱和奇怪 今天,我使用Maven Bundle插件来管理我的OSGi项目。除非您真的需要使用eclipse插件/平台,并且没有其他选择,否则我建议您使用felix maven捆绑插件()尝试maven 我这样做的方式是创建一个主项目,其中包含bundle。 以下是如何

有没有一种方法可以添加带有捆绑包的外部JAR,而无需编辑目标平台,只需为当前项目定义这些JAR。我在全局定义错误的插件时遇到了一些错误


作为参考,我使用了这个答案:

我曾经尝试使用目标平台来实现这一点。但是eclipse PDE非常混乱和奇怪

今天,我使用Maven Bundle插件来管理我的OSGi项目。除非您真的需要使用eclipse插件/平台,并且没有其他选择,否则我建议您使用felix maven捆绑插件()尝试maven

我这样做的方式是创建一个主项目,其中包含bundle。 以下是如何从头开始创建此类项目:

首先,在EclipseKepler(或更高版本)中,通过执行File->new->maven project创建一个新的maven项目。 在显示的新窗口中,选择要将项目保存到的文件夹,它可以是您的工作区或任何其他文件夹

选择选项“创建简单项目(跳过类型选择)”,然后按“下一步”。 填写组id名称和工件id名称。这些参数用于在maven存储库中标识您的项目,其中每个项目都由一对表示(不要问我为什么他们选择这种方式命名项目)。你想给他们起什么名字都行。我只是在我的工件id后面加了“.master”,以强调这个项目只是一个父POM。父POM是一个POM.XML文件,其中包含我们要开发的所有捆绑包所共有的所有信息

按完成。 现在,如果您查看POM.XML,您将看到很少几行包含一些无用信息以及我们的组和工件ID

<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>
 <groupId>such.osgi.example</groupId>
 <artifactId>very.example.master</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 </project>
注意构建成功部分,这非常重要,这意味着到目前为止一切都正常! 现在,如果您尝试运行As->Maven Build…并键入-Pcreate osgi bundles from dependencies bundle:wrap在显示的窗口中的“Goals”字段中,然后点击Run,您将让Maven下载这3个依赖项(junit和org.orgi.*),并从中创建bundle(此操作称为wrapping As a bundle)。它将把这些包放在项目的bundles/目录中:

如果在您的项目中还看到maven依赖项或其他文件夹,请不要担心。有时日食会把它们放在那里,有时不会,就像魔法一样。 如果遇到任何奇怪的问题,只需选择项目并转到Maven->Update Maven project,选择Force Update of Snapshot/Releases,确保在上面的列表中选择了您的项目,然后点击Ok

现在是创建捆绑包的时候了。为了简单起见,我们只创建一个简单的包,因此最简单的方法是在项目中创建一个新文件夹,并将其命名为包,例如:

现在,在该文件夹内创建一个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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <parent>
            <relativePath>../pom.xml</relativePath>
            <groupId>such.osgi.example</groupId>
            <artifactId>very.example.master</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>

        <artifactId>very.example.mybundles.helloworldbundle</artifactId>
        <packaging>bundle</packaging>

        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <extensions>true</extensions>
                    <configuration>
                        <instructions>
                            <Bundle-SymbolicName>${project.artifactId};singleton:=true</Bundle-SymbolicName>
                            <Bundle-Version>${project.version}</Bundle-Version>
                            <Export-Package>very.example.mybundles.helloworldbundle</Export-Package>
                            <Bundle-Activator>very.example.mybundles.helloworldbundle.Activator</Bundle-Activator>
                        </instructions>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

        <name>${project.artifactid}</name>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.zeromq</groupId>
                <artifactId>zeromq-scala-binding_2.10</artifactId>
                <version>0.0.7</version>
            </dependency>
        </dependencies>
    </project>
到目前为止,您的项目应该没有错误,并且应该可以完美地运行maven安装! 每次让maven安装时,它都会在目标/文件夹中生成一个.jar。jar是您可以部署到OSGi框架的项目包。但是现在您也希望将所有相关依赖项生成为捆绑包,以便它们也可以进入OSGi框架运行时。对于我们的示例,我在这个bundle POM文件中放了一个依赖项,它与我们的实际代码无关,它不是必需的,但我放在那里是为了向您展示如果您对这个bundle有依赖项,maven将如何工作(其他的依赖项是java.lang,已经包含在任何java运行时环境中)。 选择主项目并转到RunAs->Maven Build…并键入-Pcreate osgi bundles from dependencies bundle:在显示的窗口中的“Goals”字段中换行,然后点击Run,您将使Maven从父/主项目及其bundle下载所有依赖项(在我们的情况下,我们只有一个bundle)并从中创建包。 现在,您应该在每个项目(master和helloworldbundle)的bundles/文件夹中拥有您的bundle。现在下载FelixOSGi框架并将所有这些jar,以及捆绑包目标/目录中的jar文件复制到FelixFramework捆绑包/文件夹中

注意:每次运行felix时,您都可以删除缓存文件夹以完全重置框架,以防更新某些包并损坏运行时。 现在打开cmd,转到felix根路径并键入:java–jar bin\felix.jar 您应该会看到一条巨大的错误消息。这是因为我们在bundle的pom文件中添加了示例依赖项。因此,让我们删除它,因为它只是一个例子。 因此,您需要删除以下内容:

    <dependencies>
            <dependency>
                <groupId>org.zeromq</groupId>
                <artifactId>zeromq-scala-binding_2.10</artifactId>
                <version>0.0.7</version>
            </dependency>
        </dependencies>

org.zeromq

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

        <parent>
            <relativePath>../pom.xml</relativePath>
            <groupId>such.osgi.example</groupId>
            <artifactId>very.example.master</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>

        <artifactId>very.example.mybundles.helloworldbundle</artifactId>
        <packaging>bundle</packaging>

        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <extensions>true</extensions>
                    <configuration>
                        <instructions>
                            <Bundle-SymbolicName>${project.artifactId};singleton:=true</Bundle-SymbolicName>
                            <Bundle-Version>${project.version}</Bundle-Version>
                            <Export-Package>very.example.mybundles.helloworldbundle</Export-Package>
                            <Bundle-Activator>very.example.mybundles.helloworldbundle.Activator</Bundle-Activator>
                        </instructions>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

        <name>${project.artifactid}</name>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.zeromq</groupId>
                <artifactId>zeromq-scala-binding_2.10</artifactId>
                <version>0.0.7</version>
            </dependency>
        </dependencies>
    </project>
    <modules>
        <module>very.example.mybundles.helloworldbundle</module>
    </modules>
    package very.example.mybundles.helloworldbundle;

    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;

    public class Activator implements BundleActivator {

        @Override
        public void start(BundleContext arg0) throws Exception {
            System.out.println("hello world!");
        }

        @Override
        public void stop(BundleContext arg0) throws Exception {
            System.out.println("bye!");
        }

    }
    <dependencies>
            <dependency>
                <groupId>org.zeromq</groupId>
                <artifactId>zeromq-scala-binding_2.10</artifactId>
                <version>0.0.7</version>
            </dependency>
        </dependencies>