Gradle Version Replacing characters - gradle

I am using nebula release plugin, which generates -SNAPSHOTS for snapshot builds, also using the nebula ospackage plugin to buildRpm, I am trying to use the same version as of project for RPM as well, but rpm complains about - as illegal character, Is there a way to get this fix keeping the same nomenclature, i know the rpm nomenclature standards doesn't allow this.
Can I do something like in build.gradle project.version.toString().replace("_",".")

If project.version is where the nebula-release plugin stores the generated version string, then you should be able to use:
project.version.replace('-','.')
(toString() is not necessary, but should work with it included too.)

I fixed with following work around:
if (project.version.toString().contains("-")){
version = project.version.toString().replaceAll("-", ".")
} else {
version = project.version
}

Related

Gradle Idea plugin - issues with specifing test sources

I'm trying to create a custom source set and mark its contents in Intellij Idea as a Test Sources Root. I tried to use idea plugin and do it according to the gradle official website but it is not clear for me how it works.
First of all the documentation specifies the following configuration setup
idea {
module {
testSources.from(sourceSets["intTest"].java.srcDirs)
}
}
When I try to use it i receive Unresolved reference: testSources. Where is it coming from?
Then I tried to use:
idea {
module {
testSourceDirs = intTest.java.srcDirs
}
}
it works fine as long as I use only Java. After applying Kotlin plugin however, both kotlin and java + resources folder are again treated as Sources Root not Test Sources. To fix that I had to change from:
testSourceDirs = intTest.java.srcDirs
to:
testSourceDirs = intTest.kotlin.srcDirs
and now all folders are Test Source Root again. Since kotlin.srcDirs also includes java.srcDirs it looks like you have to specify all, otherwise it is ignored...
Now the real issue came when I used gradle-avro-plugin. Applying it made my folders marked as Sources Root again. I believe it is because it adds another avro directory, but just to main source set.
Does anyone know how to make it marked as Test Sources having both kotlin and avro plugin applied? Am I doing something wrong here? Beacause this beheviour seems to be buggy in the first place.
Tested with:
IntelliJ IDEA 2022.3.1 (Ultimate Edition)
Gradle 6.8.3 and 7.4.2
Plugin id("com.github.davidmc24.gradle.plugin.avro") version "1.5.0"
Plugin kotlin("jvm") version "1.7.0"

Apply plugin in precompile script with Gradle and Kotlin

I'm trying to apply precompile script gradle.kts file (gradle.kts script is put in buildSrc/main/java. everything work normally except that i can not add a plugin with version. for example my test.gradle.kts
plugins {
id("io.gitlab.arturbosch.detekt") version "1.1.1"
}
but always got the error
Invalid plugin request [id: 'io.gitlab.arturbosch.detekt', version: '1.1.1']. Plugin requests from precompiled scripts must not include a version number. Please remove the version from the offending request and make sure the module containing the requested plugin 'io.gitlab.arturbosch.detekt' is an implementation dependency of project ':buildSrc'.
And also I cannot the class to configure extension
For example, this normally configuration is not working
configure<DetektExtension>
How can we specify plugin version in precompile script? and how can we configure the plugin?
Without being able to see your build structure and file tree I can only answer based on how I'm understanding your question. Based on the error message you are receiving, you should be declaring the plugin dependency in the project level build.gradle.kts:
plugins {
id("io.gitlab.arturbosch.detekt") version "1.1.1"
}
Then in your buildSrc/build.gradle implement it like so:
plugins {
`io.gitlab.arturbosch.detekt`
}
Gradle has samples on GitHub, from which I drew this example, here:
https://github.com/gradle/kotlin-dsl-samples/tree/master/samples/multi-kotlin-project-with-buildSrc

Gradle - auto update dependency version

