How can I run Kotlin-Script (*.kts) files from within Gradle? - gradle

How can I run Kotlin-Script (*.kts) files from within Gradle?
From the command line, I can call:
kotlinc -script foo.kts
How can I do this from gradle (build.gradle)?

You have two options here to run a gradle build file named build.gradle.kts.
Change the name of your build file in settings.gradle to build.gradle.kts:
rootProject.buildFileName = 'build.gradle.kts' and run gradle <tasknames>.
Run gradle -b build.gradle.kts <tasknames>.

Note that with version gradle 4.21 (current at the time of writing this), you do not need any "buildFileName" entry, or any other entry in settings.gradle. If there is a build.gradle.kts and no build.gradle, gradle will use build.gradle.kts by default.
However having the setting for buildFileName does allow build.gradle to be ignored. Without the setting, you have to remove (or rename) build.gradle in order to run build.gradle.kts.
If you have both a build.gradle and a build.gradle.kts you can switch between the two by commenting and un-commenting the buildFileName setting.

Related

How to add release.useAutomaticVersion=true in build.gradle file for auto versioning?

I'm running gradle-release command as follows but I need to movr release.useAutomaticVersion=true inside build.gradle.
./gradlew gitOperations release -Prelease.useAutomaticVersion=true
Adding
release.useAutomaticVersion=true
to my gradle.properties inside the project dir why I apply the release plugin works perfectly. You really added this to the gradle.properties and not the build.gradle ?

Why is task resolution different in command-line vs dependsOn?

I am using the shadowJar plugin in my gradle build. Certain subprojects have a shadowJar task.
If I run gradlew shadowJar from the command line, the shadowJar tasks of all the sub-projects are executed. If on the other hand I have another task that dependsOn shadowJar and I run that, it only runs the shadowJar task on the root project.
What is the reason for this and how can I make my dependency use the same task resolution mechanism as the command line tool?
That is perfectly fine. From command line it's gradle that handles this and resolves the tasks for all projects that are taking part in the current build - were included in included during initialization phase.
Whereas when you use dependsOn it depends on in which: build.gradle you expressed the dependency and also in which block e.g. subprojects or allprojects.

Gradle SourceSets Usage in a task

I am new to the build tool Gradle and I have defined a source set to exclude few java files. Now, how can I use that source set in my JavaCompile task to avoid those excluded files being compiled ??? Here is my build.gradle and the error.build.gradle
output

gradle - global configuraiton for buildscript

Currently, we have a buildscript{} block in all our build.Gradle files which use our custom plugin.
Is there a provision to configure buildscript block globally instead of each build.Gradle file?
For eg: I have a plugin say "MyPlugin" . This plugin has a dependency to certain jars. If this plugin is used by 100 projects, all of these 100 projects need to
specify the dependency using buildscript{}
Instead, Can I do this programmatically in settings.gradle or in init.gradle ? Like if the project is using "MyPlugin" , then add the relevant dependencies as done using build script.
Below is the solution . We can define the build script in different place and reuse across projects
https://discuss.gradle.org/t/usage-of-apply-from-in-buildscript-scope/1844

jPOS Gradle build file

By default if i run the gradle installApp command in windows cmd it generates a jar file with name jpos-1.9.8.jar. But my project requires me to generate the jar with projectName.jar for example myproject.jar and not jpos-1.9.8.jar.
I tried understanding the build.gradle file but i am not able to figure out from where project.name and project.version properties are being read. I tried changing the rootProject.name property in settings.gradle file to myproject name but that also didn't work.
How can i customize the jar name for this jPOS project?
Add the following snippet into your build.gradle:
archivesBaseName = 'myproject'

Resources