How to match dependencies between IntelliJ and Gradle - java

So I've got a simple test app with a Gradle file that bundles JavaFX into the jar such that I can run "java -jar test.jar" from the terminal. It currently specifies 11.0.1 from OpenJFX. Now I can run from IntelliJ using their JBR, which has JavaFX included, but it uses some 10.0.2-internal version I don't want. Of course I could add each module manually via VM options, but ideally I have a solution that doesn't require me to do this for every configuration in all my projects. Really what I want to do is ensure the JFX versions used by IntelliJ and Gradle are the same.
I've tried adding in the JFX modules as Dependencies to my own module, adding them as Global Libraries, even adding the jar's to the SDK via classpath, but none of these solve the "JavaFX runtime components are missing" error. How can I get IntelliJ to match the version of JavaFX in my Gradle file?

Related

Generate Eclipse launch configurations with Gradle

We use Eclipse to launch and debug our Java GWT project. We have a couple of eclipse launch configuration files with many classpath entries needed for running the project.
Previously we used ant to build our project and had all our dependencies present on our local storage, with all classpaths pointing to libraries explicitly for project as well as launch configuration classpaths.
Once we migrated over to Gradle 6.6 and used public repositories to download our dependencies, almost everything worked smoothly - all our project classpaths were generated correctly by the gradle build. However, our eclipse launch configurations still contain the old hardcoded classpaths and I'm unable to find a way to generate these launch configurations using those gradle configured classpaths. As a result we've had to maintain the dependencies on our local filesystem so that the configurations have access to them in order to debug and run our code.
Is there a way Gradle can be used to generate these launch configurations so that we can get rid of the libraries on our filesystem and rely on whatever it pulls from the repositories during a build?
Any suggestions or workarounds will be appreciated.
It you're using Eclipse's built-in Gradle tooling (aka, Buildship) and let Eclipse generate your launch configuration, it will automatically be in sync with the project's build path, which is automatically kept in sync with the dependencies declared in your Gradle build file.
Here is an example Gradle project I just created, where I added some dependencies and the launch configuration "just works." The project is named "lib," notice under Classpath Entries is "Project Dependencies"; that's where Eclipse's Gradle tooling maintains and syncs with what's in the build.gradle file.
Here's what the project's Build Path looks like if I inspect it:
Whenever you add a dependency to build.gradle, it will automatically be included there; the launch configuration(s) then reference the project dependencies so the launches are also always in sync.
You might need to delete your existing launch configurations and let Eclipse generate new ones to get this.

How to deploy a JavaFX project with maven, including custom and non-modular dependencies?

I am currently in the process of trying to deploy my JavaFX application (either with a jar or an installer, however an installer would be preferred). I have seen some sources point to using jlink, in which I have tried to no avail.
While I would have no problem building normally with jlink, I am trying to include a custom serialization utility I made with Maven and Java 17 (no JavaFX, but it is a single modular with a module-info). To import this into my project, I am simply adding it as a dependency. Additionally, this dependency requires SnakeYAML, which I don't believe is a modular project? (Important later)
Now, with this dependency, if I try to run javafx:jlink, I typically get an error like "jlink does not work with automatic modules" (in reference to my serialization utility).
My setup:
JavaFX 17.0.1
JDK 17.0.1
Maven 3.8.4
IntelliJ IDEA ultimate
So, my main two questions:
Should I even bother with jlink, especially if I am trying to get an installer?
With modular projects like my JavaFX project, how do I add dependencies from other modular projects (and also, within the serialization utlity, somehow shade SnakeYAML) and allow them to be compiled with JavaFX?
Should I even bother with jlink, especially if I am trying to get an installer?
No.
You can make use of jlink during the process, but, in my experience, it is not necessary. Also, the images jlink creates can be a little difficult to integrate with what is required by jpackage, which you will need to use anyway.
Instead, use jpackage, not jlink if you want an installer.
jpackage can both link your application and create an installer, but jlink can only link and cannot create an installer.
With modular projects like my JavaFX project, how do I add dependencies from other modular projects (and also, within the serialization utlity, somehow shade SnakeYAML) and allow them to be compiled with JavaFX?
Don’t shade jars when packaging, it is not necessary. Also, if the jars are modular, it will break their modularity and they might not work (for example the JavaFX jars are not supported when shaded into a jar).
To add compile dependencies, you are already using Maven, just use its compile dependency mechanism.
For jar dependencies which you have created yourself, use the Maven install command to install the dependencies into your local repository, or use the install facilities in your repository manager (e.g. jfrog artifactory), if you use one.
The modern maven build process and the IDE integrations (at least Idea anyway) will automatically configure the correct paths for compilation of the declared dependencies so that they are available from the module path if they are modular and the class path or as an automatic module if they are not modular.
Include the dependent jars in your package. This can be done two ways:
if the jars are modular, link the jars into the runtime image included in the package:
Both jlink and jpackage can do this, but when creating an installer, use jpackage for the linking not jlink as jpackage will ensure all files in the image are in the right location for the installer, but jlink will not, in my experience using Java 17 tools.
if the jars are not modular, then include them in a lib directory in the package. In the startup script for your application, which is included in the package, place the lib directory for the non-modular library files on the class path, so that they can be found.
Additionally, this dependency requires SnakeYAML, which I don't believe is a modular project?
Follow the tutorial at:
https://github.com/dlemmermann/JPackageScriptFX
A description of JPackageScriptFX:
demonstrates how projects can use scripts to build self-contained, platform-specific executables and installers of their JavaFX applications via the jdeps, jlink, and jpackage tools
the scripts do not try to create a fully modularized solution but instead try to enable existing projects / applications, which often use non-modularized 3rd party dependencies, to be packaged

