Intellij Idea 15中开发Maven+osgi项目(Apache felix环境)

1.Intellij Idea创建Maven项目



利用maven模板快速创建maven项目,如图操作,填写g(groupid)a(artifactid)v(version)。

maven home directory 选择系统默认或者自己下载下来的maven主目录。

2.将项目模块化

利用maven的依赖以及集成的特性,将项目模块化。

在主module下创建module并继承主module。

这里我创建了server以及client,项目结构如下图,先忽略红圈之外的文件夹:



主pom配置如下:

<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>com.renming.osgi.helloworld</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0.0</version>
    <modules>
        <module>server</module>
        <module>client</module>
    </modules>
    <packaging>pom</packaging>

    <name>helloworld</name>
    <url>http://maven.apache.org</url>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.renming.osgi.helloworld</groupId>
                <artifactId>server</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse</groupId>
                <artifactId>osgi</artifactId>
                <version>3.9.1-v20130814-1242</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

server模块pom配置如下:

<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>helloworld</artifactId>
        <groupId>com.renming.osgi.helloworld</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>server</artifactId>
    <packaging>bundle</packaging>

    <name>server</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <dependency>
            <groupId>org.eclipse</groupId>
            <artifactId>osgi</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-SymbolicName>$(replace;${project.artifactId};-;_)</Bundle-SymbolicName>
                        <Export-Package>
                            com.renming.osgi.helloworld.server.inter;version="${project.version}"
                        </Export-Package>
                        <Import-Package>
                            org.osgi.framework
                        </Import-Package>
                        <Bundle-Activator>
                            com.renming.osgi.helloworld.Activator
                        </Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

client模块pom配置如下:

<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>helloworld</artifactId>
        <groupId>com.renming.osgi.helloworld</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>client</artifactId>
    <packaging>bundle</packaging>

    <name>client</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <dependency>
            <groupId>com.renming.osgi.helloworld</groupId>
            <artifactId>server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse</groupId>
            <artifactId>osgi</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.4.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-SymbolicName>$(replace;${project.artifactId};-;_)
                        </Bundle-SymbolicName>
                        <Import-Package>
                            org.osgi.framework,com.renming.osgi.helloworld.server.inter;version="${project.version}"
                        </Import-Package>
                        <Bundle-Activator>
                            com.renming.osgi.helloworld.Activator
                        </Bundle-Activator>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

主pom和子pom拥有继承关系,同时进行了模块化。

3.编写简单的测试用例

通过实现BundleActivator接口,可以实现与osgi框架环境的通信。

这里需要实现start以及stop,分别在bundle启动以及终止的时候被调用。

这里让它在server bundle启动的时候注册一个简单的服务:

package com.renming.osgi.helloworld;

import java.util.ArrayList;
import java.util.List;

import com.renming.osgi.helloworld.server.impl.HelloImpl;
import com.renming.osgi.helloworld.server.inter.Hello;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class Activator implements BundleActivator {

    private List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();

    private static BundleContext context;

    static BundleContext getContext() {
        return context;
    }

    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        System.out.println("----------------hello start---------------------");
        //注册hello接口中的服务
        registrations.add(bundleContext
                .registerService(Hello.class.getName(), new HelloImpl("Hello, OSGi"), null));
        System.out.println("----------------hello start---------------------");
    }

    public void stop(BundleContext bundleContext) throws Exception {
        Activator.context = null;

        for (ServiceRegistration registration : registrations) {
            System.out.println("unregistering: " + registration);
            registration.unregister();
        }

    }

}

需要注意的是编写完代码后,需要配置server模块pom中打包插件,完整的上面已经列出了。

然后是cilent模块中测试代码,需要从bundleContext中获取到对应的服务,只需要提供服务名就可以了。具体代码如下:

package com.renming.osgi.helloworld;


import com.renming.osgi.helloworld.server.inter.Hello;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class Activator implements BundleActivator {

	public void start(BundleContext ctx) {
		System.out.println("----------------hello client start---------------------");
	    ServiceReference ref = ctx.getServiceReference(Hello.class.getName());
	    if (ref != null) {
	        Hello hello = null;
	        try {
	            hello = (Hello) ctx.getService(ref);
	            if (hello != null)
	                hello.sayHello();
	            else
	                System.out.println("Service:Hello---object null");
	        } catch (RuntimeException e) {
	            e.printStackTrace();
	        } finally {
	            ctx.ungetService(ref);
	            hello = null;
	        }
	    } else {
	        System.out.println("Service:Hello---not exists");
	    }
		System.out.println("----------------hello client start---------------------");
	}

	public void stop(BundleContext ctx) throws Exception {

	}

}

4.配置Apache felix环境

下载:http://felix.apache.org/downloads.cgi


解压之后拷贝bin、conf、bundle目录到主目录下方便执行以及修改配置,同时创建plugins文件夹用于存放我们编写的bundle。

