How to add custom dependency handler to gradle plugin?

I would like to give users of my gradle plugin an easy way to add all the necessary dependencies. something like gradleApi() or localGroovy() .

I found out that both - gradleApi and localGroovy - are defined in DependencyHandler and implemented in DefaultDependencyHandler . Can I provide a custom DependencyHandler implementation that extends DefaultDependencyHandler in my plugin? Or is there an even simpler way to achieve what I want?

thanks in advance.

+6
source share
2 answers

One solution is for the plugin to install an additional method in the dependencies container:

 def jarDir = ... project.dependencies.ext.pluginDeps = { project.fileTree(jarDir) // filter file tree if necessary } 

Users can then:

 dependencies { compile pluginDeps() } 

Additional properties / methods are usually intended to be used only by build scripts (since they are not supported by a model that can be detected and argued for), but in this particular case (and with Gradle 2.1) I cannot come up with a clear solution.

PS: Keep in mind that for file-based dependencies (and not the repository), there will be no conflict resolution resolution.

+9
source

See if this helps to some extent. Say you create a custom plugin / class and destroy it with an artifact in Gradle (within the file level GRADLE_HOME / init.d / common.gradle).

The user java / groovy project of the plugin project will look like

 // Your custom plugin class file will look like // Your custom plugin source code will reside at: com.mycustom.plugin.gradle under src/main/java folder tree. package com.mycustom.plugin.gradle import org.gradle.api.* import org.gradle.api.file.* import java.io.File; public class myCustomFileUtil { /** * Default constructor */ public myCustomFileUtil() { } /** * Read file returning list of lines. Double slash (//) at start of line * defines a comment. * @param fileName File to read. * @return List of lines. */ public static List readIntoList( String fileName ) { def fname = new File( fileName ) def listFinal = [] def listLines = fname.readLines() listLines.each { it -> def str = it.trim(); if( str.length() > 0 ) { if( ! str.startsWith( "//" ) ) listFinal.add( str ); } } return listFinal } } 

In build.gradle

 // The following funcationality is coming from a custom plugin that you'll write which will read a text file and create a list. // It'll also ignore any lines that start with "//" import com.mycustom.plugin.gradle.myCustomFileUtil sourceSets { //if you have any //main //test //acceptanceTest //etc } // Read dependency lists from external files List depListCompile = myCustomFileUtil.readIntoList( "$projectDir/dep-compile.txt" ) List depListTest = myCustomFileUtil.readIntoList( "$projectDir/dep-testArtifacts.txt" ) List depListWar = myCustomFileUtil.readIntoList( "$projectDir/dep-war.txt" ) List depListJibx = myCustomFileUtil.readIntoList( "$projectDir/dep-jibx.txt" ) List depListSomeOperationINeed = myCustomFileUtil.readIntoList( "$projectDir/dep-someoperationineed.txt" ) // Define dependencies dependencies { // Compilation compile depListCompile // If dependencies already exist on the local folder / in some workspace compile fileTree(srcDir: "somefolder/whichcontain/myjar", include: "*.jar") compile fileTree(srcDir: "/path/xx/yy/somefolder/whichcontain/myotherartifacts", include: "*.[zw]*") compile fileTree(srcDir: "C:/zzz/somefolder/whichcontain", include: "myotherartifacts/*.[pj]*") // Unit Tests testCompile depListTest // Acceptance tests // Everything from compile and testCompile targets acceptanceTestCompile configurations.compile acceptanceTestCompile configurations.testCompile // Output of compiling "main" files acceptanceTestCompile sourceSets.main.output // Additional dependencies from war and others acceptanceTestCompile depListTest, depListWar // All configuration files acceptanceTestRuntime files( 'conf' ) } // ... and more code here to do other operations 

In the dep-compile.txt file you can have entries like

 com.mycompany.project:oneofmycompanyartifact1:1.1.1 com.mycompany.project:oneofmycompanyartifact2: 1.0.1@zip httpunit:httpunit:1.6 jibx:jibx-bind:0.10.3.3 jibx:jibx-extras:0.10.3.3 jibx:jibx-run:0.10.3.3 cactus:cactus:1.7.2 selenium:selenium:0.9.2 

Similarly, in the dep-testArtifacts.txt file, you can have the following entries (and create similar dep-XXXX files for the phase / Gradle functionality you want: compilation, testing, war, doSomeOperation, etc.).

 cactus:cactus:1.7.2 jackson-all:jackson-all:1.9.9 jibx:jibx-bind:0.10.3.3 jibx:jibx-extras:0.10.3.3 jibx:jibx-run:0.10.3.3 //junit:junit:4.10 junit:junit:4.11 mockito-all:mockito-all:1.9.5 
0
source

Source: https://habr.com/ru/post/973562/


All Articles