java 项目利用maven插件打成exe可执行包

需求描述

有时候我们写了一个可执行的java程序,利用maven很轻松地打成了可执行的jar包。

然是只能在安装了jdk的平台上才可以直接双击运行。那我不像利用第三方工具,可以直接打成windows可执行的exe包吗?

可以的

一款融合了launch4j的maven插件,可以帮我们实现这个难题

直接上代码 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>org.example</groupId>
    <artifactId>这里是你自己项目的信息</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
//你的项目依赖
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <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>out/artifacts/exe的路径.exe</outfile>
                            <jar>out/artifacts/jar的路径.jar</jar>
                            <errTitle>取个名字</errTitle>
                            <classPath>
                                <mainClass>指向你的可运行主类</mainClass>
                                <addDependencies>true</addDependencies>
                                <preCp>anything</preCp>
                            </classPath>
                            <icon>图标路径</icon>
                            <jre>
                                <minVersion>1.5.0</minVersion>
                                <opts>
                                    <opt>-Djava.endorsed.dirs=./endorsed</opt>
                                </opts>
                            </jre>
                            <versionInfo>
                                <fileVersion>1.0.0.1</fileVersion>
                                <txtFileVersion>1.0</txtFileVersion>
                                <fileDescription>描述一下</fileDescription>
                                <copyright>anz</copyright>
                                <productVersion>1.0.0.1(这个格式一定不能变)</productVersion>
                                <txtProductVersion>1.0</txtProductVersion>
                                <productName>名字</productName>
                                <internalName>名字</internalName>
                                <originalFilename>名字.exe</originalFilename>
                            </versionInfo>
                        </configuration>
                    </execution>
                    <execution>
                        <id>l4j-gui</id>
                        <phase>package</phase>
                        <goals>
                            <goal>launch4j</goal>
                        </goals>
                        <configuration>
                            <headerType>gui</headerType>
                            <outfile>out/artifacts/exe的路径.exe</outfile>
                            <jar>out/artifacts/jar的路径.jar</jar>
                            <errTitle>取个名字</errTitle>
                            <classPath>
                                <mainClass>Main</mainClass>
                                <addDependencies>true</addDependencies>
                                <preCp>anything</preCp>
                            </classPath>
                            <icon>图标路径</icon>
                            <jre>
                                <minVersion>1.5.0</minVersion>
                                <opts>
                                    <opt>-Djava.endorsed.dirs=./endorsed</opt>
                                </opts>
                            </jre>
                            <versionInfo>
                                <fileVersion>1.0.0.1</fileVersion>
                                <txtFileVersion>1.0</txtFileVersion>
                                <fileDescription>描述</fileDescription>
                                <copyright>随便写</copyright>
                                <productVersion>1.0.0.1</productVersion>
                                <txtProductVersion>1.0</txtProductVersion>
                                <productName>名字</productName>
                                <internalName>名字</internalName>
                                <originalFilename>名字.exe</originalFilename>
                            </versionInfo>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

注意修改我留的备注,再贴一下assembly.xml放在根目录

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>取个名字</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>out/artifacts/</directory>
            <outputDirectory>out/artifacts/</outputDirectory>
            <includes>
                <include>*.exe</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

好了,这样运行maven命令build就会在你设置好的路径下打出exe包了!

1人评论了“java 项目利用maven插件打成exe可执行包”

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Scroll to Top