# Accessing configuration files from a custom gradle plugin

**URL:** <https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294>\
**Category:** Help/Discuss\
**Created:** [June 30, 2016, 10:58am UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294 "2016-06-30T10:58:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gallarr](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@gallarr](https://discuss.gradle.org/u/gallarr)\
**Post date:** [June 30, 2016, 10:58am UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294/1 "2016-06-30T10:58:49Z")

</div>

Hello

I have a suite of microservices that are stored in individual Bitbucket projects

In order to avoid duplication in each of these projects, I’ve created a suite of custom gradle plugins so I can have the default build logic in one location and easily apply this logic to each project.

The plugins are divided into modular components:  
root-plugin to set up the default library definitions (extensions)  
publishing-plugin to set up the default publishing behavour and handle transitive dependencies through pom definitions  
coding standards plugin to set up the coding standards process  
etc, etc

In my coding standards plugin I have the following code:

apply plugin: 'java’  
apply plugin: ‘checkstyle’

checkstyle {  
toolVersion = "6.12.1"  
configFile = file(’/config/checkstyle/checkstyle.xml’)  
}

The checkstyle.xml file is contained in the src/main/resources/config/checkstyle file within the coding standards plugin project.

When I apply this plugin in another project it looks for the file within that project.

Is there any way that I can make this look for the config file within the plugin?

Or do I need to add the config file as a dependency in the pom definition when publishing the plugin?

Many Thanks

---

<div class="post-metadata">

**Author:** ![gallarr](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@gallarr](https://discuss.gradle.org/u/gallarr)\
**Post date:** [July 8, 2016, 2:41pm UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294/2 "2016-07-08T14:41:22Z")

</div>

Since there’s been no reply I thought I’d show the workaround that I implemented last week just in case it helps someone else out

I couldn’t find a way to have the project calling the plugin to access the file in the plugin and I didn’t want to have to define this configuration file in every project calling the plugin. I want the configuration to be stored in one place - the plugin

So the workaround was to add a task to my plugin to build the config file for every project calling the plugin

package com.aerlingus.plugin.coding.standards.tasks

import org.gradle.api.DefaultTask  
import org.gradle.api.tasks.TaskAction

/\*\*

- A custom task to build the checkstyle configuration file for each project.  
\*/  
class BuildCheckstyleConfigFile extends DefaultTask {

package com.aerlingus.plugin.coding.standards

import com.aerlingus.plugin.coding.standards.tasks.BuildCheckstyleConfigFile  
import com.aerlingus.plugin.coding.standards.tasks.VerifyNoCheckstyleViolations  
import org.gradle.api.Plugin  
import org.gradle.api.Project

/\*\*

- This is our coding standards plugin.

- It can be injected into other obe builds as a plugin.  
\*/  
public class CodingStandardsPlugin implements Plugin {

It’s ugly but it works

I’d prefer a cleaner solution if anyone has any suggestions

Cheers

---

<div class="post-metadata">

**Author:** ![Gaurav1](https://avatars.discourse-cdn.com/v4/letter/g/ee59a6/32.png) [@Gaurav1](https://discuss.gradle.org/u/Gaurav1)\
**Post date:** [February 21, 2018, 7:36am UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294/3 "2018-02-21T07:36:52Z")

</div>

I am also trying to achieve similar thing(having multiple micro-service) which have common gradle task and dependency used. I want to write a custom gradle plugin which will hold all common task and dependency and then I can apply that custom plugin to each micro-services.  
But don’t know how to get started.Can you help in achieving the same.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![gallarr](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@gallarr](https://discuss.gradle.org/u/gallarr)\
**Post date:** [February 21, 2018, 9:41am UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294/4 "2018-02-21T09:41:40Z")

</div>

I have several plugins in a gradle plugin project

- A root plugin which gives access to all library dfinitions and nexus credentials etc
- A versioning plugin that handles all version control logic
- A coding standards plugin that enforces findbugs, checkstyle etc in a build
- A test plugin that contains all standard logic for unit tests, library definitions etc
- A publishing plugin to publish artefacts to Nexus
- A swagger plugin that provides logic to generate code from swagger definitions (top down approach)
- A sonar plugin that provides a task to run sonarqube against the current gradle project/module
- A Java plugin that applies all of the plugins above as well as the gradle Java plugin (specifying src and targets)
- A war plugin that applies the Java plugin and the gradle war plugin as well as some custom scripts

The way this is set up, a non-Java project can still avail of any of the custom plugins without applying the Java plugin. While a Java or war plugin only needs to apply one plugin and they are up and running with all of the bells and whistles.

As a basic step I will give the details for the ei-root-plugin

Create a root-plugin gradle module with the following structure:  
src/main/groovy  
src/main/resources

In the resources folder create the following folder:  
META-INF.gradle-plugins

In this folder create the following file  
com.whatever.root-plugin.properties

This is what defines your module as a gradle plugin  
In this file specify the implementation class for the plugin

implementation-class=com.whatever.plugin.root.RootPlugin

Now create this (groovy) class in the groovy folder  
com.whatever.plugin.root.RootPlugin

package com.whatever.plugin.root

import com.whatever.plugin.root.extensions.LibrariesExtension  
import com.whatever.plugin.root.extensions.NexusExtension  
import org.gradle.api.Plugin  
import org.gradle.api.Project

/\*\*

- This is the root gradle build for all projects.

- It can be injected into other builds as a plugin.  
\*/  
public class RootPlugin implements Plugin {

You can see here that I’ve added extension folders to contain the library and nexus definitions  
Create the package in com.whatever.plugin.root.extensions  
Create the groovy class LibrariesExtension

package com.whatever.plugin.root.extensions

/\*\*

- Libraries Extension.

- Needed for access to root plugin variables.  
\*/  
public class LibrariesExtension {

You can add whatever library definitions you need in here

Next create the NexusExtension groovy class

package com.whatever.plugin.root.extensions

/\*\*

- Nexus Extension.

- Needed for access to root plugin variables.  
\*/  
public class NexusExtension {

That’s it at it’s most basic. Just publish this to Nexus or whatever you use and you can pull the root plugin down in other modules

To do this open the project that you want to use the gradle plugin and create a gradle.properties folder  
Add the following to do to specify the version of the plugin and the nexus server to find this plugin

# Nexus host

nexusServer=http://whateveryour server is

# Plugin versions

rootPluginVersion=1.0.0-SNAPSHOT

In the build.gradle you need to add a buildscript dependency so your gradle buildscript has access to your plugin. Buildscripts need to be at the top of your build.gradle file

buildscript {  
repositories {  
maven { url “$nexusServer/nexus/content/repositories/releases/” }  
maven { url “$nexusServer/nexus/content/repositories/snapshots/” }  
maven { url “$nexusServer/nexus/content/repositories/public/” }  
}

```
dependencies {
    classpath "com.whatever:root-plugin:$rootPluginVersion"
}

```

}

Next apply the plugin:

apply plugin: ‘com.whatever.root-plugin’

Now you can access anything from the root plugin:

dependencies {

```
// common libraries
compile project.libraries.spring_context
compile project.libraries.findbugs_annotations
compile project.libraries.javax_servlet
compile project.libraries.castor_dlex
compile project.libraries.mscf
compile project.libraries.jini_ext_dlex
compile project.libraries.jini_core_dlex
compile project.libraries.ojdbc
compile project.libraries.reggie_dlex
compile project.libraries.commons_collections

```

}

project is the key word here  
libraries is defined in your root plugin

```
// Add the root propject extensions
private void addExtensions(final Project project) {
    project.extensions.create("nexus", NexusExtension)
    project.extensions.create("libraries", LibrariesExtension)
}

```

To access nexus you would use  
project.nexus.nexusPassword

If your library definitions were

```
// Add the root propject extensions
private void addExtensions(final Project project) {
    project.extensions.create("nnnnn", NexusExtension)
    project.extensions.create("libs", LibrariesExtension)
}

```

To access libraries you would use  
project.libs.spring\_context

To access nexus you would use  
project.nnnnn.nexusPassword

Hope that helps

---

<div class="post-metadata">

**Author:** ![Gaurav1](https://avatars.discourse-cdn.com/v4/letter/g/ee59a6/32.png) [@Gaurav1](https://discuss.gradle.org/u/Gaurav1)\
**Post date:** [February 23, 2018, 6:31am UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294/5 "2018-02-23T06:31:36Z")

</div>

Appreciate your quick response.Will try implementing the same.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [February 23, 2018, 7:26am UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294/6 "2018-02-23T07:26:37Z")

</div>

Any files in src/main/resources of the plugin will be available via the plugin classloader at runtime. If you require it as a file you could copy it locally to the project (eg under $buildDir)

Eg:

```gradle
task copyCheckstyle {
    doLast {
        mkdir "$buildDir/checkstyleConfig"
        file("$buildDir/checkstyleConfig/checkstyle.xml").text = MyPlugin.getResource('/config/checkstyle/checkstyle.xml').text
   }
} 
task checkstyle(type: Checkstyle) {
    dependsOn copyCheckstyle
    configFile "$buildDir/checkstyleConfig/checkstyle.xml"
   ...
} 

```

---

<div class="post-metadata">

**Author:** ![gallarr](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@gallarr](https://discuss.gradle.org/u/gallarr)\
**Post date:** [February 23, 2018, 9:01am UTC](https://discuss.gradle.org/t/accessing-configuration-files-from-a-custom-gradle-plugin/18294/7 "2018-02-23T09:01:08Z")

</div>

Thanks for the info. That looks cleaner than what I have. I’ll give it a go
