java 利用 maven launch4j-plugin 插件 jar 打包 exe

看了一些博客和技术文章,实现jar 打包成 exe ,都是使用的 exe4j ,操作起来略复杂,有没有简单的方法呢?

既然写到这答案是肯定的。

launch4j-plugin插件可以直接帮我们jar 打包成exe

使用方法直接在项目pom中引入launch4j-plugin,由于还要指定入口文件我们配合maven-shade-plugin插件。

还是以上一篇的项目为例。

java 利用 maven launch4j-plugin 插件 jar 打包 exe_第1张图片

pom引入插件


        
            
                org.apache.maven.plugins
                maven-shade-plugin
                1.4
                
                    
                        package
                        
                            shade
                        
                    
                
                
                    
                        
                            *:*
                            
                                META-INF/*.SF
                                META-INF/*.DSA
                                META-INF/*.RSA
                            
                        
                    
                    true
                    shaded
                
            

            
                org.bluestemsoftware.open.maven.plugin
                launch4j-plugin
                1.5.0.0
                
                    
                    
                        l4j-gui
                        package
                        
                            launch4j
                        
                        
                            gui
                            target/app-gui.exe
                            target/${project.artifactId}-${project.version}-shaded.jar
                            App Err
                            
                                com.zgxinlei.tool.PassWordWin
                            
                            src/main/resources/icons/exeIcon.ico
                            
                                1.8.0
                                1.9.0
                                128
                                1024
                            
                            
                                1.0.0.0
                                1.0.0.0
                                Desc
                                C
                                1.0.0.0
                                1.0.0.0
                                Product
                                Product
                                App.exe
                            
                        
                    
                    
                    
                        l4j-cli
                        package
                        
                            launch4j
                        
                        
                            console
                            target/app-cli.exe
                            target/${project.artifactId}-${project.version}-shaded.jar
                            App Err
                            
                                com.zgxinlei.tool.PassWordWin
                            
                            src/main/resources/icons/exeIcon.ico
                            
                                1.8.0
                                1.9.0
                                128
                                1024
                            
                        
                    
                
            
        
    

执行打包命令

java 利用 maven launch4j-plugin 插件 jar 打包 exe_第2张图片

控制台输出

"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] ------------------------------------------------------------------------

打包完成,查看效果

java 利用 maven launch4j-plugin 插件 jar 打包 exe_第3张图片

双击正常运行。

如果出现无法运行,可能是本机安装的多份jre环境,导致版本不一样。

你可能感兴趣的:(maven)