I have Jenkins CI pipeline which releases applications. This process works in a way that person triggers release job for application. This job checks all project dependencies via gradle dependencies command. For all dependencies that are snapshot release jobs are triggered automatically.
Release job simply upgrade lib/application version and deploys it in artifactory.
How can I automaticaly upgrade SNAPSHOT dependency version to release version in gradle?
My build.gradle file looks like this:
Properties versions = new Properties()
versions.load(new FileInputStream(rootProject.projectDir.path + "/version.properties"))
dependencies {
compile("projectA:${versions.projectAVersion}")
compile("projectB:${versions.projectBVersion}")
}
and version.properties file
projectAVersion=1.1.0-SNAPSHOT
projectBVersion=1.1.0-SNAPSHOT
In fact I am looking something similar to maven versions plugin.
Is it possible to automatically upgrade version numer inside build.gradle? How?
And harder version - is it to possible to upgrade version number when version is in external version.properties file?
EDIT
In fact I just need Maven versions-plugin (versions:use-releases and versions:use-next-releases) functionality in Gradle.
I am not that clear with your question. What I understood is that, you need to dynamically update the version value after each build.
What you can do is, get the properties value. Remove -SNAPSHOT. Update 1.1.0 with increment after each build. Like 1.1.1, 1.1.2 etc.
This can be done by
task testing {
Properties props = new Properties()
//getting and loading the property file
//Give proper path to file
File propsFile = new File('version.properties')
props.load(propsFile.newDataInputStream())
//Now strip of -SNAPSHOT and get the last digit and increment it by 1
Integer rand = props.getProperty('lastDigit').toInteger()+1
String variable=rand.toString()
//Append -SNAPSHOT with 'variable and set the property
props.setProperty('version',variable)
props.store(propsFile.newWriter(), null)
}
This will work, if my understading of your problem is correct.

How to check java version when running gradle?

One of my project requires Java 1.8, but sometimes we didn't notice we are using older java so that we will get some strange errors.
I want to add the checking in build.gradle, so that when we run any task, it will firstly check the version, and prints error and quit immediately.
I tried to add the checking directly in build.gradle on the first line, but it still do some others tasks e.g. (clean, compileJava) before the checking happens, when I run:
$ ./gradlew
How to do it correctly?
If you put the check very early in your build lifecycle (plain check in the beginning of your build.gradle file or in the apply method of a plugin) you shouldn't see any tasks executed.
you can use JavaVersion enum for that which is part of the gradle api:
if(JavaVersion.current() != JavaVersion.VERSION_1_8){
throw new GradleException("This build must be run with java 8")
}
The accepted answer is nice however it could be improved a little bit by making it more generic. Indeed instead of comparing the current version with an explicit version, we could rely on the value of targetCompatibility instead (assuming it has been set properly) as next:
if (JavaVersion.current() != project.targetCompatibility) {
throw new GradleException("The java version used ${JavaVersion.current()} is not the expected version ${project.targetCompatibility}.")
}

get gradle to ignore version number in jar name

I have a project that has to build about 10 different dummy jars for unit testing. I have a gradle project setup like this
project(':CodeTools_dummydriver-true') {
ext.dummyDriver = true
archivesBaseName = "dummydriver-true"
}
But the problem is the jarfile is still named dummydriver-true-0.0.1.jar. Is there any way I can tell gradle to ignore the standard pattern and please name the output file what I want it to be named? Meaning, without the version number?
The Java plugin defines the jar task to follow the following template for archiveName:
${baseName}-${appendix}-${version}-${classifier}.${extension}
I don't think there's a way to apply a new "naming template", but what you can do is to explicitly set your jar task's archive name, like this. (Also, isn't it a good idea to use the dummyDriver property directly, instead of hardcoding "true" into the archive name?)
archivesBaseName = "dummydriver"
jar.archiveName = "${jar.baseName}-${dummyDriver}.${jar.extension}"
Alternately, set the version property to null or an empty string, like suggested in Circadian's answer, but if you ever want to use the version property for anything, you don't want to destroy it.
For newer gradle you should use:
build.gradle.kts:
tasks.jar {
archiveFileName.set("${project.name}-new-name.jar")
}
build.gradle:
jar.archiveFileName = "new-name.jar"
Because archiveName is deprecated. (You have to specify .jar as well).
Adding a version property and setting its value to an empty string worked for me.
Here is an example:
project(':CodeTools_dummydriver-true') {
ext.dummyDriver = true
archivesBaseName = "dummydriver-true"
version= ""
}
Hope that helps.

Resources