Search code examples
mavenmaven-3maven-pluginmaven-assembly-pluginmojo

how to make zip files (produced by a self-made maven plugin)from target folder end up in the local repository?


I am creating my own maven-environment-plugin that creates and bundle resources for a predefined folder structure for each environment defined in the configuration. The plugin is outputting the folder structure and resource in a zip file and placing it in the target folder.

Questions:

  • How can I make my plugin work like the maven-assembly-plugin so my output to target folder also ends up in my local repository when I use 'mvn install'?
  • Do I need to mark it or something? Its automaticallly doing it when the maven-assembly-plugin is used.
  • How does maven-assembly-plugin manage to make sure of this?

I am using mojo for my plugin development.

<plugin>
    <groupId>dk.kmd.devops.maven.plugin</groupId>
    <artifactId>envconfiguration-maven-plugin</artifactId>
    <version>1.0.3</version>
    <configuration>
        <environments>
            <environment>${env.local}</environment>
            <environment>${env.dev}</environment>
            <environment>${env.t1}</environment>
            <environment>${env.t2}</environment>
            <environment>${env.p0}</environment>
        </environments>
        <sourceConfigDir>${basedir}/src/main/config</sourceConfigDir>
        <zipEnvironments>true</zipEnvironments>
    </configuration>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>generateEnv</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Solution

  • You need to attach (that's the correct terminology in this case) the new artifact (the generated zip file) to the build as part of its official artifacts.

    This is basically what the attach-artifact goal of the build-helper-maven-plugin does:

    Attach additional artifacts to be installed and deployed.

    From its official examples, the attach goal:

    Typically run after antrun:run, or another plugin, that produces files that you want to attach to the project for install and deploy.

    The another plugin in this case can be the plugin you developed. Hence there are two solutions to your case:

    • Configure this plugin to attach the generated artifact as a further pom.xml configuration, or
    • add to your plugin the functionality to automatically attach the generated file

    The second case can be covered via Maven API, using the MavenProjectHelper and its attachArtifact method.

    In your mojo, you can import is as a component via:

    /**
     * Maven ProjectHelper
     */
    @Component
    private MavenProjectHelper projectHelper;
    

    Then use the aforementioned method:

    projectHelper.attachArtifact(project, "zip", outputFile);
    

    You should probably already have the required Maven dependency providing it, but just in case it would be this one:

    <dependency>
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-core</artifactId>
       <version>3.3.9</version>
    </dependency>
    

    Note that the artifact will be attached to the build as an additional artifact via a classifier, that is, a suffix to the default artifact name differentiating it from the default artifact and making it unique as output of the build.


    As a reference to real example and to further answer your (last) question, check this query on the GitHub maven-plugins repository, checking for the attachArtifact string, you will see it used in a number of Maven plugins, among which the maven-assembly-plugin, for example here in the AbstractAssemblyMojo class.