Last active
February 10, 2020 21:33
-
-
Save ghale/5e9244084fdb0fcb93b2250fe55bbc7c to your computer and use it in GitHub Desktop.
Make GitProperties plugin only gather properties once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'java' | |
// Only add the git-properties plugin to the classpath, don't apply (yet) | |
id 'com.gorylenko.gradle-git-properties' version '2.2.1' apply false | |
} | |
// This plugin is only applied to the root project | |
apply plugin: GitRootProjectPropertiesPlugin | |
subprojects { | |
apply plugin: "java" | |
// This plugin is applied to everything but the root project | |
apply plugin: GitSubProjectPropertiesPlugin | |
} | |
class GitRootProjectPropertiesPlugin implements Plugin<Project> { | |
void apply(Project project) { | |
// This is the only place the git properties plugin is applied | |
project.apply(plugin: "com.gorylenko.gradle-git-properties") | |
// Create a new configuration that has the git-properties attribute associated with it | |
project.configurations { | |
git { | |
canBeConsumed = true | |
canBeResolved = false | |
visible = false | |
Named gitPropertiesType = project.objects.named(GitPropertiesType.class, GitPropertiesType.NAME) | |
attributes.attribute(GitPropertiesType.ATTRIBUTE, gitPropertiesType) | |
} | |
} | |
// Set this as the default overlaps with other resources | |
project.gitProperties.gitPropertiesDir = project.file("${project.buildDir}/git") | |
// Map a provider that produces the parent directory of the generated git properties file | |
def taskProvider = project.tasks.named(com.gorylenko.GenerateGitPropertiesTask.TASK_NAME) | |
def resourceDirProvider = taskProvider.map { task -> task.output.parentFile } | |
// Add the directory as the artifact associated with the git configuration. Because this is | |
// a provider mapped from the task, there is an implicit "builtBy" relationship. | |
project.artifacts { | |
git resourceDirProvider | |
} | |
} | |
} | |
class GitSubProjectPropertiesPlugin implements Plugin<Project> { | |
void apply(Project project) { | |
// Create a corresponding consumer configuration that has the same "git-properties" attribute. | |
project.configurations { | |
git { | |
canBeConsumed = false | |
canBeResolved = true | |
visible = false | |
Named gitPropertiesType = project.objects.named(GitPropertiesType.class, GitPropertiesType.NAME) | |
attributes.attribute(GitPropertiesType.ATTRIBUTE, gitPropertiesType) | |
} | |
} | |
// This configuration has a simple project dependency on the root project. Gradle will find the | |
// git configuration with the matching attribute, whose artifact is the directory of the generated | |
// file. | |
project.dependencies { | |
git project.rootProject | |
} | |
// Add that directory to the resource dirs of this project | |
project.sourceSets.main.resources.srcDirs project.configurations.git | |
} | |
} | |
// Custom type to use for configuration attributes | |
interface GitPropertiesType extends Named { | |
Attribute<GitPropertiesType> ATTRIBUTE = Attribute.of("com.shelter.gitProperties", GitPropertiesType.class) | |
String NAME = "git-properties" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment