How to add Gradle to existing JavaFX Project (IntelliJ) - java

From this post (Best way to add Gradle support to IntelliJ Project) I can see that what I need to do is "Add build.gradle in your project's root directory."
I was hoping someone could please explain how I do that in intelliJ? (been searching and reading - still baffled).
I understand a root directory the folder which is the parent for all the project sources, but in standard JavaFX project in intelliJ what is it/how do I find it/assign it, and then how do I add build.gradle?

Note: these steps assume that you are using the latest JDK version (17).
In the project browser, right click on the project name and create a file named build.gradle:
Write a build script. Here is a template for JavaFX applications:
plugins {
id "application" // Use Application plugin
id "org.openjfx.javafxplugin" version "0.0.9" // Use JavaFX plugin
}
mainClassName = "my.package.Application" // Set this to your main class
repositories {
mavenCentral()
}
javafx {
version = "16" // JavaFX Version
modules = [ "javafx.controls" ] // JavaFX modules. Add them to this array.
}
Once you have finished, import the project:
This may take a while depending on your internet speed.
You may have to adjust runtime configurations to use Gradle, but that shouldn't be necessary.
You may encounter the following error:
BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 61
If that is the case, install Gradle CLI (if you haven't already), and open a command prompt/terminal in the project folder.
Run the following command (works on Windows/Mac/Linux):
gradle wrapper --gradle-version=7.3 --distribution-type=bin
Once you have done that, reload the Gradle project.
I could not resist another GIF:
You may also need to rearrange the source folders:
I apologise for the cat photo, but it was just a placeholder image. I definitely didn't specifically choose it.

Related

Running Eclipse JUnit Tests with Gradle Builds of Modular Java (JavaFX) Projects that can also generate executable images with Jlink

I have struggled to find a clean way to work with gradle-built modular Java FX projects (using JDK11), that can produce nice deployable jlink images, whilst still being able to perform Junit tests within the Eclipse IDE. I have:
A JavaFX app in my package in the src/main/java folder alongside the necessary info-module.java file.
JUnit tests in a corresponding package in the src/test/java folder, which obviously doesn’t contain an info-module.java because Eclipse doesn’t tolerate >1 module per project.
A gradle.build file with:
plugins { id 'java-library' id 'application' id 'eclipse' id 'org.openjfx.javafxplugin' version '0.0.12' id 'org.beryx.jlink' version '2.12.0'}
dependences {testImplementation 'junit:junit:4.13'}
javafx { version = "16" modules = [ 'javafx.controls', 'javafx.fxml' ]}
mainClassName = "{my-package}.App"
eclipse { classpath {file {whenMerged {entries.findAll {it.properties.kind.equals('lib') }.each {it.entryAttributes['module'] = 'true'}}}}}
jlink {options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages'] launcher { name = 'mini'}}
With this configuration, I can easily run my JavaFX app from within Eclipse (using gradle -> application -> run task) and could build a lovely deployable jlink image (using the jlink task). However, I could not run JUnit tests within my IDE, which I often like to do to test bits of code and code ideas as I go along – perhaps partly because I’m a bit of an amateur, albeit one with a fair bit of experience.
Having scoured the internet for solutions, I’ve ended up trying various things like adding “requires junit” to the info-module.java file, fiddling with build path configurations to add a JUnit4 library to the Module Path (which would obviously disappear every time I refreshed gradle), and trying JUnit5. I’ve found nothing satisfactory at all. Surely Gradle (or perhaps an Eclipse plugin therefor) should have a neat solution? Is there a one-size-fits-all solution here?
After fiddling around, my solution is:
To allow JUnit tests to work:
a. Comment out all content within the module-info.java file – this temporarily yields a non-modular project.
b. Comment out the eclipse {…} plugin commands in build.gradle – this temporarily breaks the eclipse modular behaviour and jlink task.
c. Refresh Gradle.
You can then run JUnit tests, and carry on working on your Project, so long as you don’t need modularity or jlink tasks.
To restore modularity and jlink tasks for image building:
a. Uncomment out (restore) the content of the module-info.java file.
b. Uncomment out (restore) the eclipse {} plugin commands in build.gradle.
c. Refresh Gradle.
This isn’t too much of a pain, so long as it’s practical to work without modularity whilst performing the JUnit tests. However, please let me know if there’s a way to avoid all this faffing – perhaps there’s a Gradle (or Eclipse plugin) solution?

The project uses Gradle 2.10 which is incompatible with Android Studio 2020.3

While learning android studio from udacity they asked me to download a project; I did so but while I was importing it to android studio I got an error message:
The project uses Gradle 2.10 which is incompatible with Android Studio 2020.3.
Why did I get this error?
If you did not figure this out yet, I am just starting the same project too. I changed the classpath like above. I then found a website to use the latest version of gradle and the plugin. It says "You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or by editing the Gradle distribution reference in the gradle/wrapper/gradle-wrapper.properties file." I did the first one.
Then, I got errors for both build.gradle files in the project and app version. I get this error: Could not find com.android.tools.build:gradle:7.0.3
If you get this error, click the link "Add google Maven repository and sync project". Then you have to click "Do refactor".
Now you come across another error in one of the build.grade files. This stackoverflow link will tell you: Gradle - Error Could not find method implementation() for arguments [com.android.support:appcompat-v7:26.0.0]
Finally, replace testCompile with testImplementation and then you should get BUILD SUCCESSFUL.
The project's Gradle version you're trying to clone is outdated. Change it based on your android studio version.
on your build.gradle(Project) change the version and sync
classpath "com.android.tools.build:gradle:4.1.2"
Got the solution :)
In gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
In build.gradle project level:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
In build.gradle module level:
Replace all "compile" with "Implementation" and do other suggested version updated and you are done here :)
Hope this works for you, if yes just press that up button :)
If not getting any solution, the best you can do is copy the "app" folder of your project(using file explorer) and paste it in a newly created project(should be of same name as of your project), in that way you'll get already synced gradle and newly created version of your app.
Because, every important thing in your project is present in its app folder and when you create a new project with same name and copy app folder from your older version of your project, you will have a newer version of your project with all same resources.
Note: You can't move through branches and other commits
I saw this issue several times with Android studio. Below trick worked me the best.
Android Studio suggests to upgrade to newer version, please upgrade it and let it sync
Mostly you would see error message again
Now, Go to File -> Invalidate Caches/Restart
It should work now
If it shows same error then I don't know what to do.
If it shows different error then repeat from step 1 to 3 for new error.
Hope, it helps some..

