用于OSGi部署的多个第三方库的Maven编译

时间:2013-07-01 07:27:06

标签: maven pom.xml cq5

我是Maven的初学者,我想从我的.java创建带有多个第三方库的.jar文件。我在项目中使用了超过32个库,我需要编译项目,以便在CQ5 OSGi中使用它。我在我的POM.xml中有这个

<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>info.hartmann.dfs</groupId>
  <artifactId>dfs-connection-handler</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>DFS connection handler</name>
  <build>
    <sourceDirectory>C:\Users\302104\workspace\DFS\src</sourceDirectory>
    <resources>
      <resource>
        <directory>C:\Users\302104\workspace\lib</directory>
      </resource>
    </resources>
    <directory>C:\Users\302104\workspace\DFS\target</directory>
    <finalName>dfs-connection-handler-0.0.1-SNAPSHOT</finalName>
    <plugins>
  <plugin>
    <groupId>org.apache.sling</groupId>
    <artifactId>maven-sling-plugin</artifactId>
    <executions>
        <execution>
            <id>install-bundle</id>
            <goals>
                <goal>install</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <slingUrl>http://localhost:4502/system/console</slingUrl>
        <user>user</user>
        <password>password</password>
    </configuration>
</plugin>
  <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.1.0</version>
    <extensions>true</extensions>
    <executions>
        <execution>
            <id>wrap-my-dependency</id>
            <goals>
                <goal>wrap</goal>
            </goals>
            <configuration>
                <wrapImportPackage>;</wrapImportPackage>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <instructions>
            <export-package>info.hartmann.dfs</export-package>
            <import-package>
                java.util.List;resolution=optional,
                com.emc.documentum.fs.datamodel.core.*;resolution=optional,
                com.emc.documentum.fs.datamodel.core.content.*;resolution=optional,
                com.emc.documentum.fs.datamodel.core.profiles.*;resolution=optional,
                com.emc.documentum.fs.datamodel.core.query.*;resolution=optional,
                com.emc.documentum.fs.rt.context.*;resolution=optional,
                com.emc.documentum.fs.services.core.client.*;resolution=optional,
                *
            </import-package>
        </instructions>
    </configuration>
  </plugin>
</plugins>
  </build>

</project>

我几乎不知道我正在用这个pom.xml做什么,所以任何帮助都会很好。

BTW如何使用

之类的标记编译.java文件
@Service(DfsHandler.class)
@Component(label = "DFS Connection Handler", immediate = true, metatype = true)

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

一个好的起点是developing with maven网站上的dev.day.com页面。这有很多信息可以帮助您入门。

如果您拥有的32个库位于maven存储库中,则应通过POM中的dependency条目引用它们。如果依赖项不在maven中,则可以在依赖项条目中使用systemPath引用它们,如下所示:

<dependency>
    <groupId>org.swinglabs</groupId>
    <artifactId>swingx</artifactId>
    <version>0.9.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/swingx-0.9.3.jar</systemPath>
</dependency>

或者,this article描述了如何使用maven将这些库添加到本地存储库。

如果你能够,最好根据maven standard directory layout放置你的项目,以避免必须配置很多路径。至少配置相对于项目的路径,而不是特定于您的机器。例如,不要使用C:\Users\302104\workspace\DFS\src,只需使用src

您可以处理@Service&amp;使用Apache Felix SCR maven插件的@Component注释。 :

  <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-scr-plugin</artifactId>
    <version>1.9.0</version>
    <executions>
      <execution>
        <id>generate-scr-scrdescriptor</id>
        <goals>
          <goal>scr</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

此插件将生成添加到您的包中的元数据,该元数据将使用Felix OSGi运行时注册您的服务。

您还需要项目中的SCR注释依赖项:

<dependency>
  <!-- scr annotations - for generating component descriptors only -->
  <groupId>org.apache.felix</groupId>
  <artifactId>org.apache.felix.scr.annotations</artifactId>
  <version>1.6.0</version>
  <scope>provided</scope>
</dependency>

这个presentation on SCR应该会让你对它们的使用有一个很好的介绍。另外,我在this github repo中有一个简单的工作示例。

相关问题