You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JavaFX与Maven构建依赖存于外部lib文件夹的项目

调整Maven配置将依赖放入外部lib文件夹

没问题,我帮你修改了pom.xml配置,这样你的JavaFX项目构建后,依赖会被单独复制到target/lib文件夹,主jar包不会包含任何依赖,同时应用依然能正确运行。以下是完整的配置和修改说明:

核心修改点:

  • 把maven-dependency-plugin的unpack-dependencies目标换成copy-dependencies,直接复制依赖jar到指定的lib目录,而不是解压到classes文件夹被打包进主jar。
  • 更新exec-maven-plugin的javafxpackager参数,添加类路径指向lib文件夹下的所有依赖,确保主jar运行时能找到这些依赖。
  • 移除了原来会把依赖打包进主jar的相关配置,同时升级了部分插件版本以提升稳定性。

完整的pom.xml配置:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <mainClass>org.main.Main</mainClass>
    <!-- 定义依赖输出目录,方便统一维护 -->
    <dependency.output.dir>${project.build.directory}/lib</dependency.output.dir>
</properties>

<dependencies>
    <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-utils</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <!-- 复制依赖到外部lib文件夹 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${dependency.output.dir}</outputDirectory>
                        <excludeScope>system</excludeScope>
                        <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- 构建JavaFX主jar并配置类路径 -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <id>create-javafx-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>${java.home}/../bin/javafxpackager</executable>
                        <arguments>
                            <argument>-createjar</argument>
                            <argument>-nocss2bin</argument>
                            <argument>-appclass</argument>
                            <argument>${mainClass}</argument>
                            <argument>-srcdir</argument>
                            <argument>${project.build.directory}/classes</argument>
                            <argument>-outdir</argument>
                            <argument>${project.build.directory}</argument>
                            <argument>-outfile</argument>
                            <argument>${project.build.finalName}.jar</argument>
                            <!-- 添加类路径指向lib文件夹下的所有依赖 -->
                            <argument>-classpath</argument>
                            <argument>${dependency.output.dir}/*.jar</argument>
                        </arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>default-cli</id>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>${java.home}/bin/java</executable>
                        <!-- 运行时指定完整类路径:主jar + lib下的所有依赖 -->
                        <commandlineArgs>-cp "${project.build.directory}/${project.build.finalName}.jar:${dependency.output.dir}/*" ${mainClass}</commandlineArgs>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- 保留原有的编译和测试配置,升级版本提升兼容性 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArguments>
                    <bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
                </compilerArguments>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${java.home}/lib/jfxrt.jar</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>
        </plugin>
    </plugins>
</build>

效果说明:

执行mvn clean package后,你会在target目录下看到:

  1. 主jar文件(比如your-project-name.jar),仅包含你的项目代码,没有依赖内容
  2. lib文件夹,里面存放了所有项目依赖的jar包

运行应用时,既可以通过mvn exec:exec直接启动,也可以手动执行命令:

java -cp "your-project-name.jar:lib/*" org.main.Main

(Windows系统请把冒号换成分号;)

内容的提问来源于stack exchange,提问作者Loandoer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:48:07