How do I include a gradle composite build into a multi project build?

I have a project that is a library (ProjectLib) and is used in many other projects. I have another project which is a multi project build (MultiProject) with a few sub projects (SubProj1, SubProj2, CoreProj). Some of these sub projects depend on the library project.
Normally I have the sub projects that depend on the library have the library specified in the dependency block of each of their build scripts and it fetches a built version of my library.
Sometimes I have to develop something in my library for this multi project and while doing this I would like to include the library as a composite build so that I can make changes and see the effect in the multi project build.
I have tried adding the path to my library in the settings.gradle of the root project using 'includeBuild' but this only half works.
What I tried is this:
MultiProject settings.gradle
include "SubProj1", "SubProj2", "CoreProj"
includeBuild "../ProjectLib"
SubProj1 and SubProj2 build.gradle
dependencies {
implementation project(":CoreProj")
implementation "com.myCompany:ProjectLib:1.0.0"
}
The build file for the CoreProj doesn't depend on the ProjectLib.
My ProjectLib normally builds to a private repo which is fetched by gradle and so typically version 1.0.0 would be included from this repo. What I would like to happen is that instead of fetching this version of the library, gradle instead includes the project in my local directory so that it has my latest changes without me having to build and release the library to the repo.
What I am getting at the moment is that the ProjectLib is being included in my IDE (I am using IntelliJ) but I get the following warning:
org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'default' not found.
This warning appears twice for my MultiProject and the once each for SubProj1 and SubProj2. This also breaks up my project structure in my IDE so that it looks like only CoreProj is included in the multi project build MultiProject.
I am using gradle 5.5.1

How to run a springmvc+gradle project from command line?

In my springmvc project, I use gradle as a build tool.
But our FE developers always complain about that why they have to start the project in IntelliJ IDEA, but they don't use the IDE as their editor at all.
so, recently I made a lot of research on running my project from command line.
Please help.
If gradle is used and all is configured well, try gradle run while using the application plugin.
You do need to load the plugin
apply plugin: 'application'
and define the main class of your application
mainClassName = "org.gradle.sample.Main"
Further parameters can be defined as well.

Empty bin,libs,src folders after creating Android project using Javafx android-tools

After creating an Android project using Javafx android-tools, the folders bin, libs, and src were empty! I followed this tutorial: android / Building and deploying JavaFX Applications.
Have I missed something? Please help!
I think that tutorial is quite old... Now you can just use the last plugin they have realeased.
Go to Getting Started site, and check you have everything in place. Basically, you will need:
JDK8u40 early access release installed, JAVA_HOME should be set with the JDK path.
Gradle 2.2.1 installed
Android SDK
Android Build Tools 21.1.1 using SDK Manager.
Gradle Plugin for NetBeans or your IDE (optional)
And the plugin which is a 'build.gradle' file.
build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:javafxmobile-plugin:1.0.0-rc3'
}
}
apply plugin: 'javafxmobile'
mainClassName='org.javafxports.android.MainJavaFX'
repositories {
jcenter()
}
jfxmobile {
android {
applicationPackage = 'org.javafxports.android'
androidSdk = file ('<your path to android sdk>')
}
}
You can try the Ensemble 8 project that you can download from here or clone the project, and see for yourself how easy it is to port this application to Android.
You can create the apk as easy as typing on command line:
gradlew android
or this to create the apk and install it on your android device if you have it connected:
gradlew androidInstall
Once you have tested it, you will want to create your own project. So now you can create new projects with the Gradle plugin. Assuming you use NetBeans, you can create first a root project, and then add an empty subproject. On this subproject you can add all your JavaFX sources and you should override the default gradle.build file with the one shown above, adapting the path to your packages.
You will need the gradlew files and folder from the ensemble project, or if you have gradle on your path, then you can build the apk with the same commands, but using gradle android.
Important considerations
The plugin works with the last JDK8 8u40, but it doesn't support all Java 8 features (Streams and Optional), while it supports Lambdas. On the contrary, it supports mainly all the JavaFX 8 features.
It's in working progress, so some issues may not been solved yet, and you can report any problem you may have.

Categories