Java Tip #1 - Build a Windows installer

If you want to distribute your Java application in a Windows installer and you want to do it using Maven the mini recipe below will get you started.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>myproduct-windows-installer</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>MyProduct Windows Installer</name>
    <build>
        <finalName>myproduct-windows-installer</finalName>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId<myproduct-main-jar</artifactId>
            <version>${project.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>windows</id>
            <activation>
                <os>
                    <family>windows</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>2.10</version>
                        <executions>
                            <execution>
                                <id>validate</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/jars</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.5.0</version>
                        <executions>
                            <execution>
                                <id>package</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>${env.JAVA_HOME}/bin/javapackager</executable>
                                    <arguments>
                                        <argument>-deploy</argument>
                                        <argument>-native</argument>
                                        <argument>exe</argument>
                                        <argument>-name</argument>
                                        <argument>MyProduct</argument>
                                        <argument>-appclass</argument>
                                        <argument>com.mycompany.MyProductMainClass</argument>
                                        <argument>-srcdir</argument>
                                        <argument>${project.build.directory}/jars</argument> 
                                        <argument>-srcfiles</argument>      
                                        <argument>myproduct-main-jar-${project.version}.jar</argument>
                                        <argument>-srcdir</argument>                                 
                                        <argument>${project.build.directory}/../src/main/license</argument>
                                        <argument>-srcfiles</argument>
                                        <argument>LICENSE.rtf</argument>
                                        <argument>-outdir</argument>
                                        <argument>target</argument>
                                        <argument>-outfile</argument>
                                        <argument>myproduct-windows-installer</argument>
                                        <argument>-Bcopyright='Copyright (c) 2016 MyCompany.com. All rights reserved.'</argument>
                                        <argument>-BlicenseFile=LICENSE.rtf</argument>
                                        <argument>-BshortcutHint=true</argument>
                                        <argument>-BsystemWide=true</argument>
                                        <argument>-Bvendor=MyCompany.com</argument>
                                        <argument>-Bwin.menuGroup=MyProduct</argument>
                                        <argument>-v</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.10</version>
                        <executions>
                            <execution>
                                <id>attach-artifacts</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>attach-artifact</goal>
                                </goals>
                                <configuration>
                                    <artifacts>
                                        <artifact>
                                            <file>${project.build.directory}/bundles/MyProduct-1.0.exe</file>
                                            <type>exe</type>
                                        </artifact>
                                    </artifacts>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

        

And voila you now have a Windows installer!

Posted September 29, 2016

Up