How to build a executable JavaFX 11 application

Problem
I can't create "JavaFX Application" Artifact on IntelliJ IDEA, It
gives " 'javafx:deploy' is not implemented in this SDK." error.
I don't want to use Maven/Gradle because;
With Maven: I can't use my local jar library, I tried including as a lib, using Jitpack, Adding something in POM but none of them worked.
With Gradle: I don't understand Gradle, but I think it has same issue with maven (local libraries).
So I need a solution without Maven/Gradle.
Details
I tried creating "JAR Application" Artifact and built it but it doesn't run with double click or console command. I am sure I installed and configured JavaFX & jmods correctly.
What I want
Build an JavaFX executable, which runs without needing console command.
System Properties
OS : Windows 10 21H1
IDE : IntelliJ IDEA Ultimate 2021.1.3
JDK & JavaFX SDK version : 11
I use FXML and Scene Builder (embedded into IDE)
Project has Multiple Classes and Scenes.
Just go for jpackage. Here is the link to the documentation. https://docs.oracle.com/en/java/javase/16/jpackage/packaging-overview.html#GUID-C1027043-587D-418D-8188-EF8F44A4C06A
Not a solution without Gradle/Maven but I highly recommend you try using Gradle. Create a new Gradle Project (very easy if you use IntelliJ), and you only need to add dependencies in the build.gradle file. Example of what I use to make a Gradle Project using JavaFX :
plugins {
// Apply the application plugin to add support for building a CLI application in java.
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.9'
}
repositories {
mavenCentral()
}
dependencies {
// This dependency is used by the application
implementation 'com.google.guava:guava:29.0-jre'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
// Use JUnit test framework
testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
testCompile('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}
run {
standardInput = System.in
}
application {
mainClass = "com.Nephty.main"
}
javafx {
version = "15.0.1"
modules = [ 'javafx.controls' , 'javafx.fxml' , 'javafx.media' ]
}
Two important points : repositories, application and javafx.
In repositories, you specify the repository from which you want to download the librairies, so you don't have to keep them on your local machine. This helps sharing your program, as another user won't have to download and setup JavaFX himself (this can be impossible for a non tech-savy).
In JavaFX, you specify the modules you want to use and the version.
In application, you specify your main class which will be ran.
Then, you can do a console command : graldew.bat run (with cwd as your project directory) or use the easily accessible task in IntelliJ. After that, you can create a shortcut that will automatically run the console command without needing you to open the console and type everything out. If you are the only who is going to use your application, this is the same as having an executable (if we only care about using the program).
There is an application that helps building executable files for java application. This video explains it.
Hopefully you can find what you're looking for here. If not, don't mind replying so we can try to figure something out.

What is org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER and how do I make it work in IntelliJ?

I have a Java codebase that was written with Eclipse that I am trying to work with in IntelliJ.
The codebase consists of about 20 Java packages built alongside each other as separate projects. (There are about twenty directories, each containing a .classpath and .project file.) They have dependencies on each other. Presumably everything builds just fine in Eclipse.
I had IntelliJ import these from the Eclipse build format. It mostly seemed to work except that Project Settings -> Modules -> Dependencies shows that many projects have an unresolved dependency on org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER. This appears to come from the following entries in the .classpath files:
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
The packages with this dependency do not build in IntelliJ because they can't resolve the dependencies on the other packages.
I suspect that org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER is some Eclipse-specific trickery that doesn't translate to a different IDE, but I can't figure out exactly what is going on here.
What is org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER? Do I have to do anything special to a build environment that uses it to make it work outside Eclipse?
IntelliJ Ultimate 2016.1.2, Java 1.8.0_91, OS X 10.11.5
I worked around this problem by going into Project Settings->Modules and manually adding module dependencies between my various projects until everything built. Tedious but effective.
I'm leaving this question up here, though, because a short definition of org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER would still have been helpful to me.
Intro
The IntelliJ IDEA Maven-2 plugin integrates Maven-2 into IntelliJ IDEA.
First,
Second,
Third,
Similar to the Ant plugin, the Maven-2 plugin allows execution of goals (or phases) and redirects output (with error highlighting) to the message window.
Installation
Available for IDEA 6.0 (until version 1.1 also for IDEA 5.0) through IDEAs built-in plugin manager or directly from
{{http://plugins.intellij.net/plugin/?id=1166}}.
In case you downloaded the plugin, simply extract the content of the binary archive into your IDEA plugin directory.
The extracted folder (idea-maven-plugin) contains another folder lib, where all the JARs reside.
Usage
Assuming you have specified the environment variables JAVA_HOME and M2_HOME the plugin should be ready to run without any further configuration.
In case JAVA_HOME is not defined, the plugin uses the internal IDEA JDK.
If M2_HOME is not defined, you must specify the home directory of Maven-2 in the configuration dialogs. This is a change starting from version 1.1 of the plugin.
Before version 1.1, Maven was called via the start script provided with the normal Maven installation. In order to get better access to the running Maven process Maven now is called directly by starting a Java process. This for example allows terminating the running Maven process by the user.
The configuration can be done in two ways:
Either via the IDE Settings or the Project Settings.
The project settings always override the application settings!
The plugin will not work with IDEA versions prior to 6.0!
The plugin was tested under Windows and Linux. It was not tested under Mac OS X.
Resource Link:
Maven 2 Integration
Import Maven dependencies in IntelliJ IDEA
I've come up with a solution that works for me, but the bounty is still open (because I can't close it :P ) and will go to someone who can provide a more detailed explanation.
The solution: Run mvn eclipse:clean and mvn eclipse:eclipse. This causes maven to clear the .classpath file and rebuild it from the pom.xml. Somewhat mysteriously, it no longer contained the dreaded reference to org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER.
Perhaps this is a deprecated tag/variable in Eclipse.

Gradle IDEA Plugin can't find JDK on Mac

PLEASE NOTE: THIS IS NOT A DUPLICATE. I want Gradle's IDEA plugin to correctly configure my IntelliJ IDEA CE project for me. I am not interested in any solution that involves manually tuning IntelliJ to find my JDK. This is a Gradle IDEA plugin feature that can and should work.
If you can find another question that involves correctly getting the Gradle IDEA plugin to configure IntelliJ (running on a Mac) so that it can find JDK 8+, then please by all means, mark this as a dupe and provide a link to that question. Otherwise, do not vote this as a dupe (it's not!).
Mac 10.9.5 here. Java 8 is my default JRE/JDK, and I just installed Groovy 2.4.6 and Gradle 2.13 via sdkman. I then installed IntelliJ IDEA CE.
On my terminal, I created a test-proj directory, and then inside that directory I issued the following Gradle command:
gradle init --type groovy-library
Gradle executed successfully, giving me a Groovy project skeleton. I then edited the generated build.gradle to contain the IDEA plugin:
apply plugin: 'idea'
And then I ran:
gradle wrapper
./gradlew clean idea
This generated the Gradle Wrapper for me, and I then used the IDEA plugin to generate IntelliJ project files for me. I then opened my brand-spanking-new IntelliJ IDE and went to Open my test-proj.
The project opened and everything appeared to be OK. But then I started coding and noticed that JRE classes such as String were not showing up as resolvable. So I went to File >> Project Structure and see this:
So it appears that IntelliJ can't find my default Java 8 JDK. I know the OS can find both the JRE and the JDK, based on the console ouput of java -version and javac -version. But something, between sdkman, Gradle or IntelliJ is preventing the IDE from finding Java. Any ideas?
I had the same problem, although i got it running by removing the .idea folder and reimporting the project by pointing to my build.gradle file. In the import dialog i selected the .ipr structure instead of using the idea "folder based structure" (default)
When executing gradle idea it will then generate an .ipr file in your project root configuring your project.
In my case the jdk was set properly and my modules were initialized correctly.
The reason behind using the .ipr files is, that the gradle idea plugin can't work with the directory based structure. See also:
directory based idea project with gradle
https://issues.gradle.org/browse/GRADLE-1041 (open for 6 years now)
Since i figured this out today i'm not sure how good this is working. Or what are the differences between directory based and .ipr based (.ipr based seems older?).

Categories

Resources