当前位置 博文首页 > 莫忘、初心:karaf + osgi +maven 框架搭建

    莫忘、初心:karaf + osgi +maven 框架搭建

    作者:[db:作者] 时间:2021-08-04 21:57

    配置环境:

    maven:3.6
    jdk:1.8
    

    karaf-osgi

    首先我们需要新建一个总项目,此项目里面没有任何的代码,只是在pom文件中进行版本控制

    新建项目的时候不需要选择任何的框架,需要的时候再添加依赖和插件即可
    注意karaf项目是需要继承apache-karaf-examples的,要不然会产生很多不必要的坑,解决起来很耗费时间’

    apache-karaf-examples实际上是官方出的实例

    <?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>
    
        <parent>
            <groupId>org.apache.karaf.examples</groupId>
            <artifactId>apache-karaf-examples</artifactId>
            <version>4.2.3</version>
        </parent>
    
        <groupId>com.karaf.osgi</groupId>
        <artifactId>karaf-osgi</artifactId>
        <version>1.0.0</version>
        <packaging>pom</packaging>
        <name>Karaf :: Osgi</name>
    
    
        <modules>
            <module>karaf-osgi-features</module>
        </modules>
    
    </project>
    

    karaf-osgi-features

    现在让我们来新建一个feature子项目来管理osgi的bundle

    为了显得专业一些,我们把src/main下方的java文件夹删除,resource修改为feature
    feature文件夹下新建xml为feature.xml,

    feature.xml内容如下:
    注意标签features 中的name值

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

    feature服务的pom文件配置如下,我们要配置下feature.xml的路径,在下方build标签内添加resource

     <resources>
                <resource>
                    <directory>src/main/feature</directory>
                    <filtering>true</filtering>
                    <targetPath>${project.build.directory}/feature</targetPath>
                </resource>
     </resources>
    

    在下方插件build-helper-maven-plugin中的标签artifact内配置feature.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">
        <parent>
            <artifactId>karaf-osgi</artifactId>
            <groupId>com.karaf.osgi</groupId>
            <version>1.0.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.karaf.osgi</groupId>
        <artifactId>karaf-osgi-features</artifactId>
        <packaging>pom</packaging>
        <name>Karaf :: Osgi :: Features</name>
    
        <build>
            <resources>
                <resource>
                    <directory>src/main/feature</directory>
                    <filtering>true</filtering>
                    <targetPath>${project.build.directory}/feature</targetPath>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>resources</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>attach-artifacts</id>
                            <phase>package</phase>
                            <goals>
                                <goal>attach-artifact</goal>
                            </goals>
                            <configuration>
                                <artifacts>
                                    <artifact>
                                        <file>target/feature/feature.xml</file>
                                        <type>xml</type>
                                    </artifact>
                                </artifacts>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    
    cs