Depend on dynamically generated gradle project - java

I have a multi-module gradle project that looks something like the following
books
books-api
books-server
books-tests
books-client // generated using 'gradle swagger'
The books-api contains service.swagger.json file that I'd like to use to generate a Java based client using Swagger codegen gradle plugin. The books-test module is a test suite that will use the generated client to run tests agains the server.
The Swagger codegen gradle plugin creates an entire gradle module books-client. Currently, the module is generated by running the swagger task on the root project. I have to perform this step manually before running a gradle build on the root project so that the books-test modules dependency on books-client is satisfied.
Is there anyway to generate books-client and have books-test depend on it for just running gradle build on the root project? I.e. gradle build would generate books-client and build books-test successfully.

Why do you create the whole gradle module dynamically? Or do you mean you just create the source of the module dynamically?
I'd suggest that you do not create the gradle module dynamically, but have it there with all the necessary dependencies and just let the swagger plugin generate the code. As far as I see in the plugin, the swagger task comes before compileJava anyway.
So my suggested workflow would be that you have the gradle module there and then use the standard gradle build workflow. Or are you running into problems with this approach?

Related

Gradle Plugin dependency

What is the exact dependency I need to develop a Gradle Plugin in Java? Ideally I would like to get it from a well-known repository such as Maven Central or similar.
I have a Maven project with a core functionality and I just added two extra plugins, one for Ant, one for Maven. They are already tested and working; easy! Now, I wanted to add a third module for a Gradle plugin to make this functionality also available from any Gradle project.
However, I can't find the exact dependencies I need to develop a Gradle plugin.
The Gradle docs (such as https://docs.gradle.org/current/userguide/java_gradle_plugin.html) are not very well written to say the least. They mention:
the gradleAPI() dependency
or the java-gradle-plugin dependency
But they are quite unclear... no group, no version (really?).
If anyone can enlighten me to where I can get these dependencies from, I would be very thankful.
Gradle's public and internal APIs, aka gradleApi(), are bundled with the Gradle distribution and not independently published and therefore not easily consumable by Maven builds. There's the pending epic #1156 (Ensure plugin cross-version compatibility by allowing a user to depend on gradlePublicApi()) that might help here.
Since Gradle plugins are best to be built with Gradle, a pragmatic solution is to invoke the Gradle build from Maven and attach the produced artifact to the Maven build. Andres Almiray (aalmiray) once described this in the blog post Running Gradle Inside Maven (Web Archive Link). He describes the following high level steps:
Create a new Maven module (e.g. gradle-plugin) and add attach it to the parent POM
In the POM of gradle-plugin add a dependency to your core module. Use the maven-dependency-plugin to store dependencies to the Maven build folder, e.g. target/dependencies.
Create the build.gradle, add a Maven repository that points to target/dependencies (step 2) and let it depend on the core module as well as gradleApi(). Implement the Gradle plugin.
Use the exec-maven-plugin to invoke the Gradle build.
Use the maven-resources-plugin to copy the Gradle built plugin jars to the standard Maven build folder.
Use the build-helper-maven-plugin to attach the copied jars to the Maven build.
Sample project to be found here (gradle-in-maven).
https://docs.gradle.org/current/userguide/custom_plugins.html#sec:custom_plugins_standalone_project
In here it is mentioned that it is gradleApi() and I know that this works (from experience). The localGroovy() on that page is only needed if your plugin code uses groovy (does not apply if you only use groovy in the build.gradle of your plugin).
java-gradle-plugin is a library that makes it a bit simpler to make plugins, it is not required though. I personally prefer using gradleApi only.
EDIT:
It appears I've misunderstood the question. Here are the steps to get gradleApi jar:
Create a Gradle project with your desired Gradle version.
Add implementation gradleApi() dependency.
Import/run the project once.
Go to your .gradle folder (located in home folder in Linux-based operating systems).
Open caches folder
Open the version folder you want, e.g. 6.0.1
Open generated-gradle-jars folder.
Copy the jar to wherever you want and use it.
For me the 6.0.1 jar is at ~/.gradle/caches/6.0.1/generated-gradle-jars/gradle-api-6.0.1.jar
Please note that I have not tested this, I know the jar is there but I haven't tried using it.

Gradle multi-project build order using Kotlin script

I use Kotlin DSL script (.kts) for building. There the structure of my project is:
Root project 'demo'
+--- Project ':backend'
\--- Project ':frontend'
I need to build project frontend first, than backend. I tried
include(":frontend")
include(":backend)
and
include(":frontend", ":backend")
with and without : in settings.gradle.kts of root project, but still the order of build is alphabetical - backend, than frontend.
View source code on GitHub
Do you have any ideas what is wrong?
There is nothing wrong. If you don't specify any inter-project dependencies, Gradle will execute them in alphabetical order. This should be fine if the two projects are unrelated, as they are now.
But let's say you like to build the frontend (using node) and then include those resources in the backend (using Spring Boot). Then you will need to make the backend depend on frontend project. Then Gradle will honor the dependency graph and build the frontend first.
There are many ways to do that. One is to use the java plugin in the frontend to build a jar file of your frontend resources. You can then make a normal project dependency to it. You could also make a dependency directly into the frontend project's "internal" build tasks, but that is a bit frowned upon. Or you could declare your own artifact, or do a it in a bunch of other different ways.
For the first approach, you can build a jar file of your frontend resources like this:
plugins {
// ...
id("java")
}
java {
// Required to make the jar artifact compatible with your backend, which is configured for Java 1.8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.named("jar", Jar::class) {
dependsOn("assembleFrontend")
from("$buildDir/dist")
into("static")
}
Then in the backend, depend on it like this:
dependencies {
// ...
runtimeOnly(project(":frontend"))
}
There are a few other things wrong with your build script as well.
The runtime configuration is deprecated; use runtimeOnly instead (for your spring-boot-devtools dependency).
A multi-project should only have a single settings.gradle file, but you have one in each project. Delete them except for the one in the root folder.
You have declared the org.siouan.frontend plugin twice: once using the recommended way and once using the "old" way. Remove the latter (everything in the buildscript block and the apply statement.
Also, while I am not familiar with the org.siouan.frontend plugin, it appears it does not declare inputs and outputs for you - probably because it is very generic. So to avoid running npm each time you build your backend (as you now have a dependency to the frontend), you should declare proper inputs and outputs for the frontend tasks like installFrontend and assembleFrontend.

How do I include a gradle composite build into a multi project build?

I have a project that is a library (ProjectLib) and is used in many other projects. I have another project which is a multi project build (MultiProject) with a few sub projects (SubProj1, SubProj2, CoreProj). Some of these sub projects depend on the library project.
Normally I have the sub projects that depend on the library have the library specified in the dependency block of each of their build scripts and it fetches a built version of my library.
Sometimes I have to develop something in my library for this multi project and while doing this I would like to include the library as a composite build so that I can make changes and see the effect in the multi project build.
I have tried adding the path to my library in the settings.gradle of the root project using 'includeBuild' but this only half works.
What I tried is this:
MultiProject settings.gradle
include "SubProj1", "SubProj2", "CoreProj"
includeBuild "../ProjectLib"
SubProj1 and SubProj2 build.gradle
dependencies {
implementation project(":CoreProj")
implementation "com.myCompany:ProjectLib:1.0.0"
}
The build file for the CoreProj doesn't depend on the ProjectLib.
My ProjectLib normally builds to a private repo which is fetched by gradle and so typically version 1.0.0 would be included from this repo. What I would like to happen is that instead of fetching this version of the library, gradle instead includes the project in my local directory so that it has my latest changes without me having to build and release the library to the repo.
What I am getting at the moment is that the ProjectLib is being included in my IDE (I am using IntelliJ) but I get the following warning:
org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'default' not found.
This warning appears twice for my MultiProject and the once each for SubProj1 and SubProj2. This also breaks up my project structure in my IDE so that it looks like only CoreProj is included in the multi project build MultiProject.
I am using gradle 5.5.1

Use maven transitive dependency in build

Is there a way to use the transitive dependency of some maven module instead using the default maven lib version?
For example:
My project has a dependency on qulice-maven-plugin, which depends on qulice-checkstyle, which depends on checkstyle libs. I want to to run checkstyle in my project, but using the same version and configuration which is loaded from these transitive dependencies.
If I run mvn checkstyle:checkstyle, which is the command for running checkstyle, it loads a default checkstyle version and uses its default configuration. I don't want to copy all my configurations from these dependencies, I just want to maven to be smart enough to execute checkstyle using the dependencies defined above.
Is there a way of doing this?
If I run mvn checkstyle:checkstyle it loads a default checkstyle version
If yours is loading version 6.18 then see https://stackoverflow.com/a/27359107/1016482 on how to override it to use a newer version.
run mvn checkstyle:checkstyle, which is the command for running checkstyle
If you want to fail the build when there is a checkstyle violation in your project, then this is not the command. As shown at https://maven.apache.org/plugins/maven-checkstyle-plugin/checkstyle-mojo.html , this will just generate a file report and not fail the build if there are checkstyle violations in your project.
For this purpose they recommend checkstyle:check and https://maven.apache.org/plugins/maven-checkstyle-plugin/usage.html#Checking_for_Violations_as_Part_of_the_Build shows an example on how to configure the it for your custom parameters.
I don't want to copy all my configurations from these dependencies
You should be able to use the embedded configuration just like sun and google configurations which are embedded in checkstyle. Just add it as a dependency and specify the config location, like /my/path/my_config.xml from the root of the dependency like you would loading a resource.

Extra Gradle tasks in Spring project

Fairly new to Gradle. Newer to Spring. I understand the creation of Gradle tasks, how to compose them, and how to create Groovy plugins but it seems that there's more going on than what's in the project.
I've got a Spring REST service with a build.gradle file but I notice that once everything syncs, there are a TON of tasks in my Gradle plugin that I can't seem to find anywhere in the project. Such as: cleanEclipse, installApp, startScripts and many others.
Are these added by something more global? If so, what is it that defines the creation of all of these tasks?
IntelliJ IDEA 14.0.1
Gradle 2.2.1
Windows 7
The extra tasks that you see look like Gradle tasks are injected into the project by various plugins.
For example, the eclipse plugin includes tasks such as eclipse, cleanEclipse etc.
Here is a listing of all the standard Gradle plugins that come bundled with Gradle: https://www.gradle.org/docs/current/userguide/standard_plugins.html
Each of these has a list of tasks/properties that it injects into the project. Third party plugins would also do the same and their corresponding documentation should have information regarding these.
I think it can be added if others plugins are applied in your project, for example eclipse plugin or application plugin.

Categories