Yves
Yves

Reputation: 172

Custom file as dependency

I have a maven project with several modules,

One of these modules produces a custom, binary file. I need this file as input in another module.

What I want to do, is to fetch this file as dependency an use it in an other module with the help of an ant-script.

I tries a lot with Maven Assembly Plugin and dependency:copy-dependencies plugin, but with no success

Thanks for any suggestions

Upvotes: 0

Views: 172

Answers (1)

Yanflea
Yanflea

Reputation: 3934

I had a very similar requirement for a project of mine. I am trying to synthesize it here, I hope this could help you :

Let's say that the project is structured as follow :

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

Let's start with the 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> 

Easy....

Now the module 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>

... and the descriptor file (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>

I assume here that you have previously generated your binary resource one way or another and stored it in src/main/resources. What the code above does is just creating a zip artifact of your resource, this is a necessary step to ease its injection as a maven dependency in module2.

So, now we just have to add this zip artifact as a dependency in module 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>

... and finally unzip it with the maven-dependency-plugin, ideally in the classpath of 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>

... and that's it !

Yannick.

Upvotes: 2

Related Questions