No option to run Java 9 project in IntelliJ IDEA - java

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'
}
}
}

Related

MissingPropertyException for Gradle Plugin - Cannot find task type

I am currently in the process of re-organizing and re-structuring some of the projects we use at work.
One of my goals is to integrate a project's unit tests correctly. I have upgraded the root projects Gradle to the latest version (5.6.2) and I have made all the necessary DSL changes.
Right now I'm in the process of including the project's integration tests under its source code. For this I created a new subproject.
So far so good but when I attempt to build the project it fails with following exception:
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'StartServerTask' for project ':integration-tests' of type org.gradle.api.Project.
at org.gradle.internal.metaobject.AbstractDynamicObject.getMissingProperty(AbstractDynamicObject.java:84)
at org.gradle.groovy.scripts.BasicScript$ScriptDynamicObject.getMissingProperty(BasicScript.java:156)
at org.gradle.internal.metaobject.AbstractDynamicObject.getProperty(AbstractDynamicObject.java:61)
at org.gradle.groovy.scripts.BasicScript.getProperty(BasicScript.java:65)
Now, the task in question comes for a Gradle plugin a colleague created in the past. Said plugin is fetched from a maven repository as a dependency like so:
buildscript {
repositories {
maven { url 'maven_link' }
}
dependencies {
classpath 'fts.gradle:start-server:0.3'
}
}
plugins {
id 'java'
id 'application'
id 'eclipse'
}
And applied like
apply plugin: 'fts.gradle'
I have tried almost everything, even changing the package structure of the plugin, but I am unable to include it properly in the build file.
If I copy paste the code directly within the project's build.gradle everything works fine but it doesn't so if I try to remote fetch and include the plugin.
Can anyone provide any insight to this?

IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources

I'm using Antlr in a simple Kotlin/Gradle project, and while my Gradle build is generating Antlr sources, they are not available for importing into the project.
As you can see (on the left), the classes (Lexer/Parser, etc.) are being generated. I have also configured this generated-src/antlr/main directory as a Source Root. Most questions I see list this as a solution, but I've already done it.
The issue persists after multiple rebuilds (both in IDEA and on the CLI), and following all the usual "Invalidate Cache and Restart" issues.
Further, the import issue is listed in the Gradle build on the CLI so it doesn't seem isolated to IDEA.
What am I missing here?
Here's the build.gradle file produced by IDEA when I was creating the project initially, and which IDEA is using for project/workspace synchronization.
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.50'
}
group 'com.craigotis'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
apply plugin: 'antlr'
dependencies {
antlr "org.antlr:antlr4:4.5"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Try adding this to your build.gradle:
sourceSets {
main.java.srcDirs += "${project.buildDir}/generated-src/antlr/main"
}
generateGrammarSource {
arguments += ["-visitor", "-package", "com.craigotis.sprint.core.antlr"]
outputDirectory = file("${project.buildDir}/generated-src/antlr/main/com/craigotis/sprint/core/antlr")
}
compileKotlin.dependsOn generateGrammarSource
Shouldn't it locate the compiled classes and not the sources? Do you see the antlr generated classes in the target directory?
Try this: first build the project without referencing or using any ANTLR generated classes, and only after the build is successful, then add the code that references them.
(In other words, what I think that happens, is that your ANTLR sources are compiled after the code that references them. They never have a chance to compile because build fails before)
Also if this is really the case, you can solve it also by splitting into two artifacts and make sure the ANTLR one is built before the one with the code that uses it
Try to add generated sources in idea module like this post from Daniel Dekany here:
apply plugin: "idea"
...
sourceSets.main.java.srcDir new File(buildDir, 'generated/javacc')
idea {
module {
// Marks the already(!) added srcDir as "generated"
generatedSourceDirs += file('build/generated/javacc')
}
}

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.

Gradle - Add JavaFX SDK to classpath

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

Gradle provided dependencies with Intellij

I'm trying to build a Bukkit plugin. The plugin also uses exp4j. The final result needs to have the exp4j code included in the released jar but not have the Bukkit code included.
I followed the advice of this answer to copy the dependencies in and used this answer to declare Bukkit as provided. My build.gradle now looks like this:
apply plugin: 'java'
apply plugin: 'idea'
configurations {
provided
}
sourceSets {
main.compileClasspath += configurations.provided
test.compileClasspath += configurations.provided
test.runtimeClasspath += configurations.provided
}
dependencies {
provided "org.bukkit:bukkit:1.8.8-R0.1-SNAPSHOT"
compile "net.objecthunter:exp4j:0.4.5"
}
jar {
// copy the dependencies across
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
This works great and I can build and run the project happily using Gradle from the command line. The trouble is that Intellij (or maybe it's the Gradle idea plugin) doesn't recognise the provided dependencies, thus, importing anything from Bukkit causes it to incorrectly report an error.
How can I get the provided dependencies to play nicely with idea?
Other things I've tried:
I've also tried declaring the provided scope like this:
configurations {
provided
compile.extendsFrom provided
}
But this causes the provided dependencies to be copied into the final jar. I've also tried the plugins as recommended by this answer, but both cause Bukkit to be copied into the created jar. I've further tried declaring Bukkit to be runtime scope instead provided, but that simply caused lots of compile errors (but interestingly Intellij did have Bukkit listed as a dependency)
I have trouble believing that this has not been asked before, but I've searched and cannot find a full solution. I'm new to Gradle, so apologies if this is a super simple thing to do.
In Gradle 2.12 and later, there is a configuration called compileOnly that has the provided semantics you are looking for.
More about this configuration on the Gradle blog post on the subject.
Before 2.12, you can use the nebula.provided-base plugin to create a provided configuration with all the correct semantics.
See Gradle issue here.
There isn't a provided configuration in gradle, though there really should be one. The most reasonable workaround currently seems to be, to create your own configuration:
configurations {
provided
}
and then:
sourceSets {
main {
compileClasspath += configurations.provided
}
}
The problem with extendsFrom is that the provided dependency will end up being bundled in your distribution anyway unless if you add another explicit exclude, defeating the whole point of provided.
Edit: To tell idea to use the provided dependencies, you could apply the 'idea' plugin and then:
idea {
module {
scopes.PROVIDED.plus += [ configurations.provided ]
}
}
see more here.
I did find a very hacky solution. But it's so bad I feel bad posting it here :P
Declare the provided dependencies as runtime dependencies
Regenerate the idea files: gradle cleanIdea idea
Idea should now recognise the dependences
Change the provided dependencies back to provided
Go into the project settings and convert all the dependencies with runtime scope to Intellij's provided scope
Stuff works :) (Just don't ever regenerate the idea files)
Obvious problems, anyone using your project has to do the same hack. And every time you regenerate the idea files, the same thing will have to be repeated.

Categories