Share gradle tasks between different repositories (without wrtiting a plugin)?

Is there an easy way to share a set of different gradle tasks between different projects in different git repositories?

Right now I have 3 different projects (that I don’t want to convert into a multi-module-project) that uses the same set of custom tasks, e.g. each project have:

build.gradle

...
apply from: 'tasks/a.gradle'
apply from: 'tasks/b.gradle'
apply from: 'tasks/c.gradle'
...

Each time there is an update to one of the above files I copy that around to the other projects/repos.

Is there an easy way to avoid this without writing a custom gradle plugin or converting to a multi-module project?

A few thing I have considered:

  1. Move the shared tasks to a new repository and do some git sub module magic to pull in the shared tasks each time a git pull is run on each of the separate projects.

  2. Similar to above but instead upload/download them from an internal artifact management server.

  3. java - How to shared gradle tasks across many projects in different git repositories - Stack Overflow and gradle/script-plugin at master · rivancic/gradle · GitHub . But that does not really seem to be active/working…

Am I missing some other simple approach?

Your can apply from an http location, it’s best to include a version in the URL

apply from: 'http://path/to/tasks/1.2.3/a.gradle'
apply from: 'http://path/to/tasks/1.2.3/b.gradle'
apply from: 'http://path/to/tasks/1.2.3/c.gradle'

Personally I’d create a plugin(s)

We have an internal on-premise gitlab instance where I have pushed a task that I can access from my browser at: > https://internal/git/project/shared-repo/-/raw/master/tasks/mytasks.gradle

The task is:

task myTask() {
  doLast {
    println "Running myTask"
  }
}

In another repo that I have cloned locally (from the same gitlab host) I have added this to build.gradle:

apply from: 'https://internal/git/project/shared-repo/-/raw/master/tasks/mytasks.gradle'

But when run my build with:

./gradlew myTask

I get:

* Where:
Script 'https://internal/git/project/shared-repo/-/raw/master/tasks/mytasks.gradle' line: 1

* What went wrong:
Could not compile script 'https://internal/git/project/shared-repo/-/raw/master/tasks/mytasks.gradle'.
> startup failed:
  script 'https://internal/git/project/shared-repo/-/raw/master/tasks/mytasks.gradle': 1: Unexpected input: '!' @ line 1, column 2.
     <!DOCTYPE html>
      ^
  
  1 error

So do I need to put the shared tasks somewhere else (could be great if I could just point directly to files in a git repo)?

I’m guessing curl/wget that URL you’ll see that it’s serving html rather than the raw gradle file

Yeah this will probably not work with files hosted in git repos in gitlab:

$ curl https://internal/git/project/shared-repo/-/raw/master/tasks/mytasks.gradle
<html><body>You are being <a href="https://internal/git/users/sign_in">redirected</a>.</body></html>

I will probably have to setup a separate web server that can host those files “raw” which is not the way to go.

Can you make the repo public?

No unfortunately that’s not an option.