使用多个第三方库进行Maven编译,用于OSGi部署



我是Maven的初学者,我想用多个第三方库从我的.java创建.jar文件。我在我的项目中使用了超过32个库,我需要编译这个项目,以便在CQ5OSGi中使用它。我的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:Users302104workspaceDFSsrc</sourceDirectory>
    <resources>
      <resource>
        <directory>C:Users302104workspacelib</directory>
      </resource>
    </resources>
    <directory>C:Users302104workspaceDFStarget</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做什么,所以任何帮助都会很好。

顺便说一句,我如何使用之类的标记编译.java文件

@Service(DfsHandler.class)
@Component(label = "DFS Connection Handler", immediate = true, metatype = true)

感谢您的帮助

一个好的起点是dev.day.com网站上的用maven开发页面。这里有很多信息可以让你开始。

如果您拥有的32个库位于maven存储库中,则应该通过POM中的依赖项引用它们。如果依赖项不在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>

另外,本文还介绍了如何使用maven将这些库添加到本地存储库中。

如果可以的话,最好根据maven标准目录布局来布局项目,以避免配置大量路径。至少要将路径配置为相对于项目,而不是特定于您的机器。例如,不使用C:Users302104workspaceDFSsrc,而只使用src

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

  <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>

这个关于SCR的演示应该能很好地介绍它们的使用。此外,我在github repo中有一个简单的工作示例。

最新更新