How to specify Java version for Gradle buildSrc? - java

My build.gradle uses a custom task, which I defined in the buildSrc/src/main/java directory using Java 11. Now, I need to build on a different machine which only has Java 8 installed, so the Gradle build cannot even configure because it complains of errors in my custom task. Of course, I could install Java 11 in my home directory (since I don't have root privileges on this machine) and run Gradle with a custom JAVA_HOME, but is there a way that Gradle could automate this process. That is, could I somehow declare the Java version required for buildSrc so that Gradle will download and use that version?
Gradle toolchains looks somewhat promising, but it is not obvious how this should be applied to buildSrc.

Related

How to change the Gradle version?

I would like to change the Gradle version to 3.5.1 according to the project version received from git using this eclipse.
So I changed the editing path for the system environment variable, but if you enter 'gradle -v' in cmd, only 4.4 appears.
I looked for various solutions on the Internet, but it didn't work out.
How can I change the version?
And how can I erase the existing version 4.4 completely? I tried to erase it, but it kept popping up even if I deleted all the related files.
gradle -v:
One of the easiest way to change the gradle version is using sdkman. Install the required version you want and change whenever you want. Then hit
sdk install gradle <versionId>
sdk use gradle <versionId>
To check the list of versions installed
sdk list gradle
First, you need to know where Gradle is installed on your device. You can type where gradle in your terminal and the path will pop up.
Then, after you deleted the old version, go to the download page to download version 3.5.1. After download, follow the instructions in Installing manually in the Gradle installation guide.
Make sure that the old version is not included in your environment variables. If so, remove the old version and add the new one.
You can check Windows path variable by typing echo %PATH% in CMD.
Using Gradle Wrapper
To quote the manual:
If your existing Gradle-based build uses the Gradle Wrapper, you can easily upgrade by running the wrapper task, specifying the desired Gradle version:
./gradlew wrapper --gradle-version=7.4.2 --distribution-type=bin

Does Gradle require JDK to compile Java?

Does Gradle requires JDK to compile Java source files?
or it is using its own internal compiler?
Thanks in advance
Short version: JRE for installing and running, Groovy build script compiled by Gradle own compiler, JDK for Java source code compilation.
Long version: There are multiple points of interest:
1) Installing & running
In order to install Gradle and run Gradle build you need a JDK or JRE which version is at least 7.
Gradle uses mainly Groovy as a language but it comes with its own library and ignores any other Groovy library that is installed. Therefore the compiler is the Groovy compiler of the Groovy library that Gradle comes with.
As a side note, you can also use Kotlin for scripting builds as described here. Which of course involves a Kotlin compiler.
You can check the JVM version by running gradle -v.
This means that during installation the runtime configured in JAVA_HOME will be used.
Also the same happens for running a build, unless you are explicitly overriding JAVA_HOME in gradle.properties using the following property org.gradle.java.home. This property and others are defined here.
JAVA_HOME can point to a JDK or JRE. In this case does suffice to have only a JRE installation.
2) Source Code Compilation
To remove any confusion, source code, means that you have your project sources, written in a language, which need to be compiled into bytecode. At previous point the code being written is the actual build script code which is a separate thing, compiled and handled by Gradle.
Source code is being compiled using Gradle plugins, being it java, groovy, kotlin, or anything else.
You can compile your JAVA source code by using the java plugin or the newer java-library plugin. The two are similar.
The JDK being used will be the one configured in JAVA_HOME or can be explicitly defined in gradle.properties using the following property org.gradle.java.home.
JAVA_HOME needs to point to the JDK not a JRE for this case.
As stated above, Gradle can only run on Java 7 or higher. But it can be configured to compile, run, test, javadoc for Java 6 by following these steps. Therefore this advanced customization can be used to handle some corner cases which may not have a resolution in simply changing gradle.properties.
The two plugins provide also two properties:
sourceCompatibility - Java version compatibility to use when compiling Java source
targetCompatibility - Java version to generate classes for.
These are related to the source language version used as input and the target bytecode version to be generated.

Portable App with Maven launch4j and NSIS

