Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Spring Boot build plugin with JavaFX app

I used Netbeans to generate a template for my JavaFX app. It generated a POM.xml and the build section seems excessively complicated. Furthermore, it unpacks ALL the dependencies every time i compile the project and it takes like 3 minutes every time. Here is the relevant section of the POM.xml file :

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

This is my first JavaFX project, in my other projects i just use this build section and everything works fine :

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

However, when trying to use it with this project, i get the following error :

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

Why won't this work with a JavaFX project? It seems like it wants to execute goals that are not even in my POM file.

EDIT

I do see that goal in the effective POM tab but I don't know enough about Maven to make sense of it, I can't change anything in the effective section anyway. Anyone knows how to make the Spring Boot plugin work with a JavaFX desktop app?

Any help would be appreciated.

Thanks!

like image 420
Martin Avatar asked Aug 31 '18 18:08

Martin


People also ask

Can I use spring with JavaFX?

For now, we have successfully created a JavaFX application which is launched and managed with Spring, and allows us to use the convenient features of any Spring application.

Can we run Spring Boot application without @SpringBootApplication?

Uses. It's not mandatory to put @SpringBootApplication to create a Spring Boot application, you can still use @Configuration and @EnableAutoConfiguration individually as shown in the example given in the next point.


2 Answers

You need to refactor the pom file. The configuration must go outside of the executions, like this:

<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>
like image 105
fl0w Avatar answered Oct 17 '22 21:10

fl0w


I finally managed to make this work without unpacking the dependencies every time i compile. I used the de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport class to extend my main class from, put launch(args); in the main method, and I now have the following code in the build section of the POM.XML file :

<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>
like image 3
Martin Avatar answered Oct 17 '22 23:10

Martin