1

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

Yaron
  • 2,209
  • 3
  • 18
  • 33

2 Answers2

1

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/

Tashkhisi
  • 2,070
  • 1
  • 7
  • 20
  • 1
    This looks promising. I'll investigate and if it will do I'll accept the answer – Yaron Jul 26 '20 at 15:49
  • @Yaron I've just tried to do it using resource plugin and it works as well with some modification I edited my answer please read that again. – Tashkhisi Jul 26 '20 at 16:46
  • Hi @tashkhisi, I updated the question with maven assembly plugin. Still something not working... – Yaron Jul 27 '20 at 08:23
  • lets make it clear: you want to have a zipfile which contains your target jar file and pipeline folder with its content inside it? – Tashkhisi Jul 27 '20 at 09:13
  • Yes. I don't really care if it'a a zip or not. I want the consumer to get the jar file dependency and the pipeline folder with the content inside it (I exclude one of the file but basically that's the idea) :-). Just to be sure, I need the consumer the get the dependency tree so the jar will be able to function properly... – Yaron Jul 27 '20 at 09:15
  • So extract `target/myLib-1.1.0-SNAPSHOT-distribution.jar` what is in it right now? – Tashkhisi Jul 27 '20 at 09:17
  • It has the pipeline folder with 3 groovy files. When I consume the jar no pipeline folder is getting created in the consumer project. – Yaron Jul 27 '20 at 09:26
  • Know you have one jar file with all its dependent resources in it, and you can also turn it into zip file. It is not your responsablity to change the project structure of consuming project you can distribute it and document how consumer project should use your distribution. – Tashkhisi Jul 27 '20 at 09:33
  • How can my consumer get this content into his project structure? – Yaron Jul 27 '20 at 09:36
  • It is something that should be done in consumer application maven, Although I can't say something you want to do is the right thing there is a workaround for that read the link below they have solved this for another problem:https://stackoverflow.com/questions/5559519/maven-extract-files-from-jar/5639045 – Tashkhisi Jul 27 '20 at 09:48
  • Also assembly plugin is used when you want to deploy your jar file somewhere and run it, In this case you can use previous resource plugin I think I solved your problem with that plugin in my answer. – Tashkhisi Jul 27 '20 at 09:50
  • To my mind CI/CD and jenkins file of project should not be controlled by dependency and also it should not be included in your jar file. jenkins file and any thing that depends on CI/CD should be completely outside jar file. – Tashkhisi Jul 27 '20 at 09:58
0

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).

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142