2012-02-16 49 views
0

我有一個Maven項目有幾個模塊,自定義文件作爲依賴

這些模塊之一產生自定義,二進制文件。我需要這個文件作爲另一個模塊的輸入。

我想要做的是取得這個文件作爲依賴關係,在一個螞蟻腳本的幫助下在其他模塊中使用它。

我嘗試了很多與Maven Assembly插件和依賴關係:複製的依賴插件,但沒有成功

感謝您的任何建議

回答

2

我有我的一個項目非常類似的要求。我想在這裏合成它,我希望這能幫助你:

比方說,該項目的結構如下:

projectfoo (pom) 
    |- module1 (your binary dependency) 
    L module2 (the module that needs your dependency) 

讓我們先從projectfoo POM:

<groupId>com.dyan.sandbox</groupId> 
<artifactId>projectfoo</artifactId> 
<version>0.0.1</version> 
<packaging>pom</packaging> 

<modules> 
    <module>module1</module> 
    <module>module2</module> 
</modules> 

易....

現在模塊1:

<parent> 
    <groupId>com.dyan.sandbox</groupId> 
    <artifactId>projectfoo</artifactId> 
    <version>0.0.1</version> 
</parent> 

<groupId>com.dyan.sandbox.projectfoo</groupId> 
<artifactId>module1</artifactId> 
<version>0.0.1</version> 
<packaging>pom</packaging> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.2</version> 
      <executions> 
       <execution> 
        <id>make-your-resource</id> 
        <goals> 
         <goal>single</goal> 
        </goals> 
        <phase>package</phase> 
        <configuration> 
         <descriptors> 
          <descriptor>src/main/assembly/resources.xml</descriptor> 
         </descriptors> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

...和描述符文件(的src/main /組裝/ resources.xml中):

<assembly> 
    <id>resources</id> 
    <formats> 
     <format>zip</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <fileSets> 
     <fileSet> 
      <directory>src/main/resources/</directory> 
      <outputDirectory>.</outputDirectory> 
     </fileSet> 
    </fileSets> 
</assembly> 

我這裏假設你以前生成的二進制資源或那種方式和存儲在src/main/resources。上面的代碼只是創建資源的zip文件,這是緩解它作爲module2中maven依賴項的注入的必要步驟。

所以,現在我們只需要這個壓縮神器模塊2添加作爲一個依賴:

<groupId>com.dyan.sandbox.projectfoo</groupId> 
<artifactId>module2</artifactId> 
<version>0.0.1</version> 

<dependencies> 
    <dependency> 
     <groupId>com.dyan.sandbox.projectfoo</groupId> 
     <artifactId>module1</artifactId> 
     <version>0.0.1</version> 
     <classifier>resources</classifier> 
     <type>zip</type> 
     <scope>provided</scope> 
    </dependency> 
</dependencies> 

...最後與Maven的依賴,插件解壓,最好在classpath模塊2的(目標/班):

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>unpack-your-resource</id> 
        <goals> 
         <goal>unpack-dependencies</goal> 
        </goals> 
        <phase>generate-resources</phase> 
        <configuration> 
         <!-- unzip the resources in compilation folder --> 
         <outputDirectory>${project.build.directory}/classes</outputDirectory> 
         <includeArtifactIds>module1</includeArtifactIds> 
         <excludeTransitive>true</excludeTransitive> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

...這就是它!

Yannick。

+0

我會盡快測試這個,但它看起來不錯:-)已經非常感謝 – Yves 2012-02-20 07:07:47

+0

讓我知道它;)。 – Yanflea 2012-02-20 07:58:15

+0

這適用於我的情況。謝謝 – Yves 2012-03-21 12:07:50