How to use classes from a neighbouring subproject during configuration phase - gradle

I want to compile one subproject, then have those classes on the classpath while building the other subproject. (A custom tass would use classes created by the first one).
Currently I'm trying:
buildscript {
dependencies {
classpath project(':MyOtherProject')
}
}
... with the following result:
Cannot use project dependencies in a script classpath definition.

You cannot build something to be used to build the something. (something being the multi-project build here)
You either need to make the MyOtherProject a complete separate build, that you either install to some repository and then depend on it per coordinates or trigger that separate build during configuration phase or your build, then depending on its outcomes.
The other alternative is, that you put it into buildSrc project. This is a full multi-project build contained in your root project that is built and added to the classpath of the main build scripts automatically by Gradle and is meant for Plugins and Custom tasks that you do not want to use in other builds also and thus do not make them a separate build.

Related

Is there any way i can see the effective build configuration of a sub project in gradle (in intelij)?

I have a root project and few sub projects.
The root project, in it's build.gradle file, adds some configurations, tasks, plugins and dependencies to subprojects.
In the subprojects's build.gradle file i have added additional deps, plugins, task and conf.
Now i want to know the effective build configuration of the subproject. Is it possible to get this info using any gradle command. If yes, is the same possible in intellij idea
(In maven I can get the effective configuration of any project using mvn help:effective-pom no matter if it's a submodule or has a parent pom. And in intellij idea i can right-click on the pom and choose to view the effective pom. It's quite simple. )
Edit:
Like Rob mentioned in comments - The below commands can be used to find any projects (parent or child/sub-project) dependencies in various scopes/configuration.
/gradlew :<sub-project-name>:dependencies
and
./gradlew :<sub-project-name<:dependencies --configuration <name-of-configuration>
And the Maven equivallent of the above would be mvn dependency:tree.
But I am interested in knowing other info also besides dependencies. Other info like: tasks, plugins, repositories etc that is applicable for the sub-project (i.e defined in the sub-project + inherited or applied from/by parent project.
maven mvn help:effective-pom == gradle ????

How to add a runtime library on Gradle in the build command

I have a build.gradle file that I want to use to build a Spring Boot microservice for multiple projects. I have created a custom library with some classes that should not be in all of the projects but I don't want to have to edit the build.gradle file when I build the microservice depending on which project I will use it in.
How can I add a command/parameter to gradle build that can add runtime libraries to the Spring Boot app?
Something like (just an exaple):
gradle buildDocker -PaddRuntime=com.skios.lib:lib-common:0.2.35
Thanks for any directions
I am not sure I understand your issue fully and thus not convinced this is the best solution.
But since a Gradle build script accepts code, you can have conditionals in a dependencies block:
dependencies {
if (project.hasProperty('addRuntime')) {
runtimeOnly('com.skios.lib:lib-common:0.2.35')
}
}
and then on the command line: ./gradlew buildDocker -PaddRuntime

How to run maven plugin for only root of multimodule project [duplicate]

I have maven multi-modules project. At the parent level, i have some java files. And in the parent pom.xml, at the package phase i do some stuff.
Usually, when i run mvn package at parent level, the package phase of parent pom will be run and all the modules will be packaged as well.
I am looking for a way that allow me to do these (when i run mvn package):
allow me to run only paren pom.xml (the script at the package phase), not the modules. This is the 1st priority.
allow me to run paren pom.xml and some particular modules (like module 1, module 2 BUT not module 3 , module 4).
Can i use profile for those issue?
Thanks.
While I agree with the fact that you may not have optimal project structure, the answer is that Maven 2.2.1 has an option "--non-recursive" which satisfies your first requirement:
-N,--non-recursive Do not recurse into sub-projects
So something like this:
mvn --non-recursive clean compile
Why do you want to have java code on the top level? In my opinion this is not a very good idea. Have your code in the subprojects and let the top-level project be responsible for holding the general information and configuration of the entire project.
If you have some base-library code in the top-level project now, you can put it in a sub-project and set up dependencies between the projects.
Take a look at Maven parent pom vs modules pom
The nature of your question indicates that your project structure may not be optimal.

Building Maven sub-modules in a Gradle project

I have a Gradle build working for a bunch of Java and C sub-modules. I would like to add several sub-modules which are incoming from existing code base and are already setup as Maven builds. Is there a way for Gradle to pickup the Maven sub-modules as part of the parent build?
It seems, there is no native way to run some maven goal within gradle build script. By the way, it is possible to run a maven goal, just providig a custom task of Exec type, which will run a maven build as a command line process. You can read more about this task type here.
Furthermore, it is even possible to provide the maven goal artifacts as dependencies for the gradle project, after you build them from custom gradle task and specify the file-dependency with builtBy property. You can read about it in the official user guide.

Gradle build succeeds even though dependencies can't be downloaded

We are new to Gradle and dependency resolution. I am in the process of creating pom.xml files for all our internally-generated artifacts and want to set up a job in our Jenkins server to verify the dependencies are properly defined and not conflicting (i.e. LibA requires x-1.0.jar, LibB requires x-1.1.jar, and AppY requires both LibA and LibB).
As such, I've set up a dummy project in SVN that simply includes a bunch of our internal artifacts as dependencies. Following TTD, I intentionally included some errors in the declarations (i.e. group and name, but not version). Sure enough, those dependencies can't be found.
But when I run this build with gradle (i.e. gradle dependencies) it includes all the failure messages but still says the build succeeded! Not good!
How can I, using Gradle/Jenkins, set up an automated job that will verify all dependencies are found?
There is no built-in task that resolves all dependencies and fails if a dependency isn't found. (IDE tasks are graceful in case of missing dependencies.) But you can easily write your own:
task resolveDependencies {
doLast {
configurations.all { it.resolve() }
}
}
gradle dependencies by design displays Gradle project dependencies reporting (if applicable) if given dependency cannot be resolved (a red text FAILED next to an unresolved dependency). To get an error use some task that depends on resolving dependencies for given configuration(s) like gradle check.
Updated. Gradle is smart in determining if given tasks are required to be executed. Therefor in case there is no source files to compile (compilation requires dependent classes/JARs to be resolved) gradle check can notice that executing compileJava/compileTestJava tasks is not needed (tasks are skipped as up-to-date). You can force it by adding any Java source file into src/main/test (tests requires also production dependencies (from compile configuration)).
This is just a workaround, there is probably a better way to do that (and I hope someone else will present it here).

Resources