How to execute a Gradle task before subprojects are evaluated - java

I've set up a Gradle task to auto generate one of the subprojects of my Gradle build on which another depends (reason for doing this: long story involving Apache Cordova!). So the root build.gradle contains this autogenerate task that creates a "CordovaLib" sub project. The build.gradle in the other sub project (that isn't autogenerated) depends on CordovaLib:
dependencies {
compile project(':CordovaLib')
}
Is there a way to execute the autogenerate task before the non-generated subproject's build.gradle is evaluated (specifically the above line)? I'm using Gradle 1.11 on JDK 1.7 and as it currently stands I can't even run gradle tasks without it failing due to the missing project.

It isn't possible to execute a task before build files have been evaluated, at least not without complications such as one build executing another build using a GradleBuild task. You are likely better off checking the generated project in to source control, or finding a solution that doesn't involve generating build scripts.

You can use init script to gradle to achieve this.
https://gradle.org/docs/current/userguide/init_scripts.html

Related

How to use delomboked sources in gradle build / test?

I'm using Lombok in a gradle based project and the build process requires to delombok the source code before continuing with unit tests or production build. What is the best way to achieve something like that in gradle?
At the moment the generated classes are created in a /build/delombok directory.
The first thought I had was to create a new source set and a task that is based on compileJava but if I do this I will probably have to update every other gradle task to depend on my new one. Is this the right approach or is there something better?
I'm using gradle 4.10

How to add Gradle nature for Gradle 2.0 to the Eclipse project

I have a project that can be compiled by Gradle 2 from the command prompt. But it cannot be compiled by Gradle 3.
In Eclipse I am trying to apply Configure/Add Gradle Nature. Then I am getting an error:
“org.gradle.tooling.BuildException: Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-3.5-bin.zip'.”
Is there any way to configure the project for Gradle 2?
Actually this BuildException is thrown when a Gradle build fails or when a model cannot be built.
Suggestion#1: Using Refresh Dependecy:
You can refresh dependencies in your cache with the command line option --refresh-dependencies.
Suggestion#2: Using deleting previous jars:
You can also delete the cached files under ~/.gradle/caches. With the next build Gradle attempts to download the dependencies again.
Suggestion#3: Using wrapper is the best solution:
If your project is previously built and deployed using specific version, then you don't need to make headache to use another version. You can easily do it by adding wrapper in build.gradle file.
// Running 'gradle wrapper' will generate gradlew - Getting gradle wrapper working and using it will save you a lot of pain.
task wrapper(type: Wrapper) {
gradleVersion = '2.2'
}
For more, you can go through this tutorial: The Gradle Wrapper
What the Gradle Wrapper does?
When you run the Gradle Wrapper it performs the following actions:
Parse the arguments passed to gradlew
Install the correct Gradle version
Invoke Gradle to run the specified tasks
The wrapper is effectively completely decoupled from Gradle itself.
Wrapper configuration:
One of the files the wrapper puts in your project is a configuration file at gradle/wrapper/gradle-wrapper.properties.
This file typically looks something like this:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
Here,
distributionBase + distributionPath --> specify the path at which the wrapper will store Gradle distributions.
By default GRADLE_USER_HOME is ~/.gradle, so the wrapper will store Gradle distributions at ~/.gradle/wrapper/dists.
zipStoreBase and zipStorePath are very similar. These specify where the wrapper will store the zipped distributions it downloads.
distributionUrl --> It specifies what version of Gradle you want to use for your builds and where to download it from.
Resource Link:
Understanding the Gradle Wrapper
2 situations :
1) You are using the Gradle wrapper, set it like that :
task wrapper(type: Wrapper) {
// Use the proper version
gradleVersion = '2.6'
}
2) You are not using the Grade wrapper, change the global setting in Gradle's settings page :

gradle depedencies and cache

I have a simple testing-purpose gradle project, which I want to scan its dependencies using gradle dependencies command. Before I do so I want to make sure that the project's dependencies are actually found in the gradle's cache (.gradle/caches/modules-2/files-2/1). To do so I run the gradle assemble command to download the missing dependencies before scanning.
I found out that its working only if the project has a src/main/java folder with a Java file inside it (even if that Java file is completely empty).
Is this a valid workaround? Is there any better solution to guarantee the dependencies are found in the cache folder before scanning them?
What is the reason that you want to do that?
assemble task assemble your source files, if there is nothing to assemble the task is not needed to run. The fact you are adding the java file to src its a hack to run this task and its children tasks.
Depending on what you want to achieve there are few ways to 'scan' dependencies.
For more info you can visit https://docs.gradle.org/current/userguide/userguide_single.html#sec:listing_dependencies
Aditionally:
There is a netflix plugin that I believe can scan through your gradle scripts a check unused dependencies https://github.com/nebula-plugins/gradle-lint-plugin
There is a plugin that can scan the vulnerabilities of used dependencies etc https://jeremylong.github.io/DependencyCheck/dependency-check-gradle/

Gradle: Multiproject build - How to determine projects that need a rebuild?

I have a multi project build setup. If I execute the "jar" task of any subproject, gradle checks whether it needs to rebuild a certain dependent project or not by using the org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.
Is there a way to access this information to like build a custom task or a task in a custom plugin which automatically copies the jars of theses projects to somewhere?
You should be able to use jar.didWork to determine whether the task jar actually did some work or not if I remember correctly: https://docs.gradle.org/current/javadoc/org/gradle/api/Task.html#getDidWork()
Or maybe more appropriate, use something like the following:
gradle.taskGraph.afterTask { task, state ->
// check anything on Task or TaskState, like didWork, executed, failure, noSource, skipMessage, skipped or upToDate
}

Perform additional tasks within a maven build

I am wondering about how to perform specific tasks during a maven build: I would like to use some of my code to do some preprocessing on the data that I am shipping in the resulting jar. Generally given some input.xml in src/main/resources I would like to be able to call a java function / main method to obtain a file output.xml which is included available as a resource (and probably placed in target/classes/...). Using Makefiles this would correspond to an additional rule, I guess this could be done with an ant task as well (though I have never used ant myself). can I add such a rule to a maven project as well?
You can use the Maven Exec Plugin to run arbitrary Java code during your build.
If you should happen to have your tasks formulated as Ant targets, the Maven Antrun Plugin can be used to run those.

Categories