【问题标题】:How to use Spring Boot build plugin with JavaFX app如何将 Spring Boot 构建插件与 JavaFX 应用程序一起使用
【发布时间】:2019-02-06 19:11:29
【问题描述】:

我使用 Netbeans 为我的 JavaFX 应用程序生成模板。它生成了一个 POM.xml,构建部分似乎过于复杂。此外,每次我编译项目时它都会解压缩所有依赖项,每次大约需要 3 分钟。这是 POM.xml 文件的相关部分:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
</parent>

<organization>
    <name>MDenis</name>
</organization>

<dependencies>

    <!--SPRING-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
    <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>

    <!--HIBERNATE-->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.10.Final</version>
    </dependency>

    <!--HSQLDB-->
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
    </dependency>

    <!--LOMBOK-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <!--LOG4J2-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <excludeScope>system</excludeScope>
                        <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>

                    <phase>package</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>${java.home}/../bin/javapackager</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>
                        </arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>default-cli</id>
                    <goals>
                        <goal>exec</goal>                            
                    </goals>
                    <configuration>
                        <executable>${java.home}/bin/java</executable>
                        <commandlineArgs>${runfx.args}</commandlineArgs>
                    </configuration>
                </execution>
            </executions>  
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.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.16</version>
            <configuration>
                <additionalClasspathElements>
                    <additionalClasspathElement>${java.home}/lib/jfxrt.jar</additionalClasspathElement>
                </additionalClasspathElements>
            </configuration>
        </plugin>
    </plugins>
</build>

这是我的第一个 JavaFX 项目,在我的其他项目中,我只使用此构建部分,一切正常:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

但是,当尝试将它与此项目一起使用时,我收到以下错误:

The parameters 'executable' for goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec are missing or invalid

为什么这不适用于 JavaFX 项目?似乎它想要执行甚至不在我的 POM 文件中的目标。

编辑

我确实在有效的 POM 选项卡中看到了这个目标,但我对 Maven 的了解还不够,无法理解它,无论如何我无法更改有效部分中的任何内容。任何人都知道如何使 Spring Boot 插件与 JavaFX 桌面应用程序一起工作?

任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: maven spring-boot javafx build compiler-errors


    【解决方案1】:

    你需要重构 pom 文件。配置必须在执行之外,像这样:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>unpack-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>exec</goal>
          </goals>
              <configuration>
                <executable>${java.home}/../bin/javapackager</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>
                  </arguments>
                </configuration>
              </execution>
            <execution>
              <id>default-cli</id>
              <goals>
                <goal>exec</goal>                            
              </goals>
            </execution>
          </executions>  
        <configuration>
          <executable>${java.home}/bin/java</executable>
          <commandlineArgs>${runfx.args}</commandlineArgs>
        </configuration>
    </plugin>
    

    【讨论】:

    • 这对我使用 Spring Boot 插件 INSTEAD 有什么帮助?
    • 我看错了,抱歉。你需要做的就是在执行标签中添加一个 id,比如 vthis:
    • 我不想解压缩依赖项,我想使用我在原始帖子中指定的 spring boot 插件。我发布的使用 Spring Boot 插件的简短构建部分适用于我的所有其他项目,除了这个,唯一的区别是使用 JavaFX。
    • 抱歉,这是一个如何在执行中添加 id 的示例,您可以随意调用它:foooo
    • 我的意思是显而易见的是,如果这是您所要求的,那么显然是为了让拆包停止所有处决。上面的提示将使 spring-boot-maven-plugin 工作
    【解决方案2】:

    我终于成功地完成了这项工作,而无需在每次编译时解包依赖项。我使用de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport 类来扩展我的主类,将launch(args); 放在main 方法中,现在我在POM.XML 文件的构建部分中有以下代码:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                </executions>  
                <configuration>
                    <executable>${java.home}/bin/java</executable>
                    <commandlineArgs>${runfx.args}</commandlineArgs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

    • 非常欢迎您,感谢您的帮助。原来你不需要 jfxsupport 库,标准 JavaFX Application 类中也有 launch() 方法,它也可以正常工作!
    猜你喜欢
    • 2017-01-04
    • 2014-03-20
    • 2016-07-07
    • 2013-04-15
    • 2015-01-28
    • 2019-07-01
    • 1970-01-01
    • 2015-05-02
    • 2019-07-05
    相关资源
    最近更新 更多