I would like to be able to use Maven to automatize the building process of my application.
The application is a runnable jar that is wrapped with a local copy of a jre using launch4j, which is then included in a NSIS script I have to build an installer.
I found a launch4j plugin for Maven that makes me able to wrap a jar with a local jre like I want to, but the thing is that I'd like to download the proper jre in a local directory when running the Maven build process.
The reason is that for compatibility reasons, the application will be using the jre 1.6 but that could change in the future.
Is there a way to make Maven download the jre corresponding to the application setting like it would download a dependency ?
Like if I change the configuration to use the 1.8 jre, Maven would download automatically the jre 1.8 in the directory I want.
Is this something possible to do or do I have to download it manually and add the jre directory to my project (or something else that I didn't think of) ?
I found a launch4j plugin for Maven that makes me able to wrap a jar
with a local jre like I want to
How did you achieve this, since launch4j doesn't wrap the jre, nor bundle/embed?
It only lets you specify a
<path>relative_jre_path_to_your_jar<path>
to a jre. You need to zip your *.exe(which results from launch4j execution) with the jre, and distribute it.
Regarding the controlling of jre versions, you can deploy jre artefacts(different versions) to your maven repository and use the dependency mechanism.

Installing and importing javafx on windows 7

I have installed jdk1.7.0_07 and changed PATH but i still cannot import javafx, is there something that i should do fix this?
Make sure that /jre/lib/jfxrt.jar is on your compile path.
For example for the 64 bit jdk7u6 version on win7, the jfxrt.jar is located here:
C:\Program Files\Java\jdk1.7.0_06\jre\lib\jfxrt.jar
jfxrt.jar was left off of the java runtime path on purpose for jdk1.7.0_06 until further testing between JavaFX and rest of the java infrastructure has been completed. This means that non-JavaFX programs cannot possibly be impacted by possible compatibility issues which may be caused by JavaFX. To date I have never encountered any compatibility issue - this was just a cautious move by Oracle in this regard I believe.
In a future release the jfxrt.jar should be added to the default compile and runtime classpath for Java and some of the information below should be irrelevant. You can track the request to add jfxrt.jar to the default java runtime.
Compiling and Running a JavaFX program from the command line
Example below is for a JavaFX application class named javafxsamples.AudioPlaylist
If you are compiling from a command line, compile with:
javac -cp ".;C:\Program Files\Java\jdk1.7.0_06\jre\lib\jfxrt.jar" javafxsamples/AudioPlaylist.java
To run from the command line, you can use:
java -cp ".;C:\Program Files\Java\jdk1.7.0_06\jre\lib\jfxrt.jar" javafxsamples.AudioPlaylist
Though, it is recommended that you package your applications with the javafxpackager, rather than manually adding jfxrt.jar to your classpath (javafxpackager packaged applications will embed a launcher which finds jfxrt.jar and adds it to the classpath for you).
javafxpackager -createjar -nocss2bin -appclass javafxsamples.AudioPlaylist -srcdir . -outfile AudioPlaylist.jar
After that you can run the app without needing to specify a jfxrt.jar location on the classpath:
java -jar AudioPlaylist.jar
Compiling and Running a JavaFX program using IDEs
NetBeans
If you are using NetBeans 7.2+, you can create a JavaFX project type and it should automatically find JavaFX jfxrt.jar and place it on your project's classpath when you set up jdk1.7.0_07 as your platform.
Eclipse
If you are using e(fx)clipse make sure you are using the latest version (0.0.14+) which is features better facilities for detecting JavaFX.
Idea
Intellij Idea 11.1.3 will automatically add all of the files from the jre lib directory to it's project classpath, so you shouldn't get compile errors with it. Note that Idea's behaviour is erroneous in this regard, it shouldn't really do this, but it in the end you end up with the expected behaviour of being able to compile and run your JavaFX classes from idea.
Building a JavaFX program using maven
Make the jfxrt.jar a system dependency for your maven project to get it on the path.
Use the maven antrunner to execute the javafx ant tasks for deployment packaging.
An example of packaging JavaFX with maven is provided in this maven project.
Even if you use an IDE or Maven for your build, it is still recommended, you package your app for delivery using the javafx ant tasks or javafxpackager utility as this should provide the most robust deployment solutions for your application.

How create a installable msi package for my java project?

This installer will first check if jvm is present on the system or not. If not then it will first install the jvm. After that it will include the java code (may be in jar) alongwith the database used. And finally it will create a desktop shortcut and make changes to registry as other s/w does.
First you need to decide on a setup authoring tool. Here is a list which can get you started:
http://en.wikipedia.org/wiki/List_of_installation_software
Advanced Installer is one of the tools which has a dedicated Java project type.
To detect and install JVM, you can add it as a prerequisite. This is done differently for each setup tool.
There is a new tool being developed for that: jpackage
If your project is build using Gradle then you can easily use the Badass jlink plugin: https://github.com/beryx/badass-jlink-plugin
to build an installer / package using jpackage
Here's an article how to build an app image using OpenJDK 11 and using OpenJDK 14 with jpackage only for building the installer / package:
https://walczak.it/blog/distributing-javafx-desktop-applications-without-requiring-jvm-using-jlink-and-jpackage

Categories