Gradle - Add JavaFX SDK to classpath - java

I am using gradle in Eclipse, and my gradle.build is pretty basic (adds java plugin, sets repos and not alot more) and I am building a JavaFX program. All my code compiles and run correctly with my build scripts with 0 errors.
I am just annoyed at the fact when I add the JavaFX SDK to my build path libraries, I can see my project has it listed. When I sync my project with Gradle, gradle removes this SDK from my classpath file.
What do I need to add to my build script to stop this from happening and gradle to normally inject it into my .classpath as it does with anything else I add?
Cheers,
P.S. I'm really new to gradle and groovy and this is my first 'project' working with it. Apart from this one annoyance, it's been smooth going.

Solved this issue: completely forgot classpath is todo witth eclipse and not java/gradle.
Adding:
apply plugin: 'eclipse'
eclipse {
classpath.containers 'org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER'
}
to my gradle.build file solved this issue.
I actually found a solution for this via https://groups.google.com/forum/#!topic/javafxports/Fn92C5ysC60 'android forum' while looking up how I could automate the building of eclipse.
Cheers if anyone looked into this.
As a side note, as I was confused about this first: A JavaFX project is no different from a Java project and you don't need to specify the fact you're using JavaFX to your ide to be able to execute JavaFX code. So I was confused why my IDE had a 'Start a new JavaFX project' and 'Start a new Gradle Project' but no JavaFX/Gradle project.
You don't 'need' a JavaFX plugin like my project originally had either.

To solve your problem, you need JavaFX-Gradle-plugin, it's a plugin that enable JavaFX support on Gradle project.
This is the link of plugin: https://github.com/FibreFoX/javafx-gradle-plugin, where you can find all infos and example...
All of you need is to start a new Gradle project, then add to your file build.gradle this code:
buildscript {
dependencies {
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
}
repositories {
mavenLocal()
mavenCentral()
}
}
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies{
// put here your project dependencies
}
apply plugin: 'javafx-gradle-plugin'
// these values are the examples and defaults
// you won't need them all
// configure javafx-gradle-plugin
jfx {
// minimal requirement for jfxJar-task
mainClass = 'YOUR.MAIN.CLASS'
// minimal requirement for jfxNative-task
vendor = 'YOUR NAME OR COMPANY'
// some optional task
jfxAppOutputDir = 'build'
jfxMainAppJarName = 'YOUR APPLICATION NAME.jar'
manifestAttributes = [
"Specification-Version": 1.0,
"Implementation-Version": 1,
"Built-By": "YOUR NAME OR COMPANY",
]
// for a full list of available settings, look the class "JavaFXGradlePluginExtension" on plugin project
}
This is the only plugin that I've found for use JavaFX with Gradle.
I'm working with Eclipse on project with same problem, and I've solved it a few days ago.
Hope this help,
BoGnY

Related

No option to run Java 9 project in IntelliJ IDEA

I'm not sure what I'm doing wrong here, I started working on a java 10 project (which of course uses java 9's new module system) so I suspect it has to do with that.
I have the following project directory structure:
However, the run button in IntelliJ is grayed out and it looks like intellij doesn't recognize this as being a properly structured Java project.
I've looked through the documentation, but a lot of it is circular explanations which cause more confusion than clarity.
I see that the .idea directory has a modules subdirectory which looks like it contains some relevant stuff - I'm probably doing something wrong here, but I don't know what.
Here's my build.gradle file, since I suspect this will be important here:
plugins {
id 'java'
id 'application'
id 'idea'
}
group 'com.chrismailloux'
version '0.1'
description = "Tutorial application developed to learn LWJGL and OpenGL."
sourceCompatibility = 1.8
task wrapper(type: Wrapper) {
gradleVersion = '4.9'
}
repositories {
mavenCentral()
}
dependencies {
// Dependencies left out for brevity
}
Can someone set me straight here? How can I fix this to be able to run the project?
The default folder structure in Gradle is src/main/java, but you use src/$project-name/main/java. I suspect that without further configuration, Gradle does not know where to find the sources and thus IntelliJ doesn't know either.
It looks like source sets (1, 2) are the answer:
sourceSets {
main {
java {
srcDirs 'src/$project-name/main/java'
}
}
}

Compile module with Gradle using Makefile

I have a Java/Gradle project with 3 different modules. One of these modules (let's call it orModule) has a dependency on or-tools, which unfortunately is not yet available through some public repository (like Maven) and therefore it has to be built from the source.
The team that's developing or-tools provides a Makefile for compiling/building the project and this is what I've been using so far. To be more specific I compile my orModule using the Makefile and then I run my main Java class, which belongs in the main module. Below are two screenshots of the Run Configurations I use for the two modules.
My goal is to move these configurations in the Gradle build system because I need the project to eventually be built with Gradle.
For the MainClass, I've added the following configuration in the root build.gradle:
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'com.some.package.MainClass'
applicationDefaultJvmArgs = ['-Djava.library.path=/orModule/lib']
And I think this is correct. However, I'm not sure how to "move" the Makefile configuration inside Gradle. The following is one of the alternatives that I've tried in the orModule/build.gradle but it fails.
task ndkBuild(type: Exec) {
commandLine 'make', ['EX=src/main/java/com/some/path/OrClass.java','PKG=com/some/package'], 'cjava', file('Makefile')
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
Any help on how to correctly write this configuration with Gradle, would be greatly appreciated.
I've solved this by getting rid of the Makefile altogether. Here's the original answer.
This was solved quite some time ago so if anyone has a simpler solution by now, please post it and I'll change the accepted answer.

Make IntelliJ resolve dependencies from modules in same project

Using IntelliJ 2016.2.5, I seem to be unable to make it resolve Gradle dependencies which are in the same project.
Project structure is as follows:
firstModule
-> build.gradle // 1
-> settings.gradle // 2
secondModule
-> build.gradle // 3
-> settings.gradle // 4
Contents of first build.gradle (1):
group 'de.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {mavenCentral()}
dependencies {}
And settings.gradle (2):
rootProject.name = 'test'
The contents of the second build.gradle (4) are:
group 'de.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {mavenCentral()}
dependencies {
compile ('de.test:test:1.0-SNAPSHOT')
}
And second settings.gradle (4):
rootProject.name = 'testdep'
Both modules are imported as Gradle projects and are set to auto-import enabled.
I know from maven projects, that IntelliJ - as well as Eclipse - does resolve those dependencies to the respective modules in the project/workspace. But with Gradle it seems to not recognize the dependencies. After every change in the module test I need to run the explicit gradle tasks clean and build before the module testdep seems to pick up the changes. And this process is not even reliable, if I don't change the version. This is most likely because of the gradle caching, but it is annoying, nevertheless.
Using the command line argument --refresh-dependencies is not a real solution because it makes the build times of our project (the one above is only for demo purposes) unbearable. Also, I would love to not having to use the gradle calls explicitly.
Any ideas/improvements how to handle such a situation?
Does it work with Eclipse, any experiences?
Will this be fixed in IntelliJ 2016.3 (I saw some improvements in the gradle area for that release).
What you are trying to do will be possible using the new Composite Builds functionality in Gradle. Support for IntelliJ IDEA is coming soon.

Unable to resolve generated classes

I'm using Dagger 2 with Gradle and have everything setup and code generation is working properly.
My build.gradle:
task wrapper(type: Wrapper) {
gradleVersion = '2.11'
}
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
... omitted as irrelevant to question ...
compile 'com.google.dagger:dagger:2.0.2'
compile 'com.google.dagger:dagger-compiler:2.0.2'
compile 'javax.inject:javax.inject:1'
}
}
My problem is that I am unable to resolve the classes and use them in my source, any solutions I've found are targeted towards Android which I am not using. How would I be able to resolve these generated classes as dependencies?
I had a similar issue some time ago. In your case I would say that you need the apt plugin. Check this question where I explained how I resolved it
I fixed this issue by enabled annotation processing in my IDE as well as adding Dr. Pelocho's answer.
Apply this answer: https://stackoverflow.com/a/33445767/1787084 to your build.gradle
Add apply plugin: 'eclipse' in your build.gradle
Enable annotation processing to the apt directory created by the ltgt gradle apt plugin in Eclipse by navigating to project properties -> Java compiler -> Annotation processing -> Enable project specific settings -> Enable annotation processing
Change generated source directory to build/generated/source/apt/main to match the ltgt default directory
Click "OK" or "Apply"
This added the Dagger generated classes to my build path and classpath

Intellij IDEA can't import Gradle project - "Could not initialize class javax.crypto.SunJCE_b"

I'm working on a Gradle project. The project downloads dependencies and runs perfectly fine when I do gradle run. Here's my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'
mainClassName = 'myProject.MyMainClass'
repositories {
mavenCentral()
}
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.7'
compile 'org.jsoup:jsoup:1.7.2'
testCompile "junit:junit:4.11"
}
jar {
baseName = 'myproject-service'
version = '0.1.0'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
Now I want to import the project in Intellij IDEA.
Import Gradle project >> Use default gradle wrapper (recommended) >> OK
However, when I hit OK it comes up with a dialogue saying:
Could not initialize class javax.crypto.SunJCE_b
Looking in the logs, it says
org.gradle.tooling.GradleConnectionException: Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.3-bin.zip'.
...
Caused by: java.lang.NoClassDefFoundError: Could not initialize class javax.crypto.SunJCE_b
at javax.crypto.KeyGenerator.a(DashoA13*..)
...
2015-03-18 21:31:17,751 [2354198] WARN - nal.AbstractExternalSystemTask - Could not initialize class javax.crypto.SunJCE_b
com.intellij.openapi.externalSystem.model.ExternalSystemException: Could not initialize class javax.crypto.SunJCE_b
at org.jetbrains.plugins.gradle.service.project.AbstractProjectImportErrorHandler.createUserFriendlyError(AbstractProjectImportErrorHandler.java:106)
...
I have no idea how to get this project to work. None of my imports from external repositories work inside IDEA without this (they all appear red). Any help would be appreciated. I'm on:
Intellij IDEA 13.1.6
Java EE 7 (jdk1.7.0_75.jdk)
Gradle 2.3
Mac OS X
SOLUTION:
Still have no idea what the issue is. I tried all the suggestions I could find online and everything I could think of: Install a different version of java, install a different IDEA, try using the original cryptography policy files, try using the unlimited strength policy files and more. None of these worked.
What did work, however, was selecting "Use a local gradle distribution", instead of using the default wrapper. I gave it my gradle install path (/usr/local/gradle-2.3), found by running which gradle (that will give you the path to the executable, namely /usr/local/gradle-2.3/bin/gradle, but I just took the directory part). Now I can build using gradle!!
My best guess is that the gradle plugin installed some other distribution of gradle that wasn't set up properly for my project.
I hope this is helpful to anyone else struggling with using gradle in Intellij IDEA.

Categories