diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/PluginShadowPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/PluginShadowPlugin.groovy index 0b1c37641..4ccef51d7 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/PluginShadowPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/PluginShadowPlugin.groovy @@ -11,12 +11,13 @@ class PluginShadowPlugin implements Plugin { 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 + } } } } diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.groovy index 91b9de17e..208254472 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowApplicationPlugin.groovy @@ -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 { @@ -57,9 +58,11 @@ class ShadowApplicationPlugin implements Plugin { 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 + } } } @@ -67,15 +70,16 @@ class ShadowApplicationPlugin implements Plugin { 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}") + } } } @@ -83,42 +87,43 @@ class ShadowApplicationPlugin implements Plugin { 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")) @@ -136,7 +141,7 @@ class ShadowApplicationPlugin implements Plugin { distSpec } - private ShadowJar getJar() { - project.tasks.findByName(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME) + private TaskProvider getJar() { + project.tasks.named(ShadowJavaPlugin.SHADOW_JAR_TASK_NAME, ShadowJar) } } diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy index 59f6deb87..8ebd6acaf 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowBasePlugin.groovy @@ -19,9 +19,10 @@ class ShadowBasePlugin implements Plugin { 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) { diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy index 5bbfb786d..307265794 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowExtension.groovy @@ -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 { @@ -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') diff --git a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy index 5e766a73a..25e051ebf 100644 --- a/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy +++ b/src/main/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowJavaPlugin.groovy @@ -48,7 +48,7 @@ class ShadowJavaPlugin implements Plugin { 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)) } } @@ -59,38 +59,37 @@ class ShadowJavaPlugin implements Plugin { 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() } @@ -98,15 +97,16 @@ class ShadowJavaPlugin implements Plugin { configurationActionContainer.add(new Action() { 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) } } })