Search code examples
mavenjarmaven-assembly-pluginexec-maven-plugin

Maven: How to package and deploy output files generated by main() method execution?


I have a Maven project that uses the exec-maven-plugin to execute a class with a main method, and this generates an output file in the target directory. The configuration looks like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
        <execution>
            <id>process-execution</id>
            <phase>package</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.example.MainClass</mainClass>
        <systemProperties>
            <systemProperty>
                <key>INPUT_FILE_PATH</key>
                <value>${basedir}/src/main/resources/input_file.csv</value>
            </systemProperty>
            <systemProperty>
                <key>OUTPUT_FILE_PATH</key>
                <value>${project.build.directory}/output_file.json</value>
            </systemProperty>
        </systemProperties>
    </configuration>
</plugin>

I want to be able to package and deploy this output file (output_file.json) as a separate jar to the package repository along with the standard jar file built with the project classes.

Is there a way to get this done? Perhaps with the maven-assembly-plugin?


Solution

  • Yes, you can install and deploy an additional artifact by using the maven-assembly-plugin:

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

    This means that an additional artifact according to "descriptor.xml" is created, installed and deployed. The file "descriptor.xml" defines which directory should be packaged:

    <?xml version="1.0" encoding="UTF-8"?>
    <assembly
      xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
        http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
          http://maven.apache.org/xsd/assembly-1.1.2.xsd"
    >
      <id>json</id>
      <formats>
        <format>jar</format>
      </formats>
      <fileSets>
        <fileSet>
          <outputDirectory>/</outputDirectory>
          <directory>/target/deploy/json</directory>
        </fileSet>
      </fileSets>
    </assembly>