Skip to content
Advertisement

Is it possible to define the gradle publish task to public?

I have a Gradle 7.0 publish task defined in my Java Spring boot project sub module build.gradle file like this:

JavaScript

It works fine in each of my sub module. The only problem is that this code snippet must be copied to each of my sub module. Now the sub module increased more than 50+. I have to copy and paste it in each location.

Is it possible to define the publish task as a public task and I only need to pass some parameters like the groupId, artifactId, version? What should I do to make it work like this so I do not have to copy and paste the duplicate code snippet?

My project structure is like this:

JavaScript

BTW, this is the full sub module build.gradle:

JavaScript

I am glad to supply any extra information to solve this problem.

Advertisement

Answer

Solution 1: buildSrc

buildSrc is a special folder implicitly treated as an included build by Gradle. You can put some common build logic here.

Structure

JavaScript

./buildSrc/build.gradle

JavaScript

./buildSrc/src/main/com.example.my-publishing.gradle

JavaScript

./module1/build.gradle

JavaScript

Solution 2: Explicit buildSrc

This solution is nearly identical to the first one. I prefer this because I believe explicit is better than implicit.

Structure

JavaScript

./settings.gradle

JavaScript

Solution 3: Standalone Plugin

If your publishing logic becomes much complicated someday, you probably need this. I choose implementing in Kotlin for static typing. You can write it in Groovy too.

Structure

JavaScript

./settings.gradle

JavaScript

./plugins/build.gradle.kts

JavaScript

./plugins/src/main/kotlin/com/example/MyPublishingPlugin.kt

JavaScript

./module1/build.gradle

JavaScript

You can generate a full Gradle plugin project via the command gradle init. If you need pass extra arguments to a plugin, you can attach a custom extension to it.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement