Create a .war file that includes a jar-with-dependencies.jar for JNLP using Maven

java, java-web-start, jnlp, maven

Solution

Firstly, what you want is the JAR file to be available to be downloaded by users of the WAR file. Including the file in WEB-INF/lib as a standard dependency of the WAR project is not what you want. What you most likely want is the JAR to be put in a different directory in the WAR (such as `/downloads`).

To achieve this in Maven, you can use the Maven Dependency Plugin.

1: Use the dependency plugin to copy your JAR file to a temporary build directory.

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>my.group</groupId>
                  <artifactId>myartifact</artifactId>
                  <version>1.0</version>
                  <type>jar</type>
                  <overWrite>false</overWrite>
                  <outputDirectory>${project.build.directory}/webapp-downloads</outputDirectory>
                  <destFileName>myartifact.jar</destFileName>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>

here we are copying your JAR file to the ${project.build.directory/webapp-downloads directory

2: configure the WAR plugin to include the resources generated by the dependency plugin.

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <webResources>
            <resource>                  
              <directory>${project.build.directory/webapp-downloads</directory>
              <targetPath>downloads</targetPath>
            </resource>
          </webResources>
        </configuration>
      </plugin>

This will cause the JAR file to be bundled in you WAR under the `downloads` directory. Users can then download it by going to /downloads/myartifact.jar to download it.

In the case of webstart, you would configure your JNLP with the appropriate path instead of having the user directly download the JAR.

Problem

I have a Maven project that builds a Swing client as a jar-with-dependencies.jar file. I want to use Java Web Start to distribute this my.gui-0.1.0-jar-with-dependencies.jar file. I've created a separate Maven project that builds a .war file with the JNLP artifacts for this purpose. I need to include the my.gui-0.1.0-jar-with-dependencies.jar in this .war file. I haven't been able to determine the Maven coordinates for the jar-with-dependencies.jar file. If I use the Maven coordinates for the GUI project it puts the dependency .jar files for the GUI project in the WEB-INF/lib, which is not what I want. I need to have the my.gui-0.1.0-jar-with-dependencies.jar file itself in the .war file. (I suppose it could be in WEB-INF/lib.) How do I tell Maven that the dependency is the jar-with-dependencies.jar file itself? If there is another way besides the mechanism to tell Maven to include the jar-with-dependencies.jar itself that would work too. The jar-with-dependencies.jar has to be somewhere in the .war file I'm creating to support Java Web Start. I know there is a Maven Webstart plugin, but that looks like a nightmare so I'm just building a .war file myself with the necessary JNLP artifacts.

Original source