本文开始osgi运行环境后续搭建,上文介绍了几个运行组件与容器,本文将开始具体的代码编写。


root module

打开intellij新建maven工程,待maven工程全部生成完成之后,在pom.xml中添加以下内容。

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

    <build.javadoc.version>2.9.1</build.javadoc.version>
    <build.bundle.version>2.3.7</build.bundle.version>
    <build.release.version>2.5.2</build.release.version>
    <build.resource.version>2.6</build.resource.version>
    <build.install.version>2.5.2</build.install.version>
    <build.compiler.version>3.1</build.compiler.version>
    <build.deploy.version>2.8.1</build.deploy.version>
    <build.source.version>2.2.1</build.source.version>
    <build.jar.version>2.4</build.jar.version>

    <org.slf4j.version>1.7.10</org.slf4j.version>
    <org.osgi.version>5.0.0</org.osgi.version>

    <karaf.version>4.0.4</karaf.version>
    <mysql.version>5.1.38</mysql.version>

    <environment>develop</environment>
  </properties>


  <repositories>
    <repository>
      <id>haizhi</id>
      <name>Haizhi</name>
      <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
    </repository>
  </repositories>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.14.8</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
        <version>${org.osgi.version}</version>
        <scope>provided</scope>
      </dependency>
      <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.compendium</artifactId>
        <version>${org.osgi.version}</version>
        <scope>provided</scope>
      </dependency>

      <!-- Logging -->
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j.version}</version>
      </dependency>

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>${org.slf4j.version}</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
  </dependencies>

  <build>
    <defaultGoal>install</defaultGoal>

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.karaf.tooling</groupId>
          <artifactId>karaf-services-maven-plugin</artifactId>
          <version>${karaf.version}</version>
          <executions>
            <execution>
              <id>service-metadata-generate</id>
              <phase>process-classes</phase>
              <goals>
                <goal>service-metadata-generate</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.felix</groupId>
          <artifactId>maven-bundle-plugin</artifactId>
          <version>${build.bundle.version}</version>
          <extensions>true</extensions>
          <executions>
            <execution>
              <id>bundle-manifest</id>
              <phase>process-classes</phase>
              <goals>
                <goal>manifest</goal>
              </goals>
            </execution>
          </executions>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>${build.resource.version}</version>
          <configuration>
            <includeEmptyDirs>true</includeEmptyDirs>
            <encoding>${project.build.sourceEncoding}</encoding>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <version>${build.install.version}</version>
        </plugin>

      </plugins>
    </pluginManagement>

    <plugins>
      <!-- Maven Javadoc -->

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

以上均是搭建环境所必须的相关依赖,因为本环境并不仅仅是进行几个osgi的example的编写,因此搭建环境比较复杂。在以上依赖添加完之后,还需要将pom.xml中的packaging改为pom,并且删去工程下的src文件夹,本工程为root模块,不需要写代码。


parent module

