Skip to content

Commit

Permalink
Migrate to task configuration avoidance APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex committed Aug 31, 2020
1 parent dd632ba commit 7a4a46f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ class PluginShadowPlugin implements Plugin<Project> {
void apply(Project project) {
project.plugins.apply(ShadowPlugin)

project.tasks.withType(ShadowJar) { ShadowJar task ->
project.tasks.withType(ShadowJar).configureEach { ShadowJar task ->
if (task.name == ShadowJavaPlugin.SHADOW_JAR_TASK_NAME) {
ConfigureShadowRelocation relocate = project.tasks.create(ConfigureShadowRelocation.taskName(project.tasks.shadowJar), ConfigureShadowRelocation)
relocate.target = (ShadowJar) project.tasks.shadowJar
project.tasks.register(ConfigureShadowRelocation.taskName(task), ConfigureShadowRelocation) { relocate ->
relocate.target = (ShadowJar) task

project.tasks.shadowJar.dependsOn relocate
task.dependsOn relocate
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.plugins.ApplicationPlugin
import org.gradle.api.plugins.ApplicationPluginConvention
import org.gradle.api.plugins.MavenPlugin
import org.gradle.api.tasks.Sync
import org.gradle.api.tasks.TaskProvider
import org.gradle.api.tasks.application.CreateStartScripts

class ShadowApplicationPlugin implements Plugin<Project> {
Expand Down Expand Up @@ -57,68 +58,72 @@ class ShadowApplicationPlugin implements Plugin<Project> {
ApplicationPluginConvention pluginConvention = (
ApplicationPluginConvention) project.convention.plugins.application

jar.inputs.property('mainClassName', { pluginConvention.mainClassName })
jar.doFirst {
manifest.attributes 'Main-Class': pluginConvention.mainClassName
jar.configure { jar ->
jar.inputs.property('mainClassName', { pluginConvention.mainClassName })
jar.doFirst {
manifest.attributes 'Main-Class': pluginConvention.mainClassName
}
}
}

protected void addRunTask(Project project) {
ApplicationPluginConvention pluginConvention = (
ApplicationPluginConvention) project.convention.plugins.application

def run = project.tasks.create(SHADOW_RUN_TASK_NAME, JavaJarExec)
Sync install = project.tasks.getByName(SHADOW_INSTALL_TASK_NAME)
run.dependsOn SHADOW_INSTALL_TASK_NAME
run.setMain('-jar')
run.description = 'Runs this project as a JVM application using the shadow jar'
run.group = ApplicationPlugin.APPLICATION_GROUP
run.conventionMapping.jvmArgs = { pluginConvention.applicationDefaultJvmArgs }
run.conventionMapping.jarFile = {
project.file("${install.destinationDir.path}/lib/${jar.archivePath.name}")
project.tasks.register(SHADOW_RUN_TASK_NAME, JavaJarExec) { run ->
def install = project.tasks.named(SHADOW_INSTALL_TASK_NAME, Sync)
run.dependsOn SHADOW_INSTALL_TASK_NAME
run.setMain('-jar')
run.description = 'Runs this project as a JVM application using the shadow jar'
run.group = ApplicationPlugin.APPLICATION_GROUP
run.conventionMapping.jvmArgs = { pluginConvention.applicationDefaultJvmArgs }
run.conventionMapping.jarFile = {
project.file("${install.get().destinationDir.path}/lib/${jar.get().archivePath.name}")
}
}
}

protected void addCreateScriptsTask(Project project) {
ApplicationPluginConvention pluginConvention =
(ApplicationPluginConvention) project.convention.plugins.application

def startScripts = project.tasks.create(SHADOW_SCRIPTS_TASK_NAME, CreateStartScripts)
startScripts.unixStartScriptGenerator.template = project.resources.text.fromString(this.class.getResource("internal/unixStartScript.txt").text)
startScripts.windowsStartScriptGenerator.template = project.resources.text.fromString(this.class.getResource("internal/windowsStartScript.txt").text)
startScripts.description = 'Creates OS specific scripts to run the project as a JVM application using the shadow jar'
startScripts.group = ApplicationPlugin.APPLICATION_GROUP
startScripts.classpath = project.files(jar)
startScripts.conventionMapping.mainClassName = { pluginConvention.mainClassName }
startScripts.conventionMapping.applicationName = { pluginConvention.applicationName }
startScripts.conventionMapping.outputDir = { new File(project.buildDir, 'scriptsShadow') }
startScripts.conventionMapping.defaultJvmOpts = { pluginConvention.applicationDefaultJvmArgs }
startScripts.inputs.files jar

project.tasks.register(SHADOW_SCRIPTS_TASK_NAME, CreateStartScripts) { startScripts ->
startScripts.unixStartScriptGenerator.template = project.resources.text.fromString(this.class.getResource("internal/unixStartScript.txt").text)
startScripts.windowsStartScriptGenerator.template = project.resources.text.fromString(this.class.getResource("internal/windowsStartScript.txt").text)
startScripts.description = 'Creates OS specific scripts to run the project as a JVM application using the shadow jar'
startScripts.group = ApplicationPlugin.APPLICATION_GROUP
startScripts.classpath = project.files(jar)
startScripts.conventionMapping.mainClassName = { pluginConvention.mainClassName }
startScripts.conventionMapping.applicationName = { pluginConvention.applicationName }
startScripts.conventionMapping.outputDir = { new File(project.buildDir, 'scriptsShadow') }
startScripts.conventionMapping.defaultJvmOpts = { pluginConvention.applicationDefaultJvmArgs }
startScripts.inputs.files jar
}
}

protected void configureInstallTask(Project project) {
ApplicationPluginConvention pluginConvention =
(ApplicationPluginConvention) project.convention.plugins.application

Sync installTask = project.tasks.getByName(SHADOW_INSTALL_TASK_NAME)
installTask.doFirst { Sync task ->
if (task.destinationDir.directory) {
if (task.destinationDir.listFiles().size() != 0 && (!new File(task.destinationDir, 'lib').directory || !new File(task.destinationDir, 'bin').directory)) {
throw new GradleException("The specified installation directory '${task.destinationDir}' is neither empty nor does it contain an installation for '${pluginConvention.applicationName}'.\n" +
"If you really want to install to this directory, delete it and run the install task again.\n" +
"Alternatively, choose a different installation directory."
)
project.tasks.named(SHADOW_INSTALL_TASK_NAME).configure { installTask ->
installTask.doFirst { Sync task ->
if (task.destinationDir.directory) {
if (task.destinationDir.listFiles().size() != 0 && (!new File(task.destinationDir, 'lib').directory || !new File(task.destinationDir, 'bin').directory)) {
throw new GradleException("The specified installation directory '${task.destinationDir}' is neither empty nor does it contain an installation for '${pluginConvention.applicationName}'.\n" +
"If you really want to install to this directory, delete it and run the install task again.\n" +
"Alternatively, choose a different installation directory."
)
}
}
}
}
installTask.doLast { Sync task ->
project.ant.chmod(file: "${task.destinationDir.absolutePath}/bin/${pluginConvention.applicationName}", perm: 'ugo+x')
installTask.doLast { Sync task ->
project.ant.chmod(file: "${task.destinationDir.absolutePath}/bin/${pluginConvention.applicationName}", perm: 'ugo+x')
}
}
}

protected CopySpec configureDistSpec(Project project, CopySpec distSpec) {
def startScripts = project.tasks.getByName(SHADOW_SCRIPTS_TASK_NAME)
def startScripts = project.tasks.named(SHADOW_SCRIPTS_TASK_NAME)

distSpec.with {
from(project.file("src/dist"))
Expand All @@ -136,7 +141,7 @@ class ShadowApplicationPlugin implements Plugin<Project> {
distSpec
}

private ShadowJar getJar() {
project.tasks.findByName(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME)
private TaskProvider<ShadowJar> getJar() {
project.tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME, ShadowJar)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class ShadowBasePlugin implements Plugin<Project> {
project.extensions.create(EXTENSION_NAME, ShadowExtension, project)
createShadowConfiguration(project)

KnowsTask knows = project.tasks.create(KnowsTask.NAME, KnowsTask)
knows.group = ShadowJavaPlugin.SHADOW_GROUP
knows.description = KnowsTask.DESC
project.tasks.register(KnowsTask.NAME, KnowsTask) { knows ->
knows.group = ShadowJavaPlugin.SHADOW_GROUP
knows.description = KnowsTask.DESC
}
}

private void createShadowConfiguration(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.api.artifacts.SelfResolvingDependency
import org.gradle.api.file.CopySpec
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.util.GradleVersion

class ShadowExtension {

Expand All @@ -19,7 +20,12 @@ class ShadowExtension {

void component(MavenPublication publication) {

publication.artifact(project.tasks.shadowJar)
if (GradleVersion.current() >= GradleVersion.version("6.6")) {
publication.artifact(project.tasks.named("shadowJar"))
} else {
publication.artifact(project.tasks.shadowJar)
}

publication.pom { MavenPom pom ->
pom.withXml { xml ->
def dependenciesNode = xml.asNode().appendNode('dependencies')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ShadowJavaPlugin implements Plugin<Project> {
it.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, LibraryElements.JAR))
it.attribute(Bundling.BUNDLING_ATTRIBUTE, project.objects.named(Bundling, Bundling.SHADOWED))
}
outgoing.artifact(project.tasks.getByName(SHADOW_JAR_TASK_NAME))
outgoing.artifact(project.tasks.named(SHADOW_JAR_TASK_NAME))
}
}

Expand All @@ -59,54 +59,54 @@ class ShadowJavaPlugin implements Plugin<Project> {
mapToOptional() // make it a Maven optional dependency
}
}

}

protected void configureShadowTask(Project project) {
JavaPluginConvention convention = project.convention.getPlugin(JavaPluginConvention)
ShadowJar shadow = project.tasks.create(SHADOW_JAR_TASK_NAME, ShadowJar)
shadow.group = SHADOW_GROUP
shadow.description = 'Create a combined JAR of project and runtime dependencies'
if (GradleVersion.current() >= GradleVersion.version("5.1")) {
shadow.archiveClassifier.set("all")
} else {
shadow.conventionMapping.with {
map('classifier') {
'all'
project.tasks.register(SHADOW_JAR_TASK_NAME, ShadowJar) { shadow ->
shadow.group = SHADOW_GROUP
shadow.description = 'Create a combined JAR of project and runtime dependencies'
if (GradleVersion.current() >= GradleVersion.version("5.1")) {
shadow.archiveClassifier.set("all")
} else {
shadow.conventionMapping.with {
map('classifier') {
'all'
}
}
}
}
shadow.manifest.inheritFrom project.tasks.jar.manifest
shadow.doFirst {
def files = project.configurations.findByName(ShadowBasePlugin.CONFIGURATION_NAME).files
if (files) {
def libs = [project.tasks.jar.manifest.attributes.get('Class-Path')]
libs.addAll files.collect { "${it.name}" }
manifest.attributes 'Class-Path': libs.findAll { it }.join(' ')
shadow.manifest.inheritFrom project.tasks.jar.manifest
shadow.doFirst {
def files = project.configurations.findByName(ShadowBasePlugin.CONFIGURATION_NAME).files
if (files) {
def libs = [project.tasks.jar.manifest.attributes.get('Class-Path')]
libs.addAll files.collect { "${it.name}" }
manifest.attributes 'Class-Path': libs.findAll { it }.join(' ')
}
}
shadow.from(convention.sourceSets.main.output)
shadow.configurations = [project.configurations.findByName('runtimeClasspath') ?
project.configurations.runtimeClasspath : project.configurations.runtime]
shadow.exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'module-info.class')
}
shadow.from(convention.sourceSets.main.output)
shadow.configurations = [project.configurations.findByName('runtimeClasspath') ?
project.configurations.runtimeClasspath : project.configurations.runtime]
shadow.exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'module-info.class')

project.artifacts.add(ShadowBasePlugin.CONFIGURATION_NAME, shadow)
project.artifacts.add(ShadowBasePlugin.CONFIGURATION_NAME, project.tasks.named(SHADOW_JAR_TASK_NAME))
configureShadowUpload()
}

private void configureShadowUpload() {
configurationActionContainer.add(new Action<Project>() {
void execute(Project project) {
project.plugins.withType(MavenPlugin) {
Upload upload = project.tasks.withType(Upload).findByName(SHADOW_UPLOAD_TASK)
if (!upload) {
return
project.tasks.withType(Upload).configureEach { upload ->
if (upload.name != SHADOW_UPLOAD_TASK) {
return
}
upload.configuration = project.configurations.shadow
MavenPom pom = upload.repositories.mavenDeployer.pom
pom.scopeMappings.mappings.remove(project.configurations.compile)
pom.scopeMappings.mappings.remove(project.configurations.runtime)
pom.scopeMappings.addMapping(MavenPlugin.RUNTIME_PRIORITY, project.configurations.shadow, Conf2ScopeMappingContainer.RUNTIME)
}
upload.configuration = project.configurations.shadow
MavenPom pom = upload.repositories.mavenDeployer.pom
pom.scopeMappings.mappings.remove(project.configurations.compile)
pom.scopeMappings.mappings.remove(project.configurations.runtime)
pom.scopeMappings.addMapping(MavenPlugin.RUNTIME_PRIORITY, project.configurations.shadow, Conf2ScopeMappingContainer.RUNTIME)
}
}
})
Expand Down

0 comments on commit 7a4a46f

Please sign in to comment.