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 {
@Override
public void apply(final Project project) {
if (null == project.extensions.findByName(“nexus”)) {
addExtensions(project)
addRepositories(project)
}
}
// Add the root propject extensions
private void addExtensions(final Project project) {
project.extensions.create(“nexus”, NexusExtension)
project.extensions.create(“libraries”, LibrariesExtension)
}
// Add the root project repositories
private void addRepositories(final Project project) {
project.repositories {
maven { url “$project.nexus.nexusServer/nexus/content/repositories/releases/” }
maven { url “$project.nexus.nexusServer/nexus/content/repositories/snapshots/” }
maven { url “$project.nexus.nexusServer/nexus/content/repositories/public/” }
}
}
}
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 {
// Spring
def spring_version = '4.3.3.RELEASE’
def spring_core = "org.springframework:spring-core:${spring_version}"
def spring_web = "org.springframework:spring-web:${spring_version}"
def spring_test = "org.springframework:spring-test:${spring_version}"
def spring_jdbc = "org.springframework:spring-jdbc:${spring_version}"
def spring_context = "org.springframework:spring-context:${spring_version}"
def spring_support = "org.springframework:spring-context-support:${spring_version}"
def spring_webmvc = "org.springframework:spring-webmvc:${spring_version}"
def spring_tx = "org.springframework:spring-tx:${spring_version}"
def spring_aop = "org.springframework:spring-aop:${spring_version}"
def spring_beans = "org.springframework:spring-beans:${spring_version}"
def spring_expression = "org.springframework:spring-expression:${spring_version}"
def spring_oxm = “org.springframework:spring-oxm:${spring_version}”
// Spring Security
def spring_security_version = "3.2.9.RELEASE"
def spring_security_web = "org.springframework.security:spring-security-web:${spring_security_version}"
def spring_security_config = “org.springframework.security:spring-security-config:${spring_security_version}”
You can add whatever library definitions you need in here
Next create the NexusExtension groovy class
package com.whatever.plugin.root.extensions
/**
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