Rpj
Rpj

Reputation: 6080

How to shared gradle tasks across many projects in different git repositories

We have lots of libraries for which we have the following snippets to configure the respective plugins. We would want to avoid code duplication and rather want to pull these definitions from a base repository which can be shared across all projects. How should this be configured?

checkstyle {
    showViolations = false
    ignoreFailures = true
..
}

pmd {
..
}

license {
..
}

spotless {
..
}

artifactory {
..
}

Upvotes: 0

Views: 350

Answers (1)

RenatoIvancic
RenatoIvancic

Reputation: 2092

With an easy approach you can import build script plugins into other build scripts even if they are located in a different location / different repository. The only requirement that I think is necessary that on the machine where you will build the project at the end you have access to those repositories.

Imagine you have GitHub repository that contains main configuration. https://github.com/user/shared-gradle-config/blob/master/scriptPlugin.gradle

That contains:

checkstyle {
    showViolations = false
    ignoreFailures = true
..
}

pmd {
..
}

...

Now be sure that you use raw resources in other scripts. Every repository service will have different kind of link. Below example for GitHub:

https://raw.githubusercontent.com/user/shared-gradle-config/master/scriptPlugin.gradle

At the beginning of the Gradle build script for the actual projects you have to include application of the remote script plugin:


// Application of shared remote Gradle script plguin with common configuration
// With this line all the checkStyle, PMD configuration and whatever you have declared in the script will be applied to current project
apply from: "https://raw.githubusercontent.com/user/shared-gradle-config/master/scriptPlugin.gradle"


// Whatever custom logic below
wrapper {
  version = "7.0.0"
  distributionType = Wrapper.DistributionType.ALL
}

...

I made a prototype project miself as I tried to understand how to share build configuration this way. Maybe it can help you, https://github.com/rivancic/gradle/tree/master/script-plugin. Note its not in final version, still improving it..

Upvotes: 2

Related Questions