最后目录结构如下:


---执行bin中felix.jar

---控制台显示g!无报错信息则执行成功

---执行命令lb,可以查看已启动bundle

---然后打包client模块以及server模块,并将打包的jar包拷贝到plugins中

---执行命令 install file:plugins/XXX.jar,会显示ID,然后执行start ID,最后执行lb查看是否启动成功,并打印对应的信息


由于client以及server都是在本地调用,如果需要远程调用可以参考这篇文章:

http://www.cnblogs.com/lw900320/archive/2012/06/26/2563221.html

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 在IntelliJ IDEA快速新建Maven Spring Boot Web项目的步骤如下: 1. 打开IntelliJ IDEA,点击主菜单的“File(文件)”选项,选择“New(新建)”,然后选择“Project(项目)”。 2. 在左侧的菜单栏,选择“MavenMaven项目)”并点击“Next(下一步)”。 3. 在“GroupId”和“ArtifactId”字段,输入您想要的项目名和包名,并选择合适的“Version”。 4. 在下方的“Project settings”,选择您想要的项目位置,并勾选“Create from archetype(从原型创建)”复选框。 5. 在“Archetype”字段,输入“spring-boot-web”,然后点击“Next”。 6. 在“New Project”窗口,您可以设置项目的名称和位置,并设置其他项目设置,如项目类型、构建工具等。 7. 点击“Finish(完成)”,IntelliJ IDEA会为您创建一个默认的Maven Spring Boot Web项目。 8. 接下来,您可以开始编写代码和配置项目。可以在src/main/java目录下找到“Application.java”文件,它是Spring Boot应用程序的入口点。 9. 您还可以在“src/main/resources”目录下找到“application.properties”文件,您可以在其配置您的应用程序的属性。 10. 当您完成项目的编写和配置后,可以使用IntelliJ IDEA的“Build”菜单,选择“Build Project(构建项目)”来构建项目并生成可执行的jar文件。 以上是在IntelliJ IDEA快速新建Maven Spring Boot Web项目的简要步骤。您可以根据自己的实际需求和项目要求进行进一步的设置和配置。 ### 回答2: 要在IntelliJ IDEA快速新建一个Maven Spring Boot Web项目,按照以下步骤进行操作: 1. 打开IntelliJ IDEA并选择“Create New Project”(创建新项目)。 2. 在左侧面板选择“Spring Initializer”(Spring初始装置)选项,然后点击右侧面板的“Next”(下一步)按钮。 3. 在“Project SDK”(项目SDK)下拉菜单选择所需的Java版本,并点击“Next”(下一步)按钮。 4. 在“Project Metadata”(项目元数据)页面,输入项目的相关信息,如项目名称、项目的GroupId和ArtifactId等。 5. 在“Spring Boot”选项卡,选择所需的Spring Boot版本,并勾选“Web”(Web应用程序)选项。 6. 在“Project Settings”(项目设置)页面,选择项目的存储位置,并点击“Finish”(完成)按钮。 7. 创建项目后,IntelliJ IDEA会自动下载所需的依赖项,并生成一个基本的Maven Spring Boot Web项目结构。 8. 如果需要运行该项目,可以通过点击工具栏上的绿色箭头按钮来启动应用程序。 通过以上步骤,您可以在IntelliJ IDEA快速创建一个Maven Spring Boot Web项目,并进行开发和调试。 ### 回答3: 要在IntelliJ IDEA快速新建一个Maven Spring Boot Web项目,可以按照以下步骤操作: 1. 打开IntelliJ IDEA,选择“新建项目”。 2. 在弹出的对话框,选择“Maven”,然后点击“下一步”。 3. 在“GroupId”和“ArtifactId”字段,分别输入您想要的项目组和项目名称,然后点击“下一步”。 4. 在“Java”页面上,选择您想要使用的Java版本,然后点击“下一步”。 5. 在“项目设置”页面上,选择您想要的项目文件夹位置,并设置其他项目相关配置,然后点击“下一步”。 6. 在“进一步设置”页面上,选择“Spring Initializr”作为项目的初始程序,然后点击“下一步”。 7. 在“Spring Initializr 服务 URL”字段,输入Spring Initializr的URL,可以是https://start.spring.io或其他可用的URL,然后点击“下一步”。 8. 在“Spring Boot”页面上,选择您需要的Spring Boot版本,然后点击“下一步”。 9. 在“选择依赖”页面上,选择您需要的依赖,比如“Spring Web”,然后点击“下一步”。 10. 在“确认项目细节”页面上,确认您的项目细节,然后点击“完成”。 这样,IntelliJ IDEA就会自动为您创建一个Maven Spring Boot Web项目,并会下载所需的依赖项。您可以在项目结构查看创建的项目,并开始编写您的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值