What is a proper way to include runtime classpath in compile classpath in Gradle? - gradle

I have a following Gradle project structure:
project-root/
├── adapters/
│ ├── adapter1/
│ │ ├── main
│ │ └── test
│ ├── adapter2/
│ │ ├── main
│ │ └── test
│ └── adapter3/
│ ├── main
│ └── test
└── app-spring-boot/
├── main
├── test
└── integrationTest
In the app-spring-boot module, the adapters are included only as runtime dependency:
// project-root/app-spring-boot/build.gradle.kts
dependencies {
runtimeOnly(project(":adapters:adapter1")
runtimeOnly(project(":adapters:adapter2")
runtimeOnly(project(":adapters:adapter3")
}
In the app-spring-boot module for integrationTest source set, I would like to be able to access all dependencies at compile time not only directly from app-spring-boot, but from all of the included :adapters projects as well.
I've used following configuration:
// project-root/app-spring-boot/build.gradle.kts
plugins {
`jvm-test-suite`
}
testing {
suites {
val test by getting(JvmTestSuite::class)
val integrationTest by registering(JvmTestSuite::class) {
useJUnitJupiter()
dependencies {
implementation(project())
}
sources {
compileClasspath += sourceSets.main.get().runtimeClasspath
}
}
}
}
compileClasspath += sourceSets.main.get().runtimeClasspath does the trick and all dependencies from included runtimeOnly projects are accessible at compile time, but I'm wondering what it is the correct and idiomatic Gradle way of doing it, especially since I saw #chalimartines comment.

I agree with the comment you found, saying that adding to the compile classpath is not the right way as you end up with duplicated dependencies.
When applying the test suites plugin, it will create a set of configurations similar to the ones from the main and test source sets, prefixed with the name of the test suite. Because your test suite is called integrationTest, the "implementation" configuration is named integrationTestImplementation.
With this, you can add the runtime dependencies to the compile classpath by making this implementation configuration of the test suite extend the regular runtimeClasspath configuration from the main source set. E.g.:
testing {
// ...
}
configurations["integrationTestImplementation"].extendsFrom(configurations["runtimeClasspath"])

Related

Make gradle point to subdirectory and treat it as a rootProject

I encountered a problem with gradle project structure. I have a task that needs to be realized and some tests are meant to be executed to check whether my project structure is correct and the tasks in gradle execute correctly. However I think I misunderstood instruction a bit and I'm wondering whether I can do something with my current folders structure or If I will have to rewrite the whole project. My current project structure looks like this:
main-repo-folder/
├── docker-related-file
├── rootProject
│ ├── sub-project-1
│ ├── build(output from tasks is created here)
│ ├── build.gradle
│ ├── sub-project-2
│ ├── gradle
│ ├── gradlew
│ ├── gradlew.bat
│ ├── settings.gradle
│ └── src
As you can see, the root project is a directory inside a repo. In order for my tests to execute I think the repo itself must be a root folder (or act as one) because the tests seem to be trying executing there. And here is my question, is it possible to add f.e settings.gradle file in main-repo-folder (at the same level as rootProject folder) to "point" gradle to build from rootProject and treat that folder as the root?(I mean f.e if I call gradle clean build task_name in main-repo-folder I want to make gradle execute it as I would be in rootProject folder)
I've tried to find some information but I'm at the path of learning gradle and I don't know if it is even possible :/ .
Rename main-repo-folder/rootProject to main-repo-folder.

generate code coverage on JenkinsPipelineUnit tests

I am building a jenkins shared library (in groovy) and testing this with JenkinsPipelineUnit and in gradle. Running ./gradlew test jacocoTestReport runs fine, but the report is almost empty (just headers); no coverage is present.
Here are the relevant parts of my build.gradle:
plugins {
id 'groovy'
id 'application'
id 'jacoco'
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.5.4'
testCompile 'junit:junit:4.12'
testCompile 'com.lesfurets:jenkins-pipeline-unit:1.1.1-custom' // minor adaptations, but that's another story
}
test {
systemProperty "pipeline.stack.write", System.getProperty("pipeline.stack.write")
}
jacocoTestReport {
group = "Reporting"
reports {
xml.enabled true
csv.enabled false
}
additionalSourceDirs = files('vars')
sourceDirectories = fileTree(dir: 'vars')
}
I think the trouble resides in the fact that my "source" files reside in the vars directory and not in src/groovy as expected in a normal groovy project. This is however a requirement for a Jenkins shared library.
I tried specifying
sourceSets {
main {
groovy {
srcDir 'vars'
}
}
}
but then gradle would start compiling this shared library while it's supposed to be loaded upon use; and this breaks everything...
My folder structure looks like this:
├── build.gradle
├── src
│   └── test
│   ├── groovy
│   │   └── TestSimplePipeline.groovy
│   └── resources
│   └── simplePipeline.jenkins
└── vars
├── MyPipeline.groovy
└── sh.groovy
I think my problem is linked to https://github.com/jenkinsci/JenkinsPipelineUnit/issues/119 , but I wouldn't know how to use the changes proposed for maven in gradle (not even sure they apply to jacoco).
The problem is that JenkinsPipelineUnit evaluates your scripts in runtime. It means jacoco agent cannot instrument the byte-code generated in runtime.
To overcome this issue you need to do two changes.
Use jacoco offline instrumentalisation
In my case I used maven, so I cannot provide you with a specific example of a gradle configuration.
Load compiled classes instead of groovy scripts in your test. Something like this:
def scriptClass = helper.getBaseClassloader().loadClass("fooScript")
def binding = new Binding()
script = InvokerHelper.createScript(scriptClass, binding)
InterceptingGCL.interceptClassMethods(script.metaClass, helper, binding)
Here fooScript is the name of the class (say you have a source file called fooScript.groovy in this case).
Now you can call methods of this class via
def result = script.invokeMethod(methodName, args)

Building Play Framework 2.4 application with Gradle 2.6 FAILED

everyone!
The main problem is that I fail to build my Play Framework 2.4.0 application with Gradle 2.6.
The following is my build.gradle file (nothing special, everything here is from the official docs on using gradle with play framework https://docs.gradle.org/current/userguide/play_plugin.html):
plugins {
id 'play'
}
repositories {
jcenter()
maven {
name "typesafe-maven-release"
url "https://repo.typesafe.com/typesafe/maven-releases"
}
ivy {
name "typesafe-ivy-release"
url "https://repo.typesafe.com/typesafe/ivy-releases"
layout "ivy"
}
mavenCentral()
}
model {
components {
play {
platform play: '2.4.0'
}
}
}
I used playBinary, runPlayBinary and the composite tasks one by one (such as compilePlayBinaryRoutes, compilePlayBinaryTwirlTemplates and compilePlayBinaryScala), however the result is essentially the same every time:
~/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI$ gradle playBinary
:compilePlayBinaryRoutes UP-TO-DATE
:compilePlayBinaryTwirlTemplates UP-TO-DATE
:compilePlayBinaryScala
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryRoutes/router/Routes.scala:56: value index is not a member of object controllers.Application
controllers.Application.index(),
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryRoutes/router/Routes.scala:73: value updateSettings is not a member of object controllers.Application
controllers.Application.updateSettings(),
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryRoutes/router/Routes.scala:107: value getResource is not a member of object controllers.Application
controllers.Application.getResource(fakeValue[String]),
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[String]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[String]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[String]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:75: value get is not a member of List[Long]
"""),format.raw/*57.25*/("""<tr class=""""),_display_(/*57.37*/abbreviations/*57.50*/.get(i)),format.raw/*57.57*/("""_"""),_display_(/*57.59*/i),format.raw/*57.60*/("""" title=""""),_display_(/*57.70*/keysToParse/*57.81*/.get(i + 1)),format.raw/*57.92*/(""""><td>"""),_display_(/*57.99*/abbreviations/*57.112*/.get(i)),format.raw/*57.119*/(""" """),format.raw/*57.120*/("""sum : </td><td>"""),_display_(/*57.136*/aggrResults/*57.147*/.get(i)),format.raw/*57.154*/("""</td></tr>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:158: value get is not a member of List[String]
document.getElementById('timeLength').value = '"""),_display_(/*140.73*/timeLengths/*140.84*/.get(1)),format.raw/*140.91*/("""'""")))}/*140.94*/else/*140.99*/{_display_(Seq[Any](format.raw/*140.100*/("""
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:182: value get is not a member of List[String]
"""),format.raw/*164.41*/("""<span title=""""),_display_(/*164.55*/keysToParse/*164.66*/.get(i + 1)),format.raw/*164.77*/("""" class=""""),_display_(/*164.87*/abbreviations/*164.100*/.get(i)),format.raw/*164.107*/("""_"""),_display_(/*164.109*/i),format.raw/*164.110*/(""""><input type="checkbox" id=""""),_display_(/*164.140*/i),format.raw/*164.141*/("""" checked onclick="change(this)">"""),_display_(/*164.175*/abbreviations/*164.188*/.get(i)),format.raw/*164.195*/("""</span>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:182: value get is not a member of List[String]
"""),format.raw/*164.41*/("""<span title=""""),_display_(/*164.55*/keysToParse/*164.66*/.get(i + 1)),format.raw/*164.77*/("""" class=""""),_display_(/*164.87*/abbreviations/*164.100*/.get(i)),format.raw/*164.107*/("""_"""),_display_(/*164.109*/i),format.raw/*164.110*/(""""><input type="checkbox" id=""""),_display_(/*164.140*/i),format.raw/*164.141*/("""" checked onclick="change(this)">"""),_display_(/*164.175*/abbreviations/*164.188*/.get(i)),format.raw/*164.195*/("""</span>
^
/home/qb-user/projects/QuickBlox-ChatStatsUIApp/ChatStatsUI/build/playBinary/src/compilePlayBinaryTwirlTemplates/views/html/index.template.scala:182: value get is not a member of List[String]
"""),format.raw/*164.41*/("""<span title=""""),_display_(/*164.55*/keysToParse/*164.66*/.get(i + 1)),format.raw/*164.77*/("""" class=""""),_display_(/*164.87*/abbreviations/*164.100*/.get(i)),format.raw/*164.107*/("""_"""),_display_(/*164.109*/i),format.raw/*164.110*/(""""><input type="checkbox" id=""""),_display_(/*164.140*/i),format.raw/*164.141*/("""" checked onclick="change(this)">"""),_display_(/*164.175*/abbreviations/*164.188*/.get(i)),format.raw/*164.195*/("""</span>
^
11 errors found
:compilePlayBinaryScala FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compilePlayBinaryScala'.
> Compilation failed
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 8.445 secs
And here's the structure of the build directory, after build failure:
build
├── playBinary
│   ├── classes
│   └── src
│   ├── compilePlayBinaryRoutes
│   │   ├── controllers
│   │   │   ├── javascript
│   │   │   │   └── JavaScriptReverseRoutes.scala
│   │   │   ├── ReverseRoutes.scala
│   │   │   └── routes.java
│   │   └── router
│   │   ├── RoutesPrefix.scala
│   │   └── Routes.scala
│   └── compilePlayBinaryTwirlTemplates
│   └── views
│   └── html
│   └── index.template.scala
└── tmp
└── compilePlayBinaryScala
My guess is that it might have something to do with the fact, that Gradle 2.6 doesn't support reverse routing for now. I tried creating a new Play application (2.4.2 this time) and built it straight away, however it also failed on the same part:
controllers.Application.index(),
^
So let's go one by one:
All the errors after the first 3 were my mistakes, but I somehow assumed, that they were caused by the first 3. All of those were basically the result of not proper treating of the data structures in scala (my bad, I'm a noob there).
The first 3 however were caused by the fact, that I had non-static methods in my main Controller. Somehow, I must have overlooked the fact, that you have to prefix your non-static methods calls in routes file with a '#' sign. So the solution is to either place the prefix in the routes file, or make methods static.
The only reference to this (static/non-static behavior), that I found is a scarce mentioning here (https://www.playframework.com/documentation/2.4.0/JavaRouting#Dependency-Injection [last line of this paragraph]).

How to set a flattened test/source folder structure?

Using gradle for a Java+Groovy+JUnit project, I get this source folder structure by default:
prjroot-src
├── main
│ ├── groovy
│ └── java
└── test
├── groovy
└── java
I wanted to have Java and Groovy sources in one folder, so I set the sourceSets like this:
sourceSets.main.java.srcDirs = []
sourceSets.main.groovy.srcDirs += ["src/main/java"]
Which resulted in the same directory structure with the difference that I could put my .groovy files in the java folder. Also, the unused Groovy folder still is there.
I'd like a much flatter, cleaner directory structure that looks like this:
prjroot
├── src
└── test
The packages, .groovy and .java files should be directly under src, and the test sources accordingly under test.
How can I achieve this using gradle?
Replace your sourceSets code with:
sourceSets.main.groovy.srcDirs = ["src"]
sourceSets.test.groovy.srcDirs = ["test"]

Gradle Project Build Order For Maven Tasks

Consider the following multi-project build script:
build.gradle
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
group = "myorg"
version = "1.0.0-SNAPSHOT"
}
project(':client') {
dependencies {
compile 'myorg:shared:1.0.0-SNAPSHOT'
}
}
With the following files:
├── build.gradle
├── client
│   └── src
│   └── main
│   └── java
│   └── myorg
│   └── client
│   └── MyOrgClient.java
├── settings.gradle
└── shared
└── src
└── main
└── java
└── myorg
└── shared
└── MyOrgObj.java
In the above files MyOrgClient.java includes myorg.shared.MyOrgObj and settings.gradle has the single line include 'client', 'shared'
Problem
The project/task build order for maven related tasks like installing locally and deploying to remote repositories does not take into account the implied project dependency. Because gradle does not know that 'myorg:shared:1.0.0-SNAPSHOT' is created by project(':shared'), the build order is :client -> :shared and causes errors like the one below:
$ gradle install
:client:compileJava
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all dependencies for configuration ':client:compile'.
> Could not find myorg:shared:1.0.0-SNAPSHOT.
Required by:
myorg:client:1.0.0-SNAPSHOT
Question:
Is there a standard way to deal with this problem? I have tried these solutions without success:
Using mustRunAfter but ran into problems with tasks not existing yet. I also don't think this would scale well with a large number of projects
Adding archives project(':shared') to the client's dependencies
Adding compile project(':shared') to the client's dependencies and then removing it from the generated pom. Unfortunately this doesn't add the dependency to the install task or artifactoryPublish Edit: This actually was the solution. A project dependency will provide the correct version/name/group in the generated pom.xml so the explicit group:name:version dependency is not needed
You have to define the dependencies between the projects more or less the same way as in Maven:
For example like this:
project(':app') {
apply plugin: 'ear'
dependencies {
compile project (':webgui')
compile project (':service')
}
}
But you need to define the settings.gradle which contains the modules like this:
include 'app'
include 'domain'
include 'service'
include 'service-client'
include 'webgui'

Resources