Gradle extra property does not exist - defined in sub-module - groovy

I'm migrating maven build to gradle and I'm struggling with defining basic plugin configuration in the root project. And providing specific configuration properties in sub-module.
Here is an example:
root: build.gradle
configure(filterSubprojects(['component'])) {
apply plugin: "org.flywaydb.flyway"
flyway {
url = "jdbc:oracle:thin:#${db.host}:${db.port}:${db.name}"
user = db.owner.name
password = db.owner.password
}
}
specific-subproject-component: build.gradle
ext {
db = [
host : <host>,
port : <port>,
name : <name>,
user : [name: <user-name>, password: <user-password>]
]
}
I'm getting this error:
Cannot get property 'db' on extra properties extension as it does not exist
Probably very basic question, but I can't figure out how to do this.

The problem here is, that the specifics component build.gradle file was not yet evaluated when your root build.gradle file is evaluated, so db does indeed not yet exist when the configure block runs. To fix this you should be able to declare evaluationDependsOnChildren() in your root build.gradle file.

Related

what's default ndk path in gradle script?

Recently I want to have variable that contains path of current ndk(in use) path.
exactly in my gradle script my task need ndk path (that is in use), but I don't know how find it.
task initDeps(type: Exec) {
// my ndk variable: ndkPath // How define it???
doFirst {
println "NDK path: " + ndkPath
}
}
for example my ndk path that is using in this project(or android studio) is : "/home/<user_name>/Android/Sdk/ndk/21.4.7075529"
hence variable ndkPath contains "/home/<user_name>/Android/Sdk/ndk/21.4.7075529" , but it;s relative path in another platforms , cause of that I want to know what's default variable that can bring me this goal.
hint: something like that is defined can be defined local properties -> ndk.dir ,
but independent platform

Pitest is failing showing: No mutations found due to the supplied classpath or filters + Gradle

I'm trying to run a pitest report on a gradle + kotlin project, but I get the following error:
Exception in thread "main" org.pitest.help.PitHelpError: No mutations found. This probably means there is an issue with either the supplied classpath or filters.
See http://pitest.org for more details.
at org.pitest.mutationtest.tooling.MutationCoverage.checkMutationsFound(MutationCoverage.java:352)
at org.pitest.mutationtest.tooling.MutationCoverage.runReport(MutationCoverage.java:132)
at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:123)
at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:54)
at org.pitest.mutationtest.commandline.MutationCoverageReport.runReport(MutationCoverageReport.java:98)
at org.pitest.mutationtest.commandline.MutationCoverageReport.main(MutationCoverageReport.java:45)
I tried everything that I found on google but still not working for me:
This is my build.gradle config
plugins {
id 'groovy-gradle-plugin'
id 'info.solidsoft.pitest' version '1.7.4'
}
repositories {
maven { url "https://plugins.gradle.org/m2/" }
gradlePluginPortal()
}
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20'
implementation 'com.github.jengelman.gradle.plugins:shadow:6.1.0'
}
pitest {
targetClasses = ['com.project.root.to.test.with.pitest.src*'] //by default
"${project.group}.*"
pitestVersion = '1.7.4' //not needed when a default PIT version should be used
threads = 4
outputFormats = ['XML', 'HTML']
timestampedReports = false
}
I tried this targetClasses in a different ways:
targetClasses = ['com.project.root.to.test.with.pitest.src.*'] //by default
targetClasses = ['com/project/root/to/test/with/pitest/src*'] //by default
Can someone help me, please?
You look to be trying to supply pitest with a source folder
com.project.root.to.test.with.pitest.src.
Pitest works against the compiled bytecode, not the source files. It expects
a glob that matches against the package.
com.example.*
I've experienced this same issue today. You'll need to make sure all references to pitest use the same version 1.7.4. This includes
plugin: id 'info.solidsoft.pitest' version '1.7.4'
pitestVersion: pitestVersion.set('1.7.4')
dependency: testCompile
'info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.7.4'
Which out changing all references, then it will break.

Add a single file to gradle resources

