How child project can have root project as dependency?


(Oleksandr Papchenko) #1

I have a simple multiproject configuration. I have a root project and a child project called “test”. In settings.gradle i have
include “test”
The goal is to have access to code of the root project in child`s code. So i added dependency to the root project from the child like this: compile rootProject. Also i tried compile project(":"). The problem is that in child project i have to the transitive dependencies from the parent project but not tho the code of the root project. Is this a bug ? i am using gradle version 4.1 with groovy version 2.4.11 Here is the screenshot of the structure of my project. Also it should be noted that Intelij IDEA recognizes classes from root project and i can also perform build. For reference:
root build.gradle file content

group ‘test’
version ‘1.0-SNAPSHOT’

apply plugin: ‘java’

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: ‘junit’, name: ‘junit’, version: ‘4.12’
}
settings.gradle content
rootProject.name = ‘test’
include ‘:test’

child build.gradle content
apply plugin: ‘java’
dependencies {
testCompile rootProject
}


(Dimitar Dimitrov) #2

The syntax for referencing the root project is project(":"), but it is not a typical thing to do… could yo elaborate why do you need to do this?


(Oleksandr Papchenko) #3

Looks like compile project(":") and compile rootProject in dependencies do the same. In my case - root project is the application itself - child project is performance test project. In child project i would like to mesure performance of the application implemented in the root project. Also, i would like to do performance testing only ocassinaly, so in settings.gradle i will have if statement that includes child project only when env vaiable is presented.


(Daryl) #4

I would probably have an outer root project with no code, and the former ‘root’ and ‘performace’ Projects both be sub projects under the root.


(Oleksandr Papchenko) #5

Thank you. I think this is one possible solution. But this is more like a workaround solution.
The fact that the code from the root project is not accessible from the child is confusing. Should this be considered as bug ?


(Dimitar Dimitrov) #6

The code is accessible as dependency through the syntac I mentioned. Do you have an example illustrating your actual problem? I mean something I can run and see an error.