osgi+maven 创建项目有关问题总结

osgi+maven 创建项目问题总结
最近公司的需在研究了一osgi 与maven 结合的开发。做了一个简单的技术预研。通多方面的分析决定采用Pax-Construct 创建项目。

Pax-Construct安装

    下载:http://repo1.maven.org/maven2/org/ops4j/pax/construct/scripts/1.4/scripts-1.4.zip

    设置环境变量:Path:C:\Pax-Construct-1.4\bin

    使用参考:http://www.ops4j.org/projects/pax/construct/
 
配置环境变量 pax/bin,maven/bin
执行以下命令:
1.pax-create-project -g gsoft -a g-osgi
  
   其中-g后代表groupId,-a代表artifactId
2.建立一个测试用的bundle:
cd g-osgi
pax-create-bundle -p com.gsoft.biz -n first.bundle
pax-create-bundle -p com.gsoft.biz.imp -n second.bundle
   其中,-p是包名package,-n是bundleName

3. 创建eclipse工程文件
mvn pax:eclipse -DdownloadSources
如下信息表示项目际创建成功:
[INFO] ------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] gsoft.g-osgi (OSGi project) ....................... SUCCESS [0.303s]
[INFO] g-osgi - plugin configuration ..................... SUCCESS [0.192s]
[INFO] g-osgi - wrapper instructions ..................... SUCCESS [0.004s]
[INFO] g-osgi - bundle instructions ...................... SUCCESS [0.004s]
[INFO] g-osgi - imported bundles ......................... SUCCESS [0.004s]
[INFO] gsoft.g-osgi.first.bundle [com.gsoft.biz] ......... SUCCESS [0.223s]
[INFO] gsoft.g-osgi.second.bundle [com.gsoft.biz.imp] .... SUCCESS [0.062s]
[INFO] ------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------
[INFO] Total time: 1.752s
[INFO] Finished at: Sun May 27 21:52:35 CST 2012
[INFO] Final Memory: 7M/111M

4.通过eclipse导入已存在工程将工程导入
执行 mvn clean install命令 出现如下异常
ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :first.bundle

使用 mvn clean install -X  -- 查看详细日志

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.ops4j:maven-pax-plugin:1.4:compile (default-compile) on project first.bundle: Execution default-compile of goal org.ops4j:maven-pax-plugin:1.4:compile failed: An API incompatibility was encountered while executing org.ops4j:maven-pax-plugin:1.4:compile: java.lang.NoSuchMethodError: org.apache.maven.project.MavenProject.addPlugin(Lorg/apache/maven/model/Plugin;)V
-----------------------------------------------------
realm =    plugin>org.ops4j:maven-pax-plugin:1.4
………………………………
修改 根项目的 pom.xml
加入 maven 资源库位置
关于这一块配置请参考
http://blog.****.net/arvinzhuo/archive/2009/06/05/4244061.aspx
<distributionManagement>
    <repository>
      <id>nexus-releases</id>
      <name>Nexus Releases</name>
      <url>http://localhost:8081/nexus/content/repositories/releases</url>
    </repository>
    <snapshotRepository>
      <id>nexus-snapshots</id>
      <name>Nexus Snapshots</name>
      <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
  </distributionManagement>

项目根下的pom.xml 修改成以下的配置
<build>
    <plugins>
      <plugin>
        <groupId>org.ops4j</groupId>
        <artifactId>maven-pax-plugin</artifactId>
        <version>1.4</version>
        <configuration>
          <!--
           | some example Pax-Runner settings
          -->
          <provision>
            <param>--platform=felix</param>
          </provision>
        </configuration>
        <executions>
          <!--
           | uncomment to auto-generate IDE files
          <execution>
            <id>ide-support</id>
            <goals>
              <goal>eclipse</goal>
            </goals>
          </execution>
          -->
        </executions>
      </plugin>
    </plugins>
  </build>


改成如下内容
<build>
    <plugins>
      <plugin>
        <groupId>org.ops4j</groupId>
        <artifactId>maven-pax-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>1.4.3</version>
        <configuration>
          <provision>
            <param>--platform=equinox</param>
            <param>--vmOptions=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000</param>
          </provision>
        </configuration>
        <extensions>true</extensions>
        <!-- 移动到这里 -->
      </plugin>
    </plugins>
  </build>



关于Pom.xml 配置请参考
http://springsfeng.iteye.com/blog/1389080

5.mvn clean install

………………………………………………
esults :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-bundle-plugin:1.4.3:bundle (default-bundle) @ second.bundle ---
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ second.bundle ---
[INFO] Installing D:\osgi\osgi_lib_simple\g-osgi\second.bundle\target\second.bundle-1.0-SNAPSHOT.jar to d:\work\gsoft\g-osgi\second.bundle\1.0-SNAPSHOT\second.bundle-1.0-SNAPSHOT.jar
[INFO] Installing D:\osgi\osgi_lib_simple\g-osgi\second.bundle\pom.xml to d:\work\gsoft\g-osgi\second.bundle\1.0-SNAPSHOT\second.bundle-1.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-bundle-plugin:1.4.3:install (default-install) @ second.bundle ---
[INFO] Parsing file:/d:/work/repository.xml
[INFO] Installing gsoft/g-osgi/second.bundle/1.0-SNAPSHOT/second.bundle-1.0-SNAPSHOT.jar
[INFO] Writing OBR metadata
[INFO] ------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] gsoft.g-osgi (OSGi project) ....................... SUCCESS [0.199s]
[INFO] g-osgi - plugin configuration ..................... SUCCESS [0.064s]
[INFO] g-osgi - wrapper instructions ..................... SUCCESS [0.010s]
[INFO] g-osgi - bundle instructions ...................... SUCCESS [0.010s]
[INFO] g-osgi - imported bundles ......................... SUCCESS [0.010s]
[INFO] gsoft.g-osgi.first.bundle [com.gsoft.biz] ......... SUCCESS [2.074s]
[INFO] gsoft.g-osgi.second.bundle [com.gsoft.biz.imp] .... SUCCESS [0.721s]
[INFO] ------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------
[INFO] Total time: 4.423s
[INFO] Finished at: Sun May 27 22:00:17 CST 2012
[INFO] Final Memory: 15M/148M
[INFO] ------------------------------------
BUN 创建完成