看了一些博客和技术文章,实现jar 打包成 exe ,都是使用的 exe4j ,操作起来略复杂,有没有简单的方法呢?
既然写到这答案是肯定的。
launch4j-plugin插件可以直接帮我们jar 打包成exe
使用方法直接在项目pom中引入launch4j-plugin,由于还要指定入口文件我们配合maven-shade-plugin插件。
还是以上一篇的项目为例。
pom引入插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
</configuration>
</plugin>
<plugin>
<groupId>org.bluestemsoftware.open.maven.plugin</groupId>
<artifactId>launch4j-plugin</artifactId>
<version>1.5.0.0</version>
<executions>
<!-- GUI exe -->
<execution>
<id>l4j-gui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<outfile>target/app-gui.exe</outfile>
<jar>target/${project.artifactId}-${project.version}-shaded.jar</jar>
<errTitle>App Err</errTitle>
<classPath>
<mainClass>com.zgxinlei.tool.PassWordWin</mainClass>
</classPath>
<icon>src/main/resources/icons/exeIcon.ico</icon>
<jre>
<minVersion>1.8.0</minVersion>
<maxVersion>1.9.0</maxVersion>
<initialHeapSize>128</initialHeapSize>
<maxHeapSize>1024</maxHeapSize>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>1.0.0.0</txtFileVersion>
<fileDescription>Desc</fileDescription>
<copyright>C</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>1.0.0.0</txtProductVersion>
<productName>Product</productName>
<internalName>Product</internalName>
<originalFilename>App.exe</originalFilename>
</versionInfo>
</configuration>
</execution>
<!-- Command-line exe -->
<execution>
<id>l4j-cli</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<outfile>target/app-cli.exe</outfile>
<jar>target/${project.artifactId}-${project.version}-shaded.jar</jar>
<errTitle>App Err</errTitle>
<classPath>
<mainClass>com.zgxinlei.tool.PassWordWin</mainClass>
</classPath>
<icon>src/main/resources/icons/exeIcon.ico</icon>
<jre>
<minVersion>1.8.0</minVersion>
<maxVersion>1.9.0</maxVersion>
<initialHeapSize>128</initialHeapSize>
<maxHeapSize>1024</maxHeapSize>
</jre>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行打包命令
控制台输出
"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" -Dmaven.multiModuleProjectDirectory=D:\project\study\javatool "-Dmaven.home=D:\Program Files (x86)\JavaTool\apache-maven-3.6.0" "-Dclassworlds.conf=D:\Program Files (x86)\JavaTool\apache-maven-3.6.0\bin\m2.conf" "-Dmaven.ext.class.path=D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=61893:D:\Program Files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files (x86)\JavaTool\apache-maven-3.6.0\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.2.2 package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.zgxinlei:java-tool >-----------------------
[INFO] Building java-tool 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-tool ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-tool ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\project\study\javatool\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-tool ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\study\javatool\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ java-tool ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-tool ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-tool ---
[INFO] Building jar: D:\project\study\javatool\target\java-tool-1.0.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:1.4:shade (default) @ java-tool ---
[INFO] Including com.microsoft.sqlserver:sqljdbc4:jar:4.0 in the shaded jar.
[INFO] Attaching shaded artifact.
[INFO]
[INFO] --- launch4j-plugin:1.5.0.0:launch4j (l4j-gui) @ java-tool ---
[INFO] Platform-specific work directory already exists: C:\Users\Administrator\.m2\repository\org\bluestemsoftware\open\maven\plugin\launch4j-plugin\1.5.0.0
[INFO] launch4j: Compiling resources
[INFO] launch4j: Linking
[INFO] launch4j: Wrapping
[INFO] launch4j: Successfully created D:\project\study\javatool\target\app-gui.exe
[INFO]
[INFO] --- launch4j-plugin:1.5.0.0:launch4j (l4j-cli) @ java-tool ---
[INFO] Platform-specific work directory already exists: C:\Users\Administrator\.m2\repository\org\bluestemsoftware\open\maven\plugin\launch4j-plugin\1.5.0.0
[INFO] launch4j: Compiling resources
[INFO] launch4j: Linking
[INFO] launch4j: Wrapping
[INFO] launch4j: Successfully created D:\project\study\javatool\target\app-cli.exe
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.173 s
[INFO] Finished at: 2019-09-26T10:34:38+08:00
[INFO] ------------------------------------------------------------------------
打包完成,查看效果
双击正常运行。
如果出现无法运行,可能是本机安装的多份jre环境,导致版本不一样。