写在前面

因为用JavaFX做了个PC的小说下载阅读软件,所以想实现下自动更新,每次手动下载更新实在太麻烦了,后来就百度了一下看看有没有现成的结果找到的FXLauncher,这个东西的官方文档实在是太过简洁了。不过好在弄出来了

操作过程

1.首先下载官方的domo,然后分析他的配置

引入依赖

<dependency>
            <groupId>no.tornado</groupId>
            <artifactId>fxlauncher</artifactId>
            <version>1.0.20</version>
        </dependency>

首先是properties

<properties>
        <app.filename>Uncle小说</app.filename>
        <!--运行的主类-->
        <app.mainClass>com.unclezs.UI.App.Main</app.mainClass>
        <!--FXLauncher运行后文件下载的位置-->
        <app.cacheDir>./lib</app.cacheDir>
        <!--FXLauncher运行时候的参数,比如什么更新失败后继续打开程序之类的,有哪些看官方的Readme里面-->
        <app.parameters></app.parameters>
        <app.vendor>Acme Inc</app.vendor>
        <app.version>3.3</app.version>
        <!--更新文件的服务器地址,在这里可以打开下载你的更新jar包就行,tomcat和apache都行-->
        <app.url>http://114.115.208.62/</app.url>
        <!--编译后的位置,这个位置在Idea里面是target下面的app目录-->
        <app.dir>${project.build.directory}/app</app.dir>
        <!--打包exe程序后的目录位置target/install-->
        <app.installerdir>${project.build.directory}/installer</app.installerdir>
        <!--是否接受版本降级-->
        <app.acceptDowngrade>false</app.acceptDowngrade>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

然后是build

<build>
        <plugins>
            <!-- 编译执行文件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <outputDirectory>${app.dir}</outputDirectory>
                </configuration>
            </plugin>
            <!-- 拷贝依赖jar包到编译目录 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <excludeScope>provided</excludeScope>
                    <outputDirectory>${app.dir}</outputDirectory>
                    <stripVersion>true</stripVersion>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.4.0</version>
                <!-- 生成 app.xml manifest -->
                <executions>
                    <execution>
                        <id>create-manifest</id>
                        <phase>package</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>fxlauncher.CreateManifest</mainClass>
                            <arguments>
                                <argument>${app.url}</argument>
                                <argument>${app.mainClass}</argument>
                                <argument>${app.dir}</argument>
                                <argument>--cache-dir=${app.cacheDir}</argument>
                                <argument>--accept-downgrade=${app.acceptDowngrade}</argument>
                                <!--这里可以放你想要更新文件打包进去的文件 比如dll,db之类的,可以事先放在app目录下-->
                                <argument>--include-extensions=jpg</argument>
                                <argument>${app.parameters}</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <!-- 把app.xml打包进fxlauncher.xml,这样就启动器就不需要依赖app.xml来启动了 -->
                    <execution>
                        <id>embed-manifest-in-launcher</id>
                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>jar</executable>
                            <workingDirectory>${app.dir}</workingDirectory>
                            <arguments>
                                <argument>uf</argument>
                                <argument>fxlauncher.jar</argument>
                                <argument>app.xml</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.配置分析完了可以看看怎么用了

打开控制台输入:mvn package

javafx可以做成exe文件吗 javafx转exe_javafx可以做成exe文件吗


编译完成后会在target/app下面出现我们的jar包和依赖文件

全部复制到tomcat的root目录下,这个时候要能通过tomcat下载你的jar文件

<app.url>http:// 202.202.144.134:8080/</app.url>

如果这个url+你的jar包名字拼接出来的URL,能够在浏览器下载这个jar包,那这个时候就可以了
这个时候复制出fxlauncher.jar,随便到一个文件夹,这个时候就能够自动下载依赖并且自动更新了
更新的时候你只需要把新的依赖在服务器更新就行了,app启动的时候会自动更新

exe4j打包exe

1.打开exe4j,选择打包方式

javafx可以做成exe文件吗 javafx转exe_maven_02

2.输入软件名字和打包后的exe文件放在哪里

javafx可以做成exe文件吗 javafx转exe_jar_03

3.选择图标,选择打包程序类型,应用名字

javafx可以做成exe文件吗 javafx转exe_jar_04

4.选择哪种运行方式,勾了就是64,不勾就是32

javafx可以做成exe文件吗 javafx转exe_javafx_05

5.选择fxlauncher.jar和运行主类,fxlauncher.Laucnher

javafx可以做成exe文件吗 javafx转exe_jar_06

6.这里配置jre,是搜索自带的环境变量或者添加本地的jre都可以

javafx可以做成exe文件吗 javafx转exe_javafx_07

7.然后点完成就可以了

双击exe可以自动更新的exe程序出炉,上张图

javafx可以做成exe文件吗 javafx转exe_jar_08


此软件源码地址:

https://github.com/unclezs/NovelHarvester