Understanding Gradle project dependancies, Gradle site example seems incomplete - java

I am trying to understand Gradle build dependencies but am having trouble using the examples on the Gradle website and wondered if anyone could help.
In the Adding dependencies to a task section of their website there is the following code to be saved in build.gradle:
project('projectA') {
task taskX {
dependsOn ':projectB:taskY'
doLast {
println 'taskX'
}
}
}
project('projectB') {
task taskY {
doLast {
println 'taskY'
}
}
}
They say that the Output of gradle -q taskX is:
> gradle -q taskX
taskY
taskX
But I get:
FAILURE: Build failed with an exception.
* Where:
Build file '/tmp/x/build.gradle' line: 3
* What went wrong:
A problem occurred evaluating root project 'x'.
> Project with path ':projectA' could not be found in root project 'x'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
With --stacktrace I get the following:
Caused by: org.gradle.api.UnknownProjectException: Project with path ':projectA' could not be found in root project 'x'.
NOTE: This build.gradle file is the only file in the /tmp/x folder.
------------------------------------------------------------
Gradle 3.4.1
------------------------------------------------------------
Build time: 2012-12-21 00:00:00 UTC
Revision: none
Groovy: 2.4.15
Ant: Apache Ant(TM) version 1.10.3 compiled on July 19 2018
JVM: 1.8.0_191 (Oracle Corporation 25.191-b12)
OS: Linux 4.15.0-43-generic amd64

This chatper from the documentation explains how to create dependencies between tasks from different (sub)projects of a same build (task Xfrom project projectA depends on task Y from project projectB in the example).
So you need to setup a multi-project build before trying to execute the given example: see basics in multi-project build here
In your case: you need to:
1) create two subproject directories 'projectA' and 'projectB'
2) Create the required settings.gradle file in root project directory, to declare/include the two subprojects
include ("projectA" , "projectB")

Related

Integration with JIB Gradle cause compilation error

