用于OSGi部署的多个第三方库的Maven编译
问题描述:
我是Maven的初学者,我想从多个第三方库的.java创建.jar文件。我在我的项目中使用了超过32个库,我需要编译该项目,以便在CQ5 OSGi中使用它。我有这个在我的pom.xml用于OSGi部署的多个第三方库的Maven编译
<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)
感谢您的帮助
答
一个良好的开端将是developing with maven页面上的dev.day.com网站。这有很多信息让你开始。
如果您拥有的32个库位于Maven存储库中,则应通过POM中的dependency条目引用它们。如果依赖关系是不是行家,你可以用一个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
。
您可以使用Apache Felix SCR maven插件处理@Service & @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>
这个插件会产生被添加到您的包,将登记与费利克斯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中有一个简单的工作示例。
我已经更新了上面的答案。您通常不会将依赖项放在src/main/resources中。此位置通常用于静态资产/配置文件等。但是,如果您希望保留此位置,请将systemPath更新为如下所示:' $ {project.basedir} /src/main/resources/swingx-0.9。 3.jar ' –
diffa
非常感谢!现在我可以用所有的库编译它,但由于\ @Service和\ @Component,我仍然遇到错误。我已经在的部分添加了Apache Felix SCR maven插件,但它仍然相同。 –
Jakolcz
SCR注释依赖项也需要使用注释来编译项目。我已将详细信息添加到答案中。 – diffa