Yaron
Yaron

Reputation: 2739

Adding content files to a Maven artifact

I create an artifact with maven and I want to add some content files to the target consumer beside the jar file. (I want to add some Jenkins scripts, but I want these scripts to get updated when the consumer upgrades into newer version of the artifact).
This is similar to .net nuget, where you can add a content library to the consumer project.

According to @tashkhisi suggestion I'm trying Maven assembly plugin.

The project structure:

> pipline (folder)
>>> file1.groovy (file)
>>> file2.groovy (file)
>>> file3.groovy (file)
> src (folder)
>>> ...
> assembly (folder)
>>> distribution.xml (file)
> pom (file)

In the pom file:

...
 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>assembly/distribution.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>trigger-assembly</id>
                        <phase>package</phase>
                        <goals>
                           <goal>single</goal>
                       </goals>
                    </execution>
                </executions>
            </plugin>
    </build>                     

The assembly/distribution.xml looes like that:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.4.1">
    <id>distribution</id>
    <formats>
        <format>jar</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${basedir}/pipeline</directory>
            <includes>
                <include>*.groovy</include>
            </includes>
            <excludes>
                <exclude>file2.groovy</exclude>
            </excludes>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>pipeline/file2.groovy</source>
            <outputDirectory></outputDirectory>
            <filtered>true</filtered>
        </file>
    </files>
</assembly>

I can see in the target folder that two jar files are created:

target/myLib-1.1.0-SNAPSHOT.jar
target/myLib-1.1.0-SNAPSHOT-distribution.jar

But when I try to consume it from another project the pipeline folder with the groovy files is not getting created...

Upvotes: 1

Views: 884

Answers (2)

J Fabian Meier
J Fabian Meier

Reputation: 35823

This is not possible.

Adding something as dependency will not add or create any folders in your project.

The consumer will need to add logic to the project to extract files from the dependency and copy them to the right place (probably possible with the Maven dependency plugin).

Upvotes: 0

Tashkhisi
Tashkhisi

Reputation: 2262

In your configuration you said pipeline is beside src and in your pom.xml you define outputDirectory as <outputDirectory>${basedir}/pipeline</outputDirectory> which is exactly beside src(pipeline is already there!) so if you want to put this pipeline directory beside target jar file in target directory you should modify this configuration to something like this:

<outputDirectory>${basedir}/target/pipeline</outputDirectory>

By the way creating a zip file which contains all the thing that you need in your deployment using assembly plugin is better approach, read the following link:

https://maven.apache.org/plugins/maven-assembly-plugin/

Upvotes: 1

Related Questions