Package java files in exe for windows with maven

2.5k Views Asked by At

The Launch4J port for Maven is terribly documented and I fail to use it in the way I want.

Is there some good Maven plugin to gernerate an exe file with following criteria (at least some of them, if possible):

  • No wrapping of jars
  • Jars can be in different directories relative to the jar file
  • The jars are exactly the dependancies, so adding new jars to a directory with a different name does not have any effect.
  • Xmx and Xms can be configured through a file
  • Process runs under the .exe name if possible (not so important)
2

There are 2 best solutions below

0
On

You can use the maven-assembly-plugin, with two execution one specifying the main class and packaging all your dependencies in a single jar, so you don't need any classpath. The second execution will put all you configuration files in the same jar. So finaly you have not an exe you have a jar with the manifest file in a zip file.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-5</version>
  <executions>
    <execution>
      <id>make-assembly1</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com....class.with.the.main</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </execution>
    <execution>
      <id>make-assembly2</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>distribution.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
  </executions>
</plugin>
0
On

You could use the plugin

            <plugin>
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <version>1.5.2</version>
            </plugin>

for wrapping your jar files in an exe.

Beforehand you could wrap up everything in one jar like described in the answer of aberes.

So, for example, a configuration could look like this:

                   <plugin>
                    <groupId>com.akathist.maven.plugins.launch4j</groupId>
                    <artifactId>launch4j-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>l4j-clui</id>
                            <phase>install</phase>
                            <goals>
                                <goal>launch4j</goal>
                            </goals>
                            <configuration>
                                <!--
                                <headerType>gui</headerType>        -->
                                 <headerType>console</headerType>
                                <jar>target/yourFinalJar.jar</jar>
                                <outfile>target/${project.build.finalName}.exe</outfile>
                                <errTitle>${project.name}</errTitle>
                                <icon>your/Icon.ico</icon>
                                <jre>
                                    <path>jre</path> <!-- if you bundle the jre -->
                                </jre>

                                <versionInfo>
                                    <fileVersion>1.2.3.4</fileVersion>
                                    <txtFileVersion>${project.version}</txtFileVersion>
                                    <fileDescription>${project.description}</fileDescription>
                                    <copyright>(c) ${project.inceptionYear} MyCompany</copyright>
                                    <productVersion>1.0.0.0</productVersion>
                                    <txtProductVersion>${project.version}</txtProductVersion>
                                    <productName>${project.name}</productName>
                                    <companyName>MyCompany</companyName>
                                    <internalName>${project.name}</internalName>
                                    <originalFilename>${project.build.finalName}.exe</originalFilename>
                                </versionInfo>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>