javamavenmaven-assembly-pluginmaven-deploy-plugin

How can I use Maven to generate a JAR file from the contents of a directory I specify?


I'm using the maven-deploy-plugin to deploy a third party jar (previously downloaded to my target directory from a plugin not show here) to Nexus as the name 'third-party-1.0.jar', this all works fine using the configuration below.

I also have a javadoc directory in my target directory, which is the javadoc for this third party jar. I'd like to package that javadoc directory as 'third-party-1.0-javadoc.jar'.

If I could get the directory packaged as a JAR, I think I could the javadoc parameter of the deploy plugin below to deploy it, just unsure how to package a custom directory as a JAR with a specific name using Maven, maybe the assembly plugin?

TLDR; How do I use Maven to create a JAR file from the contents of a directory I specify?

<plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <executions>
        <execution>
            <id>default-deploy</id>
            <phase>none</phase>
        </execution>

        <execution>
             <id>deploy-jar</id>
             <phase>deploy</phase>
             <goals>
                 <goal>deploy-file</goal>
             </goals>
             <configuration>
             <file>${project.build.directory}/code.jar</file>
             <url>...</url>
             <repositoryId>...</repositoryId>  
             <url>...</url>            
             <packaging>jar</packaging>
             <generatePom>true</generatePom>
             <groupId>com.example</groupId>
             <artifactId>third-party</artifactId>
             <version>1.0</version> 
             </configuration>     
        </execution>
    </executions>
</plugin>

Solution

  • I ended up using the maven-assembly-plugin instead of the maven-jar-plugin as it allows more control over the resulting JAR, including not adding a pom.xml to it, not attaching it to the project as an artifact to be deployed (attach=false), and how it is named. This allows the maven-deploy-plugin to control how it is deployed, and what Maven coordinates to use when deploying it.

    I can then just attach the Javadoc JAR built with the assembly plugin into my deploy file execution using the javadoc tag:

    src/main/assembly/assembly.xml

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
        <id>package-third-party-javadoc</id>
        <formats>
            <format>jar</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <fileSet>
                <directory>${project.build.directory}/dir</directory>  
                <outputDirectory>.</outputDirectory>
            </fileSet>
        </fileSets>
    </assembly>
    

    pom.xml

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
            <execution>
                <id>package-third-party-javadoc</id>
                <phase>compile</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <finalName>third-party-javadoc</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <attach>false</attach>  
                </configuration>
            </execution>
        </executions>
    </plugin>
    
    <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.2</version>
        <executions>
            <execution>
                <id>default-deploy</id>
                <phase>none</phase>
            </execution>
    
            <execution>
                <id>deploy-jar</id>
                <phase>deploy</phase>
                <goals>
                    <goal>deploy-file</goal>
                </goals>
                <configuration>
                    <file>${project.build.directory}/code.jar</file>
                    <url>...</url>
                    <repositoryId>...</repositoryId>  
                    <url>...</url>            
                    <packaging>jar</packaging>
                    <generatePom>true</generatePom>
                    <groupId>com.example</groupId>
                    <artifactId>third-party</artifactId>
                    <version>1.0</version> 
                    <javadoc>${project.build.directory}/third-party-javadoc.jar</javadoc>
                </configuration>     
            </execution>
        </executions>
    </plugin>