I'm using the config var plugin for heroku (see https://devcenter.heroku.com/articles/config-vars).
It allows me to use a .env file which I do not push to source control to define the heroku environment.
So when I'm on heroku, I can access sensible information via System.properties.
In dev, I would like to read from this file so it would be best if it were on my classpath.
The .env file is at the root of my project so I cannot use something like this :
sourceSets {
main {
resources {
srcDirs = ['src/main/resources', '/']
}
}
}
What is the simplest way to include a single file into gradle resources ?
The earlier answers seemed more complicated than I was hoping for, so I asked on the gradle forums and got a reply from a Sterling Green who's one of the gradle core devs.
He suggested configuring processResources directly
processResources {
from(".env")
}
He also mentioned that it might be a bad idea to include the project root as an input directly.
After merging the previous answer here with another tips from other sites, I came up with the following solution:
sourceSets {
specRes {
resources {
srcDir 'extra-dir'
include 'extrafiles/or-just-one-file.*'
}
}
main.resources {
srcDir 'src/standard/resources'
srcDir specRes.resources
}
}
processResources {
rename 'some.file', 'META-INF/possible-way-to-rename.txt'
}
I still wonder whether there is some better way.
What I ended up doing was to add project root directory as a resource folder including only the file I was interested in :
sourceSets.main.resources { srcDir file('.') include '.env' }
Seems to do the trick. I wonder if it's the best solution thought

Specify ivy configuration in gradle dependency

I want to resolve dependencies from ivy repository but I don't know how to specify ivy configuration for it. I found that I should do it in this way:
myconf group: 'com.eu', module:'MyModule', version:'1.0.0', configuration: 'ivyconf'
but it doesn't work. When I run gradle dependencies command gradle returns this error:
Could not create a dependency using notation: {group=com.eu, module=MyModule, version=1.0.0, configuration=ivyconf}
My build doesn't use plugins. I want to download dependencies in simple build which should create product from downloaded dependencies.
Build looks like this:
group = 'com.eu'
version = '0.9a'
configurations {
myconf
}
repositories {
ivy {
url 'http://ivyrepo.local/ivyrep/shared'
layout "pattern", {
artifact "[organisation]/[module]/[revision]/[type]s/[artifact].[ext]"
}
}
}
dependencies {
myconf group: 'com.eu', module:'MyModule', version:'1.0.0', configuration: 'ivyconf'
}
Instead of module, it has to be name. (see "49.4. How to declare your dependencies" in the Gradle User Guide). The declared configuration (myConf) must match the configuration used in the dependencies block (installer).

How can I create a pathing jar in Gradle

When running groovyc in a Windows env, I am running into issues due to the length of the classpath, in my situation. I would like to work around this by creating a pathing jar, and then put that jar on the cp. How can I create a pathing jar w/ all of the classpath entries specified automatically in gradle and then add that jar to the cp?
Here is a tested solution:
task pathingJar(type: Jar) {
appendix = "pathing"
doFirst {
manifest {
attributes "Class-Path": configurations.compile.files.join(" ")
}
}
}
compileGroovy {
dependsOn(pathingJar)
classpath = files(pathingJar.archivePath)
}
Depending on your exact requirements, you might have to tweak this a bit. For example, if you have tests written in Groovy, you will also need a pathing Jar for the test compile class path. In this case you'll need to repeat above configuration as follows:
task testPathingJar(type: Jar) {
appendix = "testPathing"
doFirst {
manifest {
attributes "Class-Path": configurations.testCompile.files.join(" ")
}
}
}
compileTestGroovy {
dependsOn(testPathingJar)
classpath = files(testPathingJar.archivePath)
}
I finally got the "pathing jar" idea to work. I consider this to be a permanent workaround. This could be considered a solution if it is made part of gradle itself.
The original pathing jar code was provided by Peter, but it didn't work. The problem: classpath elements referenced in the pathing jar must be relative to the location of the pathing jar. So, this appears to work for me.
task pathingJar(type: Jar , dependsOn: 'cleanPathingJar') {
/**
* If the gradle_user_home env var has been set to
* C:\ on a Win7 machine, we may not have permission to write the jar to
* this directory, so we will write it to the caches subdir instead.
* This assumes a caches subdir containing the jars
* will always exist.
*/
gradleUserHome = new File(gradle.getGradleUserHomeDir(), "caches")
relativeClasspathEntries = configurations.compile.files.collect {
new File(gradleUserHome.getAbsolutePath()).toURI().
relativize(new File(it.getAbsolutePath()).toURI()).getPath()
}
appendix = "pathing"
destinationDir = gradleUserHome
doFirst {
manifest {
attributes "Class-Path": relativeClasspathEntries.join(" ")
}
}
}
compileGroovy {
dependsOn(pathingJar)
classpath = files(pathingJar.archivePath)
}
This is what helped me:
"The filename or extension is too long error" using gradle
In other words: use the com.github.ManifestClasspath plugin.
The other solutions did not work for me because the actual project main class ended up no being included in the classpath at execution time.

Resources