新建maven module为当前工程的子module,命名为parent,与上述module相同的是,parent同样是一个父module,因此删除目录下src文件夹,仅保留pom.xml,同时在pom.xml中添加如下内容:

 <properties>
        <docker.image.prefix>wbg.space</docker.image.prefix>

        <build.docker.version>0.3.6</build.docker.version>
        <docker.base.image>java8</docker.base.image>
        <docker.base.version>1.1.4</docker.base.version>

        <camel.version>2.15.2</camel.version>
        <pax.web.version>4.2.2</pax.web.version>
        <cxf.version>3.0.4</cxf.version>
        <shiro.version>1.2.4</shiro.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-core</artifactId>
                <version>${camel.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-blueprint</artifactId>
                <version>${camel.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-servlet</artifactId>
                <version>${camel.version}</version>
            </dependency>

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>

            <!-- Testing & Camel Plugin -->
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-test-blueprint</artifactId>
                <version>${camel.version}</version>
                <scope>test</scope>
            </dependency>

            <!-- logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.12</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.7.12</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>cn.com</groupId>
            <artifactId>features</artifactId>
            <version>1.0-SNAPSHOT</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.karaf.shell</groupId>
            <artifactId>org.apache.karaf.shell.console</artifactId>
            <version>${karaf.version}</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>framework</artifactId>
            <version>${karaf.version}</version>
            <type>kar</type>
        </dependency>
        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>standard</artifactId>
            <version>${karaf.version}</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>spring</artifactId>
            <version>${karaf.version}</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.karaf.features</groupId>
            <artifactId>enterprise</artifactId>
            <version>${karaf.version}</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.camel.karaf</groupId>
            <artifactId>apache-camel</artifactId>
            <version>${camel.version}</version>
            <classifier>features</classifier>
            <scope>runtime</scope>
            <type>xml</type>
        </dependency>
        <dependency>
            <groupId>org.apache.karaf.cellar</groupId>
            <artifactId>apache-karaf-cellar</artifactId>
            <version>4.0.0</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-features</artifactId>
            <version>${shiro.version}</version>
            <classifier>features</classifier>
            <type>xml</type>
            <scope>runtime</scope>
        </dependency>

        <!--<dependency>-->
            <!--<groupId>cn.com</groupId>-->
            <!--<artifactId>features</artifactId>-->
            <!--<version>1.0-SNAPSHOT</version>-->
            <!--<scope>runtime</scope>-->
            <!--<type>xml</type>-->
        <!--</dependency>-->
        <!-- logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <extensions>true</extensions>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>install</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>target/assembly</outputDirectory>
                                <overwrite>true</overwrite>
                                <resources>
                                    <resource>
                                        <directory>../../assembly/develop</directory>
                                        <filtering>false</filtering>
                                    </resource>
                                    <resource>
                                        <directory>src/test/resources</directory>
                                        <filtering>false</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.karaf.tooling</groupId>
                    <artifactId>karaf-services-maven-plugin</artifactId>
                    <version>${karaf.version}</version>
                    <executions>
                        <execution>
                            <id>service-metadata-generate</id>
                            <phase>process-classes</phase>
                            <goals>
                                <goal>service-metadata-generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.karaf.tooling</groupId>
                    <artifactId>karaf-maven-plugin</artifactId>
                    <version>${karaf.version}</version>
                    <extensions>true</extensions>
                    <executions>
                        <execution>
                            <id>process-resources</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>assembly</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>package</id>
                            <phase>install</phase>
                            <goals>
                                <goal>archive</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <karafDirectory>${project.build.directory}/assembly</karafDirectory>
                        <startSsh>true</startSsh>
                        <keepRunning>true</keepRunning>
                        <deployProjectArtifact>false</deployProjectArtifact>
                        <includeBuildOutputDirectory>false</includeBuildOutputDirectory>
                        <includeProjectArtifact>true</includeProjectArtifact>
                        <includeTransitiveDependency>true</includeTransitiveDependency>
                        <includeHelpOption>true</includeHelpOption>
                        <bootFeatures>
                            <feature>wrap</feature>
                            <feature>config</feature>
                            <feature>system</feature>
                            <feature>package</feature>
                            <feature>shell</feature>
                            <feature>shell-compat</feature>
                            <feature>feature</feature>
                            <feature>deployer</feature>
                            <feature>ssh</feature>
                            <feature>log</feature>
                            <feature>bundle</feature>
                        </bootFeatures>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>install</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

以上基本就是我们当前需要添加的所有依赖,在当前还添加了个别项目中的模块,这些是在后续中将添加的,当前报错的地方可以先注释起来。
并且注意maven插件中的karaf-tooling这一个插件,这个就是提供osgi的运行容器karaf,在后文的编写中,这个将起到非常重要的作用,同时maven-resources-plugin这一个插件管理我们项目中其他资源的位置,在日后的编写中也将会提到这个。当然上文中也必少不了felix这一个插件,在后文中编写felix官网中的example时,这个将不时出现。


feature module

再次在root module下新建maven工程,命名为feature module,这一个尤其重要,这个module中,将对具体的feature进行管理,feature是一批bundle的集合,同时也在管理接下来中的子module中所需的其他feature依赖和bundle。
管理这些feature需要一个单独的xml配置文件,删除src下的main文件夹,保留resource文件夹,新建xml命名为feature.xml,添加以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<features name="karaf-swagger-${project.version}"
          xmlns="http://karaf.apache.org/xmlns/features/v1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.0.0
        http://karaf.apache.org/xmlns/features/v1.0.0">


</features>

现在这个feature.xml中没有管理任何featue,在后文中将会添加其他featue进来。现在是最基本的配置。
在新建完feature之后,在pom文件中添加以下内容:


    <properties>
        <deploy.file>${project.build.directory}/classes/features.xml</deploy.file>
    </properties>

    <build>
        <defaultGoal>install</defaultGoal>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>filter</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.9.1</version>
                <executions>
                    <execution>
                        <id>attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>${deploy.file}</file>
                                    <type>xml</type>
                                    <classifier>features</classifier>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

以上内容是指定feature.xml文件位置以及相应对该文件进行操作的插件,在以上完成之后,需要改当前pom的parent为parent module,同时在parent module中添加module为feature。


总结

以上完成之后,基本的环境搭建基本就完成了,具体的编码module的创建,还有karaf输出日志的的规定,将在下次博文中编写,本文仅只搭建基本的运行环境。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