How to provide global gradle tasks for all gradle builds

Hi there,

as far as I know it’s possible to put a gradle.properties file into the .gradle directory which contains properties I like to use within all gradle builds.

Now I like to define global tasks because I don’t want to define the same task within different gradle builds again.
So if I would create a new project with it’s special gradle build I would only call the needed global tasks without defining them within my new build script (like a parent pom in Maven).

Can anybody give me a hint how to realize this?

Regards
Tom

Do you want to have these special tasks available for all projects on a single system? If so, look at init scripts in the user guide https://docs.gradle.org/current/userguide/init_scripts.html

If you want to share logic across builds on different systems, then my recommendation is to create a plugin that you can publish to an artifact repository accessible to all systems performing the builds.

Another possibility is to just put the shared logic into a file hosted on some server and then call

apply from: <some uri>

in any build.gradle files that you want to include that shared logic in.

1 Like

Hi,

thx for the suggestions.
I like the idea with the Plugin very much.
So I created a standalone custom plugin and published it within our nexus server.
Within another project build.gradle I tried to use it. But every time I call apply plugin with the name of the properties file under META-INF/gradle-plugins I get the error “Plugin with id ‘myplugin’ not found”. I have no idea why.

Here is my plugin configuration:
myplugin.zip (3.2 KB)

And here is the gradle build of another project I like to use my plugin in:
build.zip (471 Bytes)

The download of my plugin works and I find it within the gradle cache.
I get only the error that the plugin id can’t be found. Here is the debug output of the build within build.zip:
output.zip (8.8 KB)

gradle -v
------------------------------------------------------------
Gradle 3.3
------------------------------------------------------------

Build time:   2017-01-03 15:31:04 UTC
Revision:     075893a3d0798c0c1f322899b41ceca82e4e134b

Groovy:       2.4.7
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_65 (Oracle Corporation 25.65-b01)
OS:           Linux 2.6.32-573.7.1.el6.x86_64 amd64

Has anybody an idea what I am doing wrong?

Regards
Tom

I think you might have accidentally gotten into a state where your sources jar is the published artifact. I just made that mistake while trying to get your examples to work (double check the published jar in your artifact repository and see if it’s actually the sources jar).

I made the following changes to the plugin and then published it (which correctly created a publication with both the plugin and sources jars). I then was able to resolve and use the plugin.

--- build.gradle        2017-03-07 14:23:02.000000000 -0700
+++ build.gradle.fixed  2017-03-14 16:45:43.154388566 -0600
@@ -6,7 +6,7 @@
     compile localGroovy()
 }

-apply plugin: 'maven'
+apply plugin: 'maven-publish'

 // Defining project group and version
 // The project name is defined within settings.gradle
@@ -18,11 +18,13 @@
     exclude ".*"
     exclude "*.iml"
     exclude "build"
+    classifier 'sources'
 }

 publishing {
     publications {
         mavenCustom(MavenPublication) {
+            from components.java
             artifact sourceJar
         }
     }
1 Like

Oh my goodness. :slight_smile:

I would never have thought that I have to make these kind of changes to get this work.
Your hint was right and my plugin works now. I’m very glad about that. Thank you very very much.
You saved me a lot of wasting frustrating hours of my lifetime.