2013-07-01 63 views
0

我是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) 

感謝您的幫助

回答

4

一個良好的開端將是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中有一個簡單的工作示例。

+0

我已經更新了上面的答案。您通常不會將依賴項放在src/main/resources中。此位置通常用於靜態資產/配置文件等。但是,如果您希望保留此位置,請將systemPath更新爲如下所示:' $ {project.basedir} /src/main/resources/swingx-0.9。 3.jar' – diffa

+0

非常感謝!現在我可以用所有的庫編譯它,但由於\ @Service和\ @Component,我仍然遇到錯誤。我已經在部分添加了Apache Felix SCR maven插件,但它仍然相同。 – Jakolcz

+0

SCR註釋依賴項也需要使用註釋來編譯項目。我已將詳細信息添加到答案中。 – diffa

相關問題