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.
Related
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?
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'
}
}
}
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
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.
I'm playing around with a Gradle java project, and I'm having a difficult time getting annotation processor's to run. For some reason when I run an intellij configuration (pictured below), the annotation processors aren't running. I'm assuming this is because the configuration has the Make command configured to run before launch. The annotation processors seem to run when assemble or build is called.
The issue is reproducible when calling ./gradlew clean make. I don't have that issue when calling ./gradlew clean assemble, or ./gradlew clean build. What's the best practice for getting around this?
IntelliJ needs Annotation Processing enabled for the project. Here is an image that details where you can enable Annotation Processing for IntelliJ:
Preferences > Build, Exection, Deployment > Compiler > Annotation Processors > Check "Enable annotation processing"
make sure that Annotation Processing is enabled for your project (as described by #spierce7)
also make sure that apply plugin: 'idea' is in your build.gradle
sample build.gradle snippet:
plugins {
id "net.ltgt.apt" version "0.5"
}
apply plugin: 'java'
apply plugin: 'idea'
...
dependencies {
compile 'com.google.dagger:dagger:2.10'
apt 'com.google.dagger:dagger-compiler:2.10'
}
from: https://github.com/tbroyer/gradle-apt-plugin (github for net.ltgt.apt plugin)
IntelliJ IDEA
When the idea plugin is applied, the idea task will auto-configure the
generated files to enable annotation processing in intelliJ IDEA.
When using the Gradle integration in IntelliJ IDEA however, rather
than the idea task, you'll have to manually enable annotation
processing: in Settings… → Build, Execution, Deployment → Compiler →
Annotation Processors, check Enable annotation processing and Obtain
processors from project classpath. To mimic the Gradle behavior and
generated files behavior, you can configure the production and test
sources directories to build/generated/source/apt/main and
build/generated/source/apt/test respectively and choose to Store
generated sources relative to: Module content root.
Note that starting with IntelliJ IDEA 2016.1, you'll have to uncheck
Create separate module per source set when importing the project.
In any case, the idea plugin has to be applied to the project.
An alternative, starting with IntelliJ IDEA 2016.3, is to delegate the
IDE build actions to Gradle itself:
https://www.jetbrains.com/idea/whatsnew/#v2016-3-gradle
You are not applying the APT plugin
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
And
apply plugin: 'com.neenbedankt.android-apt'
Or for the core, its pure-Java alternative:
https://plugins.gradle.org/plugin/net.ltgt.apt
Also try using apt instead of providedCompile