Home > Net >  Java files dosen't see maven dependencies
Java files dosen't see maven dependencies

Time:12-07

So I've been trying to set up my already existing project with maven and when I try to install it I get javafx.*** does not exist for every javafx import. I've tried changing the project structure but it never works. This is my pom:

<?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>com.group-12</groupId>
  <artifactId>snake</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>snake</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>19</maven.compiler.source>
    <maven.compiler.target>19</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx</artifactId>
    <version>19</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
  </dependencies>

  <build>
      <sourceDirectory>src/main/java</sourceDirectory>
      <testSourceDirectory>test/main/java</testSourceDirectory> 

    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Here is my file structure.


├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── com
│   │           └── group-12
│   │               ├── Controller.java
│   │               ├── Draw.java
│   │               ├── Food.java
│   │               ├── Fruits.java
│   │               ├── Game.java
│   │               ├── GameScene.fxml
│   │               ├── Grid.java
│   │               ├── GridPos.java
│   │               ├── Main.java
│   │               ├── MainGame.java
│   │               ├── Snake.java
│   │               ├── StartMenu.fxml
│   │               └── Window.java
│   └── test
│       └── java
│           └── com
│               └── group-12

I tried having different packages for different files but in the end I just but it all in the same folder.

I'm using neovim also so shouldn't be an IDE problem.

The application also worked perfectly before I tried to switch over to maven with no build tool.

CodePudding user response:

I did add maven to an already existing project a while ago and I had the same problem. I got rid of it by clicking the "Reaload All Maven Projects" button in Intellij. That is the button that looks like a "refresh button" on the top left of the maven window

CodePudding user response:

Approach

As James suggests:

  1. Use the correct JavaFX dependencies e.g javafx-controls.
  2. It is better to start with a maven project that works than one that is broken.
  3. Follow official documentation at openjfx.io.

Incidental

I advise getting rid of the sourceDirectory and testSourceDirectory settings as your values are already the default layout and maven works best when you follow the principle of convention over configuration.

Also, none of the lockdown stuff is really needed (my opinion, others could differ), so remove everything in pluginManagement.

Don't use the exec-maven-plugin for executing JavaFX apps via Maven

To address your comment:

I'm at least getting a different error, when I run mvn clean install exec:java

Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.1.0:java (default-cli) on project snake: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:3.1.0:java are missing or invalid -> [Help 1]

Don't use the exec-maven-plugin to execute JavaFX apps, it does not work.

The exec:java goal will try to run the JavaFX apps in the same process as the maven process, which won't have the JavaFX modules available.

You can use exec:exec then specify the java executable to execute and it will execute in a separate process, but then it won't have the module path by default. You can supply the module path (supposedly via command line using -Dexec.args="%modulepath", though that did not work for me) or via configuration in the pom.xml, which I tried, but that did not work either. The JavaFX modules in maven are defined strangely with empty modules for non-classified dependencies and classified dependencies for the actual implementation. The exec plugin module handling is not smart enough to work that out, so can't find the JavaFX modules.

Use the javafx-maven-plugin for executing JavaFX apps via Maven

Instead, use the javafx-maven-plugin, as is also documented at openjfx.io.

Copied from the plugin documentation (with some simplifications):

Create a new Maven project, use an existing one like HelloFX.

JavaFX dependencies are added as usual:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>19</version> 
</dependency> 

Add the plugin:

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.8</version>
    <configuration>
        <mainClass>hellofx/org.openjfx.App</mainClass>
    </configuration> 
</plugin>

Run the project:

mvn javafx:run

Note that (as this is a modular project in this example), the main class is fully qualified by module name and package name in addition to class name. Options for specifying the main class for non-modular projects are provided in the javafx-maven-plugin documentation.

  • Related