如何让JRE与launch4j捆绑?

时间:2018-04-04 13:08:48

标签: java maven launch4j

我知道launch4j并没有将{J {1}}中的JRE捆绑在一起,而是必须将它放在旁边。我的问题是,我该怎么做?有没有办法让maven自动定位和复制JDK的JRE用于构建我的应用程序并将其复制到给定目录?

我尝试过这样的事情:

.exe

但该计划尚无法启动。它显示了一个立即消失的小对话框(它似乎是空白的,但它让我真的注意到它太快了。)

2 个答案:

答案 0 :(得分:12)

更新:删除了我以前的答案并替换为经过测试的工作示例

更新2:这个pom.xml现在下载JRE tgz并解压缩它,launch4j exe使用它并且它可以工作。我添加了评论来解释它是如何工作的。

我建议只使用32位exe和JRE。使用64位JRE的唯一原因是,如果您的程序需要使用超过4 GB的RAM。

当然,现在您需要一个能够完成所有这些并安装到Program Files的安装程序。我过去曾经使用过NSIS。 NSIS有一个学习曲线,但也不算太糟糕。

<?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.akathist.encc</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- This is the win32 JRE tgz hosted by alfresco - https://mvnrepository.com/artifact/com.oracle.java/jre -->
        <dependency>
            <groupId>com.oracle.java</groupId>
            <artifactId>jre</artifactId>
            <classifier>win32</classifier>
            <type>tgz</type>
            <version>1.8.0_131</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <!-- this repository has the JRE tgz -->
            <id>alfresco</id>
            <url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <!-- this is to extract the JRE tgz file we downloaded -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>com.oracle.java</includeGroupIds>
                            <includeTypes>tgz</includeTypes>
                            <includeArtifactIds>jre</includeArtifactIds>
                            <includeClassifiers>win32</includeClassifiers>
                            <outputDirectory>target/win32</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- This calls launch4j to create the program EXE -->
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>console</headerType>
                            <outfile>target/encc.exe</outfile>
                            <jar>target/mavenproject1-1.0-SNAPSHOT.jar</jar>
                            <errTitle>encc</errTitle>
                            <classPath>
                                <mainClass>com.akathist.encc.Clui</mainClass>
                                <addDependencies>false</addDependencies>
                                <preCp>anything</preCp>
                            </classPath>
                            <jre>
                                <path>./win32/java</path>
                            </jre>
                            <versionInfo>
                                <fileVersion>1.2.3.4</fileVersion>
                                <txtFileVersion>txt file version?</txtFileVersion>
                                <fileDescription>a description</fileDescription>
                                <copyright>my copyright</copyright>
                                <productVersion>4.3.2.1</productVersion>
                                <txtProductVersion>txt product version</txtProductVersion>
                                <productName>E-N-C-C</productName>
                                <internalName>ccne</internalName>
                                <originalFilename>original.exe</originalFilename>
                            </versionInfo>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

更新3:可悲的事实是,您所需的JRE不存在官方甚至最新的maven回购。您可以托管自己的具有所需JRE的maven仓库。新版本完成后,您必须更新此更新。在发布之前使用新版本进行测试也是一个好主意。每个月的第三个星期二是新Java发布完成的时间。您可以为此设置提醒,以检查是否已发布新版本并下载。由于许可协议检查,自动化这是一个痛苦。这篇文章可能有所帮助,但您可能无法以这种方式下载JRE的tar.gz版本:Java check latest version programmatically

如果您想支持多个平台,那么托管您自己的maven repo是一个很好的方法。每次发布版本时,您都可以托管自己的仓库并使用新的JRE tar.gz进行更新:https://stackoverflow.com/a/29261502/35264

最简单的选择是完成您的目标,只使用您正在构建的JRE。只要使用32位JRE构建,这将允许您支持Windows 32和64。您可以随时更新此版本,因为您有时间使用新版本进行测试。这是一个有效的pom.xml:

<?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.akathist.encc</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <!-- This copies the JRE used to do the build from java.home - should be 32 bit Windows JRE -->
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <!-- here the phase you need -->
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/win32/java</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${java.home}</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- This calls launch4j to create the program EXE -->
                <groupId>com.akathist.maven.plugins.launch4j</groupId>
                <artifactId>launch4j-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>l4j-clui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>console</headerType>
                            <outfile>target/encc.exe</outfile>
                            <jar>target/mavenproject1-1.0-SNAPSHOT.jar</jar>
                            <errTitle>encc</errTitle>
                            <classPath>
                                <mainClass>com.akathist.encc.Clui</mainClass>
                                <addDependencies>false</addDependencies>
                                <preCp>anything</preCp>
                            </classPath>
                            <jre>
                                <path>./win32/java</path>
                            </jre>
                            <versionInfo>
                                <fileVersion>1.2.3.4</fileVersion>
                                <txtFileVersion>txt file version?</txtFileVersion>
                                <fileDescription>a description</fileDescription>
                                <copyright>my copyright</copyright>
                                <productVersion>4.3.2.1</productVersion>
                                <txtProductVersion>txt product version</txtProductVersion>
                                <productName>E-N-C-C</productName>
                                <internalName>ccne</internalName>
                                <originalFilename>original.exe</originalFilename>
                            </versionInfo>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

答案 1 :(得分:-1)

我相信这就是你要找的东西:

<plugin>
  <groupId>com.akathist.maven.plugins.launch4j</groupId>
  <artifactId>launch4j-maven-plugin</artifactId>
  <executions>
    <execution>
      ...
      <configuration>
        ...
        <jre>
          <path>${java.home}</path> <!-- SEE THIS -->
        </jre>
        ...
      </configuration>
    </execution>
  </executions>
</plugin>

Java允许您使用java.home系统属性访问JRE路径。你可以access java system properties within pom。你有一个包装launch4j的插件。将所有这些放在一起,你就有了解决方案。