I have 5 subprojects in my modules directory, each having a build.gradle file:
modules :
aSubProject
bSubProject
cSubProject
dSubProject
eSubProject
and my settings.gradle includes all 5 projects, but project b is dependent on the build output jar of project e (say eSubProject.jar),
How can I avoid the alphanumeric execution order in Gradle?
What you want to do is to tell gradle how the projects are related to each other. In the dependencies of each project you can add a dependency to another subproject.
For example:
dependencies {
compile project(':eSubProject')
}
Gradle will make sure that the projects are compiled in the proper order.
Related
I have following structure:
ProjectA -> depends on ProjectB
ProjectB -> depends on ProjectC
compiling projectB everything works:
ProjectB/build.gradle:
dependencies {
compile project(':ProjectC')
}
ProjectB/settings.gradle:
include ':ProjectC'
project(':ProjectC').projectDir = new File(settingsDir, '../ProjectC')
However, compiling ProjectA it says it can not find ProjectC
ProjectA/build.gradle:
dependencies {
compile project(':ProjectB')
}
ProjectA/settings.gradle:
include ':ProjectB'
project(':ProjectB').projectDir = new File(settingsDir, '../ProjectB')
This will show following error:
Where:
Build file ProjectB\build.gradle
What went wrong:
A problem occurred evaluating project ':ProjectB'.
Project with path ':ProjectC' could not be found in project ':ProjectB'.
I Could only make it work including ProjectC in ProjectA. But this is not what I want.
I also tried to exclude on ProjectA but didnt work
ProjectA/build.gradle:
dependencies {
compile (project (':ProjectB')) {
exclude module: ':ProjectC'
}
}
But shows same error.
How can I fix this?
Multi-Project builds are not cascadable. You can have either one or no settings.gradle file in a multi-project build, but not multiple.
Besides that it is not working as expected, it can even get more confusing if you call Gradle from different directories. Gradle looks up (and to the side into directories called master) to find the nearest settings.gradle if none is specified. Then it evaluates the settings.gradle and checks whether the project in which your working directory is, is part of the multi-project build defined in that settings.gradle file. If so, it is executed in the context of the multi-project build, if not, it is executed as standalone build.
This means, if you call Gradle from inside ProjectA, you have a completely different build that probably als is configured differently, than if you call Gradle from inside ProjectB.
If you want to compose multiple multi-project builds into one build, you should instead use GradleBuild tasks or composite builds. In both cases the sub-build is completely independent and can itself be a multi-project build. Which one to use depends on the exact use-case.
With gradle you should be using only a single settings.gradle file. See Multiple settings gradle files for multiple projects building
Also just follow gradle multiproject documentation.
I am new to the Java tools and stuff, so please be gentle.
I see that someone has added the Simple Logging Façade 4 Java (SLF4J) logging capability to a project I am looking at in IntelliJ IDEA. The project is a Gradle project and when I see the Project Structure -> Module -> Dependencies, I do see a dependency for the SLF4 jars.
I also see these listed in the External Libraries node in the treeview in the Project window.
However, when I open the build.gradle file, I see no entry for slfj. How is that? How would this library have been added to the project?
slf4j is a transitive dependency of some other library.
Use ./gradlew dependencies on the root folder of your project to see the dependencies graph.
slf4j ist most likely a transitive dependency of one of the declared dependencies.
To list the dependency tree, you can use gradlew dependencies.
To list the dependency tree for a specific configuration, you can use gradlew dependencies --configuration runtime.
In your situation you can also use the other way around and use dependencyInsight task instead like gradlew dependencyInsight --configuration runtime --dependency org.slf4j:slf4j-api to see which declared dependencies depen on the given dependency in the given configuration.
I have two Gradle projects, A and B. B is a simple Java application and is consumed by A as a compile project dependency. A is a web application.
Both A and B apply the java plugin, A applies the war plugin as well.
When building A, I get the following error:
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':war'.
> Configuration with name 'default' not found.
When separately building B I get no errors. When building from root, I get no errors either. The issue shows up only when building A.
I've also tried copying the B.jar to the lib folder of A and setting a dependency:
compile files("lib/B.jar")
The build in this case works fine.
What configurations do I need to include to avoid the error?
It's possible, when one subproject doesn't see another, if settings.gradle file is not on the parent directory for both subprojects and the subprojects in it are included via includeFlat. In this case you can call any task from your parent projects and all subprojects will know about each other, but it'll be unable to build separate subproject.
Any way, you need to show your project structure and build/settings files as well, to find out the problem.
I’m basically just trying to do what’s described in Maven - How to build multiple Independent Maven projects from one project. The accepted answer for that question describes how to use a Maven POM with pom packaging and a list of modules.
I need to do the same thing, but with Gradle. What I actually have is two separate projects which I currently have to cd into and build individually, and what I want to have is one directory (project) that contains the two projects and from which I can build them both at once. They’re not interdependent—they just generally will need to be built at the same time. Can this be done?
It looks like Rene Groeschke has the answer at https://github.com/breskeby/gradle-snippets/tree/master/multiparallel.
In summary, this is the project structure:
- maindir
build.gradle
settings.gradle
- subproject1
build.gradle
- subproject2
build.gradle
The top-level (maindir) build.gradle:
subprojects {
apply plugin:'java'
repositories {
mavenCentral()
}
}
Top-level settings.gradle:
include ':subproject1',':subproject2'
The subproject1 and subproject2 directories have their own build.gradle files, which work to build them separately.
One way would be to use (as Luke Daley suggests) Prezi Pride: https://github.com/prezi/pride.
See https://discuss.gradle.org/t/how-can-i-build-independent-projects-from-one-parent-directory/10632/2.
I've got several sub-projects in my gradle project:
Project
Common
Consumer1
Consumer2
.....
ConsumerN
My first - and main goal – is to include classes from Common project into resulting jar of every ConsumerN projects. So I can develop and test shared part (DB logic, some utils) independently in Common project and next other projects will get this classes (already tested) and include them into their jars.
Second goal is to make IntelliJ Idea to understand such dependency and work with it correctly.
Would you please suggest the "most conceptual and right way" to do this in gradle.
Assume You have the following project structure:
root
build.gradle
common
m1
m2
m3
settings.gradle
First of all You need to set a multimodule project - it's done in settings.gradle (this is a normal gradle script) and its content is as follows:
include 'm1', 'm2', 'm3', 'common'
Per project settings are done in dedicated build.gradle files, but the settings You asked can be done globally - in root build.gradle. Here's its content:
subprojects {
apply plugin: 'java'
}
subprojects.findAll { it.name != 'common' }.each {
configure(it) {
dependencies {
project(':common')
}
}
}
The question is what artifacts are produced from mN modules. If these are jar files You need to use fatjar or shadow plugin. If there are web applications war plugin is what You need.
Some further reading.
IntelliJ IDEA should handle these dependencies while importing the project using gradle wrapper.