I have a project setup like this
+ rootProject
+ module1
+ src
build.gradle
+ module2
+ src
build.gradle
+ src
build.gradle
settings.gradle
Content of rootProject/build.gradle
evaluationDependsOn(':module1')
apply plugin: 'java'
dependencies {
compile project(':module1')
compile project(':module2')
}
task myTask (type: JavaExec) {
main = 'org.gradle.example.Test.Main'
classpath runtimeClasspath
}
Content of module1/build.gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'commons-codec:commons-codec:1.9'
}
Content of module2/build.gradle
apply plugin: 'java'
The dependencies graph
root --> module1 --> commons-codec
--> module2
gradle report this at build with command gradle build in root folder
Could not resolve all dependencies for configuration ':compile'.
> Could not find commons-codec:commons-codec:1.9. Required by:
:rootProject:unspecified > rootProject:codec:unspecified
if I add dependencies block in the rootProject's build file the it get build normally.
As you see, i already determine the dependencies in module1 build file. Why gradle keep saying that it couldnt resolve?
Do i need to put all dependencies of submodule in root build files?
First of all I see that You're missing settings.gradle file which is necessary in multimodule projects. You need to create it at the same level as rootProject/build.gradle is located.
The content should be
include 'module1', 'module2'
UPDATE
Ok, to root build.gradle added following piece of code:
allprojects {
repositories {
mavenCentral()
}
}
And removed from module1/build.gradle the repositories section. Now it works. Dependencies are fetched and displayed. Currently no idea what is the explanation.
Related
I am trying to configure repositories for all subprojects.
I have the main build.gradle:
buildscript {
repositories {
mavenLocal()
mavenCentral()
google()
jcenter()
...
}
dependencies {
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
plugins {
id 'base'
}
allprojects {
apply plugin: 'base'
repositories {
mavenLocal()
mavenCentral()
google()
jcentre()
...
}
wrapper{
gradleVersion = '6.5.1'
distributionType = Wrapper.DistributionType.ALL
}
dependencies {
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
In the subprojects build.gradle I just have:
...
dependencies {
implementation ....
}
I am getting:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Cannot resolve external dependency .... because no repositories are defined.
Required by:
project :
I want to define repositories once in the main file as these do not change in subprojects.
In the settings.gradle of the main project I have:
rootProject.name = 'main-project-name'
include 'sub-project-name'
And in the settings.gradle of the sub project I have:
rootProject.name = 'sub-project-name'
A multi-project build in Gradle may have multiple build.gradle files, but only one settings.gradle file (usually in the root project directory). Your second settings.gradle files defines a second setup that only contains a single project. You can check this by running gradle projects. Just delete the second settings.gradle file to solve your problem.
Usually you can simply define the names of your sub-projects by naming the respective directories and then calling include. The name of the rootProject may be defined inside settings.gradle, because the name of the directory is often not stored in version control systems like Git. Developers may check out the repository to different directories causing Gradle to use different names for the root project. If you want a subproject to have a different name than its containing directory, use include with the desired name and then change the project directory via project(':foo').projectDir = file('path/to/foo').
Modern Gradle versions provide a recommended way to declare dependencies in a centralized way.
TLDR: Use the dependencyResolutionManagement DSL in your settings files to configure the repositories in all the subprojects. Both Groovy and Kotlin DSL look the same 👇
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
Read more in the docs: "Centralizing repositories declaration".
I have a Spring Boot project that is built using Gradle. It has compile dependencies on other projects. All the projects are mentioned via "includeFlat" in a settings.gradle file in a master project.
My problem: the boot ("fat") jar that is generated by the build omits the project dependencies.
Here's the project structure:
master
(no source)
build.gradle - applies 'Eclipse' plugin (but not Java)
settings.gradle -- has 'includeFlat' for projectA, projectB
projectA
src/main/java/...
build.gradle
projectB
src/main/java/...
build.gradle -- see below
The build.gradle for projectB looks roughly like this:
buildscript
{
repositories
{
mavenCentral()
}
dependencies
{
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar
{
baseName = 'xxx'
version = '0.1.0'
}
repositories
{
mavenCentral()
}
jar {
enabled = true
}
dependencies
{
compile project(":projectA")
}
"gradle build" on projectB generates both a regular and a boot jar. I expect the boot jar to include the classes from ":projectA" -- but they are missing.
Other than that, both projects build and run properly, whether built individually or via the master project.
I have a project corehibernate and a project coregeneral. corehibernate is dependent on coregeneral. I need the jar file of coregeneral to be bundled along with the corehibernate jar. I tried various versions of the build.gradle thing, nothing worked.
I tried compile files("../coregeneral/build/libs/coregeneral.jar")
This version of fatJar too does not work.
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile (':coregeneral')
testCompile 'junit:junit:4.12'
}
jar {
baseName='corehibernate'
from ('bin')
}
task fatJar(type: Jar, dependsOn: jar) {
baseName = project.name + '-fat'
}
There are two basic ways how to bundle projects together. The first would be to use application plugin which creates a zip with scripts that will also execute your application and bundle all jars by default. Second way is to use distribution plugin and define the final archive yourself (zip or tar).
Here is a sample project using the application plugin:
settings.gradle
rootProject.name = 'root'
include 'partone', 'parttwo'
build.gradle
subprojects {
apply plugin: 'java'
}
partone/build.gradle - this one is empty
parttwo/build.gradle
apply plugin: 'application'
mainClassName = 'Hello'
dependencies {
compile project (':partone')
}
Give that both projects actually have some content (classes), when you run gradle :projecttwo:build it will generate a zip file with executable scripts and both jars bundled inside.
If you prefer to use distribution plugin, change the parttwo/build.gradle to:
apply plugin: 'distribution'
distributions {
main {
contents {
from jar
from (project.configurations.runtime)
}
}
}
dependencies {
compile project (':partone')
}
And again run gradle :parttwo:build. It will create a zip file that contains both jars.
I am having a problem building some interdependent modules in a gradle based project. What I want to be able to do is have a project (similar to some maven projects I have worked with), where the dependencies are on each others versions, so when deployed to a maven repository, are available, but building a module builds the dependencies.
To illustrate, I have a gradle project with a few submodules.
ProjectA
|- module-a
\- module-b (depends on module-a)
In module b, I specify the dependency using compile "{groupId:module-b:{versionName}, and in the project root build.gradle, I have
project(':module-b') {
dependencies {
compile project(':module-a')
}
}
Another project with a dependency on the other project.
ProjectB
\- module-c (depends on module-b)
When building ProjectB, there are errors about not being able to find ProjectA:module-a:unspecified. The pom generated for module-b does include that dependency.
If I remove the dependency specification in ProjectA's root build.gradle, when I try and build, module-b cannot find module-a (during the dependency resolution phase). module-a would not be deployed anywhere yet, so this makes sense to me.
I would like module-b to depend on it's sibling, but have it's pom show a maven-style version dependency, so that module-c can depend on module-b and transitively get module-a.
(There is no requirement that ProjectB be a gradle project. Either maven or gradle should work.)
Gradle configuration files
settings.gradle
include 'module-a', 'module-b'
build.gradle
project(':module-b') {
apply plugin: 'java'
dependencies {
compile project(':module-a')
}
}
module-a/build.gradle
group 'com.example.coatedmoose'
version '1.0.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
mavenLocal()
}
}
}
module-b/build.gradle
group 'com.example.coatedmoose'
version '1.0.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'maven'
dependencies {
compile 'com.example.coatedmoose:module-a:1.0.0-SNAPSHOT'
}
uploadArchives {
repositories {
mavenDeployer {
mavenLocal()
}
}
}
I have a project which is splitted into two subprojects.
/project
/sub-project-a (backend with JAVA source which is compiled into JAR file)
/sub-project-b (frontend sources which are compiled with grunt via gradle call)
build.gradle
settings.gradle (contains include 'sub-project-a', 'sub-project-b')
My Question is how can I create a War file with sub-projects and external lib dependencies? The following code snipped is my current build.gradle:
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile project(':sub-project-a')
compile project(':sub-project-b')
compile 'com.google.code.gson:gson:2.2.4'
}
task copy(type: Copy) {
from 'sub-project-a/build', 'sub-project-b/build'
into 'build'
}
build.dependsOn clean, copy
war {
archiveName 'project.war'
}
One detail is important. The java context listener (deep inside project code) work with compiled backend as jar file from WEB-INF/lib folder. This means that all class files can't be easily used from WEB-INF/classes folder.
As you can see I played with dependencies and a custom copy task. I'm not sure what is right gradle way. How should I do this?
SOLUTION
Define with war.from methode, where you get your static sources.
gradle docu
from(sourcePaths) -
Specifies source files or directories for a copy. The given paths are
evaluated as per Project.files().
My changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}
SOLUTION (for cleanly closing this question) shamefully taken from the question's originator ;-)
Define subproject dependencies with the "war.from" method, where you get your static sources.
gradle documentation excerpt: from(sourcePaths) - Specifies source files or directories
for a copy. The given paths are evaluated as per Project.files().
Ronny's changed build.gradle
apply plugin: 'war'
version '1.0.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
war {
archiveName 'project.war'
from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}