自定义文件作为依赖

时间:2012-02-16 16:00:55

标签: maven maven-assembly-plugin

我有一个包含多个模块的maven项目,

其中一个模块生成一个自定义的二进制文件。我需要这个文件作为另一个模块的输入。

我想要做的是将此文件作为依赖项获取,并在ant脚本的帮助下在其他模块中使用它。

我尝试了很多Maven Assembly Plugin和依赖:copy-dependencies插件,但没有成功

感谢您的任何建议

1 个答案:

答案 0 :(得分: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 / assembly / 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依赖项注入的必要步骤。

所以,现在我们只需将此zip工件添加为模块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-dependency-plugin 解压缩,理想情况是在module2的类路径中( target / classes ):

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