搭建osgi项目的记录

最近在做一个用osgi为框架的项目,所以学习如何搭建osgi的框架。废话不多说,直接上建立过程

第一步:新建一个maven project,  右键eclipse-new-other-maven-maven project,然后选好存放项目的地址。接下来建立Apache 骨架

然后填写相应的artificated和groupId之后点击确定,我们将这个项目是作为父项目,在该项目的pom文件中如下配置:

<?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>
	<groupId>com.gzydt.license</groupId>
	<artifactId>parent</artifactId>
	<version>1.0.0</version>
	<packaging>pom</packaging>
	<name>Gzydt :: Parent</name>
	<url>http://www.gzydt.com</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<cxf.version>2.7.13</cxf.version>
	</properties>

	<repositories>
		<repository>
			<id>nexus</id>
			<name>GZYDT Nexus Repository</name>
			<url>http://192.168.10.62:8081/nexus/content/groups/public</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>nexus</id>
			<name>GZYDT Nexus Repository</name>
			<url>http://192.168.10.62:8081/nexus/content/groups/public</url>
		</pluginRepository>
	</pluginRepositories>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.osgi</groupId>
				<artifactId>org.osgi.core</artifactId>
				<version>4.3.1</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>org.apache.cxf</groupId>
				<artifactId>cxf-rt-frontend-jaxrs</artifactId>
				<version>${cxf.version}</version>
				<scope>provided</scope>
			</dependency>
			<dependency>
				<groupId>org.apache.cxf</groupId>
				<artifactId>cxf-rt-rs-security-oauth2</artifactId>
				<version>${cxf.version}</version>
			</dependency>
			<dependency>
				<groupId>org.codehaus.jackson</groupId>
				<artifactId>jackson-mapper-asl</artifactId>
				<version>1.9.13</version>
			</dependency>
			<dependency>
				<groupId>org.json</groupId>
				<artifactId>json</artifactId>
				<version>20140107</version>
			</dependency>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.11</version>
				<scope>test</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
         <!--打算在该项目下面新建四个子项目-->
	<modules>
	<module>base</module>
	<module>assembly</module>
	<module>sign</module>
	<module>catalog</module>
	</modules>
</project>
接下来建立该项目的子项目,首先建立base组件,这个组件主要是用来管理基本的子系统,如用户管理等,这个base组件也是一个父项目,在base组件下分为三层,一层是persist层,该层是定义持久化和实体层,第二层是service,主要定义service服务,第二层是restful层,在该层主要实现和前端交互的服务。所以我新建一个maven modle工程该工程命名为base,(这个必须要记住,第一层是project,第二层是moudle,第三层是moudle)其中base的pom文件配置如下:

<?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/maven-v4_0_0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<parent>	
	<groupId>com.gzydt.license</groupId>
	<artifactId>parent</artifactId>
	<version>1.0.0</version>
	</parent>

	<artifactId>com.gzydt.license.base</artifactId>
	<name>Gzydt :: License :: Base</name>
	<url>http://www.gzydt.com</url>
	<packaging>pom</packaging>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.gzydt.license</groupId>
				<artifactId>com.gzydt.license.base.persist</artifactId>
				<version>${project.version}</version>
			</dependency>
			<dependency>
				<groupId>com.gzydt.license</groupId>
				<artifactId>com.gzydt.license.base.service</artifactId>
				<version>${project.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

    <!--打算在该项目下面新建3个子项目-->
	<modules>
	<module>com.gzydt.license.base.persist</module>
	<module>com.gzydt.license.base.service</module>
	<module>com.gzydt.license.base.rest</module>
	</modules>
</project>
接下来在base组件下新建三个子module

右键eclipse-new-maven-maven module


然后写好相对应的groupId和artificateId,,最后配置相应的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>
         <!--指定父项目  -->
	<parent>
    <groupId>com.gzydt.license</groupId>
	<artifactId>com.gzydt.license.base</artifactId>
	<version>1.0.0</version>
	</parent>
	<artifactId>com.gzydt.license.base.persist</artifactId>
	<packaging>bundle</packaging>
	<name>Gzydt :: License :: Base :: Persist</name>
	<url>http://www.gzydt.com</url>

	<dependencies>
		<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-jpa_2.0_spec</artifactId>
			<version>1.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-jta_1.1_spec</artifactId>
			<version>1.1.1</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.openjpa</groupId>
			<artifactId>openjpa</artifactId>
			<version>2.3.0</version>
			<scope>provided</scope>
		</dependency>

		<!-- OpenJPA PCEnhancer depends -->
		<dependency>
			<groupId>net.sourceforge.serp</groupId>
			<artifactId>serp</artifactId>
			<version>1.13.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- end OpenJPA PCEnhancer depends -->
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi.core</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi.enterprise</artifactId>
			<version>4.2.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.karaf.shell</groupId>
			<artifactId>org.apache.karaf.shell.console</artifactId>
			<version>2.3.0</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- mysql-connector -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.34</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.0.1</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>2.5.3</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Version>1.0.0</Bundle-Version>
						<!-- 嵌入依赖的非 provided 的 bundle,加入私有类空间 -->
						<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
						<Embed-Transitive>true</Embed-Transitive>
						<Embed-Directory>dependency</Embed-Directory>
						<Export-Package>
                                                    <!--将base中的persist和entity包都导出给别的项目使用  --> 
                                                        com.gzydt.license.base.persist,
							com.gzydt.license.base.persist.entity,
							org.apache.commons.dbcp2
						</Export-Package>
						<Import-Package>  
                                                     <!--将base的persist组件进行调试,如果不能启动base.persist组件则看是否确实什么依赖,若确实,则加!依赖 --> 
                                                         org.apache.felix.service.command,
							org.apache.felix.gogo.commands,
							org.apache.karaf.shell.console,
							org.apache.karaf.shell.commands,
							!com.mchange.v2.c3p0,
							!net.sf.cglib.proxy,
							!org.apache.log,
							!org.hibernate.service.jdbc.connections.spi,
							!org.jboss.resource.adapter.jdbc,
							!org.jboss.resource.adapter.jdbc.vendor,
							*
						</Import-Package>
						<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
					</instructions>
				</configuration>
			</plugin>
			<!-- Skip Test by default and enable them only in Test profile -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.18</version>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

然后在base.persit组件中将persistence.xml配置文件放在src/recource包下面MATE-INF组件下面,其中persistence.xml的内容如下:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
	version="1.0">
	<persistence-unit name="com.gzydt.license.base"
		transaction-type="JTA">
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl
		</provider>
		<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/mysqlds)
		</jta-data-source>
		<properties>
			<!-- <property name="openjpa.jdbc.Schema" value="TODS" /> -->
			<property name="openjpa.jdbc.DBDictionary" value="mysql" />
			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)" />
			<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
		</properties>
	</persistence-unit>
</persistence>
接下来是建立一个module为com.gzydt.license.service这一层主要是放service服务,其中在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>

	<parent>
	<groupId>com.gzydt.license</groupId>
	<artifactId>com.gzydt.license.base</artifactId>
	<version>1.0.0</version>
	</parent>
	<artifactId>com.gzydt.license.base.service</artifactId>
	<packaging>bundle</packaging>
	<name>Gzydt :: License :: Base :: Service</name>
	<url>http://www.gzydt.com</url>

	<dependencies>
		<dependency>
		  <groupId>com.gzydt.license</groupId>
			<artifactId>com.gzydt.license.base.persist</artifactId>
			<version>1.0.0</version>
			<!--这个范围一定要提供,不然的话会依赖其他的包  -->
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>2.5.3</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Version>1.0.0</Bundle-Version>
						<!-- 嵌入依赖的非 provided 的 bundle,加入私有类空间 -->
						<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
						<Embed-Transitive>true</Embed-Transitive>
						<Embed-Directory>dependency</Embed-Directory>
						<Export-Package>
							com.gzydt.license.base.service
						</Export-Package>
						<Import-Package>
							org.apache.felix.service.command,
							org.apache.felix.gogo.commands,
							org.apache.karaf.shell.console,
							org.apache.karaf.shell.commands,
							!com.mchange.v2.c3p0,
							!org.apache.log,
							!org.hibernate.service.jdbc.connections.spi,
							!org.jboss.resource.adapter.jdbc,
							!org.jboss.resource.adapter.jdbc.vendor,
							*
						</Import-Package>
					</instructions>
				</configuration>
			</plugin>
			<!-- Skip Test by default and enable them only in Test profile -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.18</version>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
按上面的方式建立module为com.gzydt.license.rest,其中pom文件的配置如下图所示:

<?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>
		<!-- <groupId>com.gzydt.formatLicense</groupId>
	<artifactId>com.gzydt.formatLicense.base</artifactId>
	<version>1.0.0</version> -->
	 <groupId>com.gzydt.license</groupId>
	<artifactId>com.gzydt.license.base</artifactId>
	<version>1.0.0</version>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<artifactId>com.gzydt.license.base.rest</artifactId>
	<packaging>bundle</packaging>
	<name>Gzydt :: License :: Base :: Rest</name>
	<url>http://www.gzydt.com</url>

	<dependencies>
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>org.osgi.core</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxrs</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
		 <groupId>com.gzydt.license</groupId>
			<artifactId>com.gzydt.license.base.persist</artifactId>
			<version>1.0.0</version>
			<!--这个范围一定要提供,不然的话会依赖其他的包  -->
			<scope>provided</scope>
		</dependency>
		<dependency>
			 <groupId>com.gzydt.license</groupId>
			<artifactId>com.gzydt.license.base.service</artifactId>
			<version>1.0.0</version>
			<!--这个范围一定要提供,不然的话会依赖其他的包  -->
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
		</dependency>

		<!-- 引入 json 处理返回简单类型数据,复杂数据需要通过 jackson-mapper-asl来处理业务实体 -->
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
		</dependency>

		<!-- We are also building a small Java client application that we can use 
			to test our web web services. For performing the HTTP requests, we are using 
			Apache Commons HttpClient. -->
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
			<scope>provided</scope>
		</dependency>
		<!-- For logging, we will use SLF4J, which is also available in the container 
			by default. -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.6</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<version>2.5.3</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Version>${project.version}</Bundle-Version>
						<Bundle-Activator>com.gzydt.license.base.rest.RestActivator</Bundle-Activator>
						<!-- 嵌入依赖的非 provided 的 bundle,加入私有类空间 -->
						<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
						<Embed-Transitive>true</Embed-Transitive>
						<Embed-Directory>dependency</Embed-Directory>
						<Export-Package>com.gzydt.license.base.rest*;version=${project.version}</Export-Package>
						<Import-Package>
						!com.mchange.v2.c3p0,
						!org.apache.log,
						!org.hibernate.service.jdbc.connections.spi,
						!org.jboss.resource.adapter.jdbc,
						!org.jboss.resource.adapter.jdbc.vendor,
						   *
						</Import-Package>
					</instructions>
				</configuration>
			</plugin><!-- Skip Test by default and enable them only in Test profile -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.18</version>
				<configuration>
					<skipTests>true</skipTests>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
最后我们再建立一个assembly组件,这个组件是用来配置整个项目的。他位于paren父项目的子项目

新建一个module然后设置父项目为parent,然后在改组件下配置dataSource.xml文件,是用于配置数据库的

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
      http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
      ">

	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/license" />
		<property name="username" value="root" />
		<property name="password" value="123" />
	</bean>

	<service interface="javax.sql.DataSource" ref="dataSource">
		<service-properties>
			<entry key="osgi.jndi.service.name" value="jdbc/mysqlds" />
		</service-properties>
	</service>
</blueprint>

如果在servicemix或者是Apache karaf中运行项目的话还需要将这个datasource.xml文件放在容器deploy文件下面


最后要配置相关的futures.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<features>

	<!-- ServiceMix5.4及以上版本已经集成,这里主要是提供给 Karaf 使用 -->
	<repository>mvn:org.apache.cxf.karaf/apache-cxf/3.0.2/xml/features
	</repository>

	<!-- 项目系统组件的 feature 配置 -->
	<feature name="gzydt-license">
		<!-- <bundle>blueprint:file:/C:/datasource-mysql.xml</bundle> -->
		<feature>license-base</feature>
		<feature>license-sign</feature>
		<feature>license-catalog</feature>
	</feature>

	<feature name="license-cxf">
		<!-- ServiceMix5.4需要3.0.2,具体版本要求请查看 ServiceMix关于CXF的集成 -->
		<feature version="3.0.2">cxf</feature>
		<!-- openjpa及jndi在 karaf集成 -->
		<feature version="2.3.0">openjpa</feature>
		<feature>jndi</feature>
	</feature>

	<feature name="license-base">
		<feature>license-cxf</feature>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.base.persist/1.0.0
		</bundle>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.base.service/1.0.0
		</bundle>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.base.rest/1.0.0
		</bundle>
	</feature>
	
	<feature name="license-sign">
		<feature>license-cxf</feature>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.sign.persist/1.0.0
		</bundle>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.sign.service/1.0.0
		</bundle>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.sign.rest/1.0.0
		</bundle>
	</feature>
	
	<feature name="license-catalog">
		<feature>license-cxf</feature>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.catalog.persist/1.0.0
		</bundle>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.catalog.service/1.0.0
		</bundle>
		<bundle>mvn:com.gzydt.license/com.gzydt.license.catalog.rest/1.0.0
		</bundle>
	</feature>
</features>

过程就是这样的,


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值