Tring to integrate with JIB (Containerize your Gradle Java project) with my Java project.
Following this documentation: https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin
Added the following to my project:
dependencies {
classpath("gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:3.1.4")
}
apply plugin: 'com.google.cloud.tools.jib'
jib.to.image = 'my-docker-local/my-app'
Gradle version is: 5.6.4
The compilation with "./gradlew" build is working fine. but, when
excecuting "gradle jib" getting the following error:
gradle jib To honour the JVM settings for this build a single-use
Daemon process will be forked. See
https://docs.gradle.org/7.2/userguide/gradle_daemon.html#sec:disabling_the_daemon.
Daemon will be stopped at the end of the build
FAILURE: Build failed with an exception.
Where: Build file '/Users/igorgumush/dev/java11/my-service/build.gradle' line: 139
What went wrong: A problem occurred evaluating root project 'my-service'.
Could not find method compile() for arguments [org.springframework.boot:spring-boot-starter-web,
build_9nl2gys0kuhc8m4mdlq51u41r$_run_closure4$_closure24#6ea6088b] on
object of type
org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it
incompatible with Gradle 8.0.
Well the solution was very simple.
I used:
./gradlew jib
instead of
grade jib

Where/how to get the MSVC dlls Java 1.8.0_144 wants?

I'm experimenting with building my application with Java 1.8.0_144 to workaround an issue that apparently started after that. I run this command to build:
gradle jfxNative -Dorg.gradle.java.home="c:\Program Files\Java\jdk1.8.0_144"
and it stops with this error:
Execution failed for task ':jfxNative'.
> Not found MSVC dlls
Where and how do I get these MSVC dlls?
The full output looks like this:
c:\...\>gradle jfxNative -Dorg.gradle.java.home="c:\Program Files\Java\jdk1.8.0_144"
> Task :jfxNative
The jar lib\lombok-1.16.18.jar has a main class lombok.launch.Main that does not match the declared main tech.dashman.dashman.ConfiguratorApp
The jar lib\jna-4.5.0.jar has a main class com.sun.jna.Native that does not match the declared main tech.dashman.dashman.ConfiguratorApp
The jar lib\javassist-3.22.0-CR2.jar has a main class javassist.CtClass that does not match the declared main tech.dashman.dashman.ConfiguratorApp
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':jfxNative'.
> Not found MSVC dlls
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 8s
5 actionable tasks: 2 executed, 3 up-to-date
My current list of dependencies look like this:
dependencies {
compile "tech.dashman:dashmancommon:1.0.0-SNAPSHOT"
compile "org.projectlombok:lombok:1.16.18"
compile "org.springframework:spring-web:5.0.2.RELEASE"
compile "org.springframework.retry:spring-retry:1.2.2.RELEASE"
compile "com.fasterxml.jackson.core:jackson-databind:2.9.3"
compile "org.kordamp.ikonli:ikonli-javafx:2.1.0"
compile "org.kordamp.ikonli:ikonli-fontawesome5-pack:2.1.1"
compile "net.harawata:appdirs:1.0.1"
compile "io.sentry:sentry:1.6.4"
compile "org.javassist:javassist:3.22.0-CR2"
testCompile "junit:junit:4.12"
}
I tried adding
compile "net.java.dev.jna:jna-platform:4.5.1"
to that list but I'm still getting the same error when trying to build the installer.
Adding it to my buildscript dependencies did not change the error either:
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath "de.dynamicfiles.projects.gradle.plugins:javafx-gradle-plugin:8.8.2"
classpath "com.github.ben-manes:gradle-versions-plugin:0.17.0"
classpath "de.dynamicfiles.projects.javafx.bundler:custom-file-extension-windows-bundler:1.0.2-SNAPSHOT"
classpath "net.java.dev.jna:jna-platform:4.5.1"
}
}
Maintainer of the javafx-maven-plugin/Author of the javafx-gradle-plugin here!
Even if this is a very old post, and I did not see this one before (sorry), I might have an answer to this issue. And it happens even today.
While debugging an issue on the plugin (https://github.com/javafx-maven-plugin/javafx-maven-plugin/issues/395) I found that sometimes there are 2 files missing inside the JDK itself.
In case someone has installed any non-Oracle JDK, e.g. OpenLogic, the provided files are incomplete.
The JDK provided by OpenLogic (other others) provides a file called ant-javafx.jar, where all the system native files are contained (can be found inside the installed JDK in the lib-folder). That file is missing the required runtime files, that are required by the launcher.
Here a screenshot of what it looks like with OracleJDK:
Here a screenshot of what it looks with OpenLogic JDK:
As you can see, there are files missing in there, which makes that JDK not equivalent to the OracleJDK sadly.
On maven-plugin level I can not do anything here to fix this, but you might be able to fix that by modifying that JAR-file by adding the missing files. You should be able to find them on your local windows installation at C:\Windows\System32\vcruntime140.dll or C:\Windows\System32\msvcp140.dll. If these files are missing, you just can download a official runtime installer of these Visual C++ Redistributable files here:
for 32-bit: https://aka.ms/vs/16/release/vc_redist.x86.exe
for 64-bit: https://aka.ms/vs/16/release/vc_redist.x64.exe
Another alternative would be to install a different OpenJDK, for example ojdkbuild (https://github.com/ojdkbuild/ojdkbuild). Using chocolatey on Windows just makes this a one-liner choco install ojdkbuild8 and does contain these required files.

gradle check fails in travis-ci

I've added travis-ci to my github project. Project itself is multi-moduled and consists of :client, :server and :shared.
In the build.gradle of :client module I have organised sourceSets as follows:
sourceSets {
main {
java {
runtimeClasspath += project(":shared").sourceSets.main.java.srcDirs
srcDirs += project(":shared").sourceSets.main.java.srcDirs
}
}
}
Now, when travis-ci runs gradle check it fails with following:
$ gradle server:clean server:check
FAILURE: Build failed with an exception.
* Where:
Build file '/home/travis/build/bduisenov/mockserver/client/build.gradle' line: 55
* What went wrong:
A problem occurred evaluating project ':client'.
> Could not find method main() for arguments [build_ab0sue8nexlysud7wfupu5ddr$_run_closure4_closure12#3fb9a67f] on project ':client'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 10.05 secs
The command "gradle server:clean server:check" exited with 1.
Although running locally, I have no issue with it.
Any thoughts on that?
I think that the error simply comes from the gradle version on Travis CI using an old version 2.0. If you generate your gradle wrapper first then Travis CI will use that instead of the built in version - See https://docs.travis-ci.com/user/languages/java#Projects-Using-Gradle. I did try your project locally and there is no problem on Gradle 2.12.

Gradle builds, then ignores sub-project

I have a gradle project in big-project/ and a sub-project in big-project/lib/.
In big-project/'s big.project.Main class I import big.project.lib.Utils, which is defined in the big-project/lib/ sub-project.
When I try to run gradle build, this is what happens:
:lib:
compileJava UP-TO-DATE
:lib:processResources UP-TO-DATE
:lib:classes UP-TO-DATE
:lib:jar
:compileJava
big-project/src/main/java/big/project/Main.java:3: error: package big.project.lib not exist
import big.project.lib.Utils;
^
1 error
:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 6.243 secs
For some reason, gradle compiles the code of the subproject, and then decides to ignore it. How do I fix that?
These are my gradle files:
big-project/settings.gradle:
include 'lib'
big-project/build.gradle:
evaluationDependsOnChildren()
allprojects {
apply plugin: 'java'
sourceCompatibility = '1.8'
version = '1.0'
group = 'big.project'
}
apply plugin: 'war'
dependencies {
compile project(':lib')
}
big-project/lib/build.gradle is empty
I did study the gradle manual: Chapter 56. Multi-project Builds, but nothing useful came from this.
TL;DR: My big-project/lib/src folder wasn't set up correctly (source code was in the wrong sub directory). As this answer states, Java sources need to be in src/main/java.
Thanks to comments on the question I was able to look in the right place for a solution. As suggested, I looked at a working sample project. As I was using Eclipse, I generated a new Gradle project based on the flat-java-multiproject sample. This enabled me to eliminate my build.gradle files as the source of the error.
Next, I decided to inspect the build/ artifacts of the lib/ project and discovered that the .jar file did not contain any .class files. That explains the compiler error I reported in the question. This caused my to see if all .java sources were in the right place, and as mentioned above, they weren't. Fixing this, fixed the compiler error.
The whole crux was that while I was writing my code in eclipse, all references to the lib project's classes seemed to be found.

Gradle 1.1 vs 2.0: Could not determine the dependencies of task ':compileAcctJava'

Gradle 1.11 gives me a deprecation warning:
robert#pferdeapfel:~/prj> gradle --version | grep Gradle
Gradle 1.11
robert#pferdeapfel:~/prj> gradle compileJava
Converting class org.gradle.api.internal.file.DefaultSourceDirectorySet to File using
toString() method has been deprecated and is scheduled to be removed in Gradle 2.0.
Please use java.io.File, java.lang.String, java.net.URL, or java.net.URI instead.
:compileAcctJava
:processAcctResources UP-TO-DATE
:acctClasses
:resources
:acct UP-TO-DATE
:beans UP-TO-DATE
:svninfo UP-TO-DATE
:settings UP-TO-DATE
:compileJava UP-TO-DATE
BUILD SUCCESSFUL
Total time: 10.865 secs
and Gradle 2.0 fails the build:
robert#pferdeapfel:~/prj> /usr/local/gradle-2.0/bin/gradle --version | grep Gradle
Gradle 2.0
robert#pferdeapfel:~/prj> /usr/local/gradle-2.0/bin/gradle -i compileJava
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':compileAcctJava'.
> Cannot convert the provided notation to a File or URI: acct Java source.
The following types/formats are supported:
- A String or CharSequence path, e.g 'src/main/java' or '/usr/include'
- A String or CharSequence URI, e.g 'file:/usr/include'
- A File instance.
- A URI or URL instance.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option
to get more log output.
BUILD FAILED
Total time: 3.676 secs
However, I have no idea why and where in my build script the problem is.
I have defined source sets as described in http://www.gradle.org/docs/current/userguide/java_plugin.html#sec%3asource_sets like this cause my project does not follow the default project layout.
sourceSets {
...
acct {
java {
srcDir "src"
srcDirs("src") {
include "my/program/some/package/Acct.java"
}
}
}
...
}
I do have some dependencies defined, but not for acct; this is just a simple enum that's used as a base for code generation. It has no dependencies. Even if I add a dummy dependency like below, I get the same error.
dependencies {
...
acctCompile junit
...
}
How can I fix this or how can I find out more about what exactly went wrong?
I don't find the output of Gradle's --debug and --stacktrace helpful at all.
There is no such notation as srcDirs("src") { include "my/program/some/package/Acct.java" }. Try:
sourceSets {
acct {
java {
srcDirs = ["src"] // replaces default rather than adding another dir
include "my/program/some/package/Acct.java"
}
}
}

Categories

Resources