Skip to content

Instantly share code, notes, and snippets.

@lukebemish
Created May 24, 2024 14:44
Show Gist options
  • Save lukebemish/5b79f393c6a44e31311a8b8f18844c9c to your computer and use it in GitHub Desktop.
Save lukebemish/5b79f393c6a44e31311a8b8f18844c9c to your computer and use it in GitHub Desktop.
plugins {
id 'java'
id 'maven-publish'
}
group = 'org.example'
version = '1.0.0'
configurations {
hasSecondaryVariants {
attributes.attribute Usage.USAGE_ATTRIBUTE, objects.named(Usage, 'something-unique')
}
}
abstract class WriteTask extends DefaultTask {
@OutputFile
abstract RegularFileProperty getOutputFile()
@Input
abstract Property<String> getText()
@TaskAction
void write() {
def file = outputFile.get().asFile
file.text = getText().get()
}
}
tasks.register('write', WriteTask) {
outputFile = file('write.txt')
text = 'Hello, world!'
}
tasks.register('writeSecondary', WriteTask) {
outputFile = file('writeSecondary.txt')
text = 'Hello, world! But different'
}
artifacts {
hasSecondaryVariants(tasks.write.outputFile) {
builtBy tasks.write
type = 'text'
classifier = 'primary'
}
}
configurations.hasSecondaryVariants.outgoing.variants {
secondary {
attributes {
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, 'something-else-unique'))
}
artifact(tasks.writeSecondary.outputFile) {
builtBy tasks.writeSecondary
type = 'text'
classifier = 'secondary'
}
}
}
def component = (AdhocComponentWithVariants) components.java
component.addVariantsFromConfiguration(configurations.hasSecondaryVariants) {}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url = "$projectDir/repo"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment