CookieHoodie
CookieHoodie

Reputation: 11

How to convert JavaFx project to Gradle project?

I'm building a JavaFx project using Eclipse and I have a file structure like this:

Project │ .classpath │ .gitignore │ .project │ build.fxbuild │ README.md │ (sqlite).db (*local database this project operates on) │ ├───.settings │ ... │ ├───bin │ ... │ ├───lib │ ... │ └───src ├───application | ... │ Main.java │ ├───controllers │ ... │ ├───css │ ... │ ├───fxml │ ... │ └───resources 123.jpg

How do I convert this into a gradle project? I'm totally new to gradle and I have hard time finding documentation to get started.

*I'm using JavaFx 8 and jdk-9.

Upvotes: 1

Views: 1407

Answers (1)

Peter Walser
Peter Walser

Reputation: 15706

I have hard time finding documentation to get started.

Gradle is well-documented, if you google you find a lot of beginner's tutorials as well, and there's lots of examples on GitHub. See https://guides.gradle.org/building-java-applications/

The documentation is quite overwhelming, and you need to familiarize yourself with a bunch of concepts too. As a starter, you need to grasp following concepts:

  • Project structure
    Gradle uses the default folder structure introduced by Maven:
    src/main/java for Java sources
    src/main/resources for resources (get bundled into the JAR/WAR/EAR)
    src/test/java for Java test sources
    src/test/resources for test resources
  • Build file
    The build.gradle file defines your project and how it should be built. Each project/artefact has a groupId, artefactId and version.
  • Build tasks
    Gradle projects use plugins (such as the 'java' plugin for building Java projects). Learn about the build tasks (clear, test, build, deploy, execute gradle tasks to list all tasks.
  • Dependencies
    External libraries you use shouldn't be downloaded and put into the project lib folder, but rather you declare dependencies to libraries (browse for them on Maven Central. Gradle will take care of the dependency management and download libs and transient dependencies automatically.
  • External resource loading
    You project contains a file-DB. Is it read-only? Bundle it as resource and load it over the class loader, not a file. Is it writeable? Keep it outside the project, and access it over a URL or file path. Consider to make the URL/path configurable (command line args).

Here's the generic build.gradle which I use as a starter for my projects (with a cleaner approach to managing dependencies), for JDK 9/10/11+ you can change the compatibility java version to 9/10/11+:

defaultTasks 'clean', 'build'
apply plugin: 'java'

description = 'Example Java Project'
group = 'org.test'
version = '1.0.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = 'UTF-8'

repositories {
    jcenter()
}

ext.libs = [
        testbase: [
                "junit:junit:4.12"
        ],
        loggingAPI   : [
                "org.slf4j:slf4j-api:1.7.25"
        ],
        loggingImpl  : [
                "org.slf4j:jcl-over-slf4j:1.7.25",
                "org.slf4j:jul-to-slf4j:1.7.25",
                "ch.qos.logback:logback-classic:1.2.3"
        ]
]

dependencies {
    testCompile libs.testbase
    compile libs.loggingAPI
    compile libs.loggingImpl
}

Upvotes: 3

Related Questions