📜 ⬆️ ⬇️

Dynamic connection of external native modules and plug-ins in Gradle

Preamble


It has its own “external” library and there is its own application using this library (it is loaded via an external repository). A change is required in both the library and the application.

It would seem, collect the library and upload it to the local maven repository, and then build the application. But I want to be able to correct the code in the library immediately to try the changes in the application and at the same time keep separate storage of the library code and the application, including the IDE settings and so on.

With the help of gradle and symbolic links in the file system this can be easily arranged.

Library


First, I’ll give an example of the contents of build.gradle in the library:
')
import java.text.SimpleDateFormat apply plugin: 'java' apply plugin: 'maven-publish' sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 jar.baseName = 'library' publishing { publications { mavenJava(MavenPublication) { groupId='name.alenkov.habr.gradle-dynamic-dependency' version = new SimpleDateFormat('yyyyMMddHHmm').format(new Date()) from components.java } } } 

Here the key is the string “version = new SimpleDateFormat ('yyyyMMddHHmm'). Format (new Date ())”, which sets the version of the assembly based on the current time at the time of its publication in the repository - the rest of the time we do not need a version for the library.

Disclaimer : In our demo application, we do not support more than one library branch in the product environment and therefore there is no need to support versioning of the XYZ view.

Note: in the examples I use local Maven and do not give examples using Artifactory, since this does not affect the approach.

application


Now let's move on to setting up our build.gradle in the application.

Initial build.gradle state:

 apply plugin: 'java' sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 repositories { mavenLocal() jcenter() } dependencies { compile 'name.alenkov.habr.gradle-dynamic-dependency:library:+' } 



Now let's modify the gradle configuration so that we get the desired — dynamic connection of modules.

The first step is to link our library into the application's project in the ext subdirectory:

 cd ./app/ext ln -s ../../library/ ./ 

The second step is to add a small code in settings.gradle, which scans the "/ ext" directory for gradle projects and connect them to our project:

 final extDir = new File(rootDir, 'ext') if (extDir.exists()) { extDir.eachDir { dir -> if (new File(dir, 'build.gradle').exists()) { logger.trace('found ext module: ' + dir.name) final String prjName = ':' + dir.name logger.lifecycle('include ext project: ' + prjName) include prjName project(prjName).projectDir = dir project(prjName).name = 'ext-' + dir.name } } } 

And the third, final touch - we modify the dependencies section in build.gradle:

 dependencies { compile findProject(':ext-library') ?: module(group: 'name.alenkov.habr.gradle-dynamic-dependency', name: 'library', version: '+') } 



and an example when the library has additional graddle tasks:



Gradle plugin


In the same way, you can add external gradle-plugins.

Steps:

1. Create the buildSrc directory
2. Create buildSrc / settings.gradle

 final extDir = rootDir if (extDir.exists()) { extDir.eachDir { dir -> if (new File(dir, 'build.gradle').exists()) { logger.trace('found ext plugin: ' + dir.name) final String prjName = ':' + dir.name logger.lifecycle('include ext plugin: ' + prjName) include prjName project(prjName).projectDir = dir project(prjName).name = 'ext-plugin-' + dir.name } } } 

3. Create buildSrc / build.gradle

 dependencies { runtime subprojects.collect { owner.project(it.path) } } 

Now it’s enough to link an external plugin to buildSrc , as it will be picked up by the project:

 cd ./app/buildSrc ln -s ../../gradle-plugin/ ./ 



From the current inconvenience - in IDEA dynamic modules and plugins are connected not quite correctly - a bug and a bug .

The source code of both modules and plug-in can be viewed on github

UPD: in the same way, you can connect gradle plugins only by mounting them in buildSrc. If you wish, I can write an example ...

UPD: added plugin example

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


All Articles