1. 基本配置
要使用iPOJO的maven插件(maven-ipojo-plugin),就要使用如下的配置(需要将$YOUR_XXX的部分替换成你需要的)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
<project> <modelVersion>4.0.0</modelVersion> <groupId>$YOUR_GROUP_ID</groupId> <artifactId>$YOUR_ARTIFACT_ID</artifactId> <version>$YOUR_ARTIFACT_VERSION</version> <name>$YOUR_PROJECT_NAME</name> <!-- 这里必须指定bundle(尽管最终打包出来还是一个jar包,但是它实际上已经是一个bundle包了) --> <packaging>bundle</packaging> <dependencies> $YOUR_MAVEN_DEPENDENCIES </dependencies> <build> <plugins> <!-- BND Maven Plugin插件配置 --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName> <Private-Package>$YOUR_PRIVATE_PACKAGE</Private-Package> <Export-Package>$YOUR_EXPORTED_PACKAGE</Export-Package> </instructions> </configuration> </plugin> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-ipojo-plugin</artifactId> <version>1.8.6</version> <executions> <execution> <goals> <goal>ipojo-bundle</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> |
iPOJO maven plugin通常要和BND maven plugin(maven-bundle-plugin)一起使用,且需要<package/>的值为bundle,但是这两个插件的配置是完全分开的。所以你可以使用到所有的BND插件的所有功能特性。上面的pom配置实例中iPOJO配置的部分可以不经修改的使用到你的eclipse工程中,前提条件是你的ipojo metadata文件要么在src/main/ipojo目录下,要么在src/main/resources目录下,而且必须以metadata.xml命名。
其实metadata文件对打包后的bundle没有作用,因为metadata文件会被iPOJO maven plugin解析,并以另一种格式写到打包后的jar的META-INF/MANIFEST.MF文件里面,作为iPOJO-Components的值。默认情况metadata文件是不会出现在最后的jar里面的。
2. 高级配置
指定ipojo metadata文件的配置路径和文件名,甚至是目录
例如:
|
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-ipojo-plugin</artifactId> <version>1.8.6</version> <executions> <execution> <goals> <goal>ipojo-bundle</goal> </goals> <configuration> <!-- 配置metadata文件所在的目录 --> <metadata>src/main/resources/ipojo</metadata> </configuration> </execution> </executions> </plugin> |
在<metadata>标签中,你可以指定metadata文件或者是多个metadata文件所在目录。文件路径是相对工程根目录来说的。如果没有配置默认情况ipojo插件会先在src/main/ipojo或者src/main/resources目录下查找metadata.xml文件,如果没找到,就找target/classes/metadata.xml和工程根目录下的metadata.xml。
设置跳过annotation的处理
要跳过annotation的处理,可以在<configuration>标签中配置<ignoreAnnotations
>,例如:
|
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-ipojo-plugin</artifactId> <version>1.8.6</version> <executions> <execution> <goals> <goal>ipojo-bundle</goal> </goals> <configuration> <ignoreAnnotations>true</ignoreAnnotations> </configuration> </execution> </executions> </plugin> |
iPOJO xml schema的使用
一般就用官方的schema就行了,有了xml schema在配置component和instance的时候就方便很多。
|
<?xml version="1.0" encoding="UTF-8"?> <iPOJO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="org.apache.felix.ipojo http://felix.apache.org/ipojo/schemas/CURRENT/core.xsd org.apache.felix.ipojo.extender http://felix.apache.org/ipojo/schemas/CURRENT/extender-pattern.xsd" xmlns="org.apache.felix.ipojo" xmlns:extender="org.apache.felix.ipojo.extender"> <component classname="ipojo.example.hello.client.HelloClient" architecture="true"> <!-- 。。。。。 --> </component> </iPOJO> |
当然不使用xml schema也没有问题,iPOJO maven plugin会使用内嵌的xml schema来解析你配置的metadata文件。如果想让iPOJO maven plugin不使用内嵌的xml schema,可以配置<ignoreEmbeddedSchemas>为true,例如:
|
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-ipojo-plugin</artifactId> <version>1.8.6</version> <executions> <execution> <goals> <goal>ipojo-bundle</goal> </goals> <configuration> <ignoreEmbeddedSchemas>true</ignoreEmbeddedSchemas> </configuration> </execution> </executions> </plugin> |
(可能不能访问外网的公司,需要使用ipojo xml schema时,需要把xsd放在内网中,这时候就需要这个功能了吧)
使用maven命令(skeleton)来创建一个iPOJO bundle工程
maven-ipojo-plugin提供了一个方便的创建iPOJO bundle工程的方案,你只需要根据需要执行下面的maven命令即可:
|
mvn org.apache.maven.plugins:maven-archetype-plugin:generate \ -DarchetypeArtifactId=maven-ipojo-plugin \ -DarchetypeGroupId=org.apache.felix \ -DartifactId=ARTIFACT_NAME_OF_YOUR_PROJECT \ -DgroupId=GROUP_ID_OF_YOUR_PROJECT \ -DarchetypeVersion=VERSION_OF_YOUR_PROJECT \ -DpackageName=PACKAGE_NAME |
这个命令会生成pom.xml、src/main/java和src/main/resources目录,以及groupId.artifactId组成的包名目录,还有使用了iPOJO annotation的实例代码。需要注意的是,pom.xml中的maven-ipojo-plugin将默认使用最新的版本,如需使用低版本需要手动修改pom.xml。
在pom.xml中配置iPOJO的metadata
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-ipojo-plugin</artifactId> <version>1.8.6</version> <executions> <execution> <goals> <goal>ipojo-bundle</goal> </goals> <configuration> <ignoreAnnotations>true</ignoreAnnotations> <metadata> <![CDATA[ <ipojo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="org.apache.felix.ipojo http://felix.apache.org/ipojo/schemas/CURRENT/core.xsd" xmlns="org.apache.felix.ipojo"> <component classname="org.apache.felix.ipojo.test.scenarios.component.LifecycleControllerTest" name="LFC-Test"> <provides /> <controller field="m_state" /> <properties> <property name="conf" field="m_conf" method="setConf" /> </properties> </component> <component classname="org.apache.felix.ipojo.test.scenarios.component.LifecycleControllerTest" name="LFC-Test-Immediate" immediate="true" architecture="true"> <provides /> <controller field="m_state" /> <properties> <property name="conf" field="m_conf" method="setConf" /> </properties> </component> </ipojo> ]]> </metadata> </configuration> </execution> </executions> </plugin> |
( 额,这种方式感觉不太雅~~不建议使用)
3. Reference
maven-ipojo-plugin的官方文档:http://felix.apache.org/documentation/subprojects/apache-felix-ipojo/apache-felix-ipojo-tools/ipojo-maven-plug-in.html