Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more CC compatibility issues #835

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {
mavenCentral()
}

test {
tasks.withType(Test).configureEach {
useJUnitPlatform()

if (System.env.CI == 'true') {
Expand All @@ -39,6 +39,19 @@ test {
}

systemProperty 'java.io.tmpdir', buildDir.absolutePath

// Required to test configuration cache in tests when using withDebug()
// https://github.com/gradle/gradle/issues/22765#issuecomment-1339427241
jvmArgs(
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.util.concurrent.atomic=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang.invoke=ALL-UNNAMED",
"--add-opens",
"java.base/java.net=ALL-UNNAMED",
)
}

// jar {
Expand Down
2 changes: 1 addition & 1 deletion src/docs/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ task testJar(type: Jar) {

shadowJar {
manifest {
inheritFrom(project, project.tasks.testJar.manifest)
inheritFrom(project.tasks.testJar.manifest)
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class ShadowApplicationPlugin implements Plugin<Project> {
}

protected void configureInstallTask(Project project) {
project.tasks.named(SHADOW_INSTALL_TASK_NAME).configure { installTask ->
installTask.doFirst { Sync task ->
project.tasks.named(SHADOW_INSTALL_TASK_NAME, Sync).configure { task ->
Goooler marked this conversation as resolved.
Show resolved Hide resolved
task.doFirst {
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 '${javaApplication.applicationName}'.\n" +
Expand All @@ -104,8 +104,12 @@ class ShadowApplicationPlugin implements Plugin<Project> {
}
}
}
installTask.doLast { Sync task ->
project.ant.chmod(file: "${task.destinationDir.absolutePath}/bin/${javaApplication.applicationName}", perm: 'ugo+x')
task.doLast {
task.eachFile {
if (it.path == "bin/${javaApplication.applicationName}") {
it.mode = 0x755
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
package com.github.jengelman.gradle.plugins.shadow

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.SelfResolvingDependency
import org.gradle.api.file.CopySpec
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.util.GradleVersion

class ShadowExtension {

CopySpec applicationDistribution
Project project
private final Provider<Provider<RegularFile>> archiveFile
private final Provider<List<Dep>> allDependencies

ShadowExtension(Project project) {
this.project = project
applicationDistribution = project.copySpec {}
archiveFile = project.provider { project.tasks.withType(ShadowJar).getByName("shadowJar").archiveFile }
allDependencies = project.provider {
project.configurations.getByName("shadow").allDependencies.collect {
if ((it instanceof ProjectDependency) || !(it instanceof SelfResolvingDependency)) {
new Dep(it.group, it.name, it.version)
}
}
}
}

void component(MavenPublication publication) {
publication.artifact(archiveFile.get())

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

// Don't inline this variable, it seems Groovy closure capturing is confused by the field instead of a local variable.
final def allDeps = allDependencies
publication.pom { MavenPom pom ->
pom.withXml { xml ->
def dependenciesNode = xml.asNode().get('dependencies') ?: xml.asNode().appendNode('dependencies')
project.configurations.shadow.allDependencies.each {
if ((it instanceof ProjectDependency) || ! (it instanceof SelfResolvingDependency)) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', 'runtime')
}
allDeps.get().each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
dependencyNode.appendNode('scope', 'runtime')
}
}
}
}

private class Dep {
String group
String name
String version

Dep(String group, String name, String version) {
this.group = group
this.name = name
this.version = version
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ShadowJavaPlugin implements Plugin<Project> {
}
}
}
shadow.manifest.inheritFrom(project, project.tasks.jar.manifest)
shadow.manifest.inheritFrom(project.tasks.jar.manifest)
def libsProvider = project.provider { -> [project.tasks.jar.manifest.attributes.get('Class-Path')] }
def files = project.objects.fileCollection().from { ->
project.configurations.findByName(ShadowBasePlugin.CONFIGURATION_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ class DefaultInheritManifest implements InheritManifest {

private List<DefaultManifestMergeSpec> inheritMergeSpecs = []

private final transient Project project

private final FileResolver fileResolver

private final Manifest internalManifest

DefaultInheritManifest(FileResolver fileResolver) {
DefaultInheritManifest(Project project, FileResolver fileResolver) {
this.project = project
this.internalManifest = new DefaultManifest(fileResolver)
this.fileResolver = fileResolver
}

InheritManifest inheritFrom(Project project, Object... inheritPaths) {
inheritFrom(project, inheritPaths, null)
InheritManifest inheritFrom(Object... inheritPaths) {
inheritFrom(inheritPaths, null)
return this
}

InheritManifest inheritFrom(Project project, Object inheritPaths, Closure closure) {
InheritManifest inheritFrom(Object inheritPaths, Closure closure) {
DefaultManifestMergeSpec mergeSpec = new DefaultManifestMergeSpec()
mergeSpec.from(inheritPaths)
inheritMergeSpecs.add(mergeSpec)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.github.jengelman.gradle.plugins.shadow.tasks

import org.gradle.api.Project
import org.gradle.api.java.archives.Manifest

interface InheritManifest extends Manifest {

InheritManifest inheritFrom(Project project, Object... inheritPaths)
InheritManifest inheritFrom(Object... inheritPaths)

InheritManifest inheritFrom(Project project, Object inheritPaths, Closure closure)
InheritManifest inheritFrom(inheritPaths, Closure closure)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ShadowJar() {
setDuplicatesStrategy(DuplicatesStrategy.INCLUDE); //shadow filters out files later. This was the default behavior in Gradle < 6.x
dependencyFilter = new DefaultDependencyFilter(getProject());
dependencyFilterForMinimize = new MinimizeDependencyFilter(getProject());
setManifest(new DefaultInheritManifest(getServices().get(FileResolver.class)));
setManifest(new DefaultInheritManifest(getProject(), getServices().get(FileResolver.class)));
transformers = new ArrayList<>();
relocators = new ArrayList<>();
configurations = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class AppendingTransformer implements Transformer {
@Input
String resource

private ByteArrayOutputStream data = new ByteArrayOutputStream()
/**
* Defer initialization, see https://github.com/johnrengelman/shadow/issues/763
*/
private ByteArrayOutputStream data

boolean canTransformResource(FileTreeElement element) {
def path = element.relativePath.pathString
Expand All @@ -53,17 +56,25 @@ class AppendingTransformer implements Transformer {
}

void transform(TransformerContext context) {
if (data == null) {
data = new ByteArrayOutputStream()
}

IOUtil.copy(context.is, data)
data.write('\n'.bytes)

context.is.close()
}

boolean hasTransformedResource() {
return data.size() > 0
return data?.size() > 0
}

void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
if (data == null) {
data = new ByteArrayOutputStream()
}

ZipEntry entry = new ZipEntry(resource)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ApplicationSpec extends PluginSpecification {
settingsFile << "rootProject.name = 'myapp'"

when:
BuildResult result = run('runShadow', '--stacktrace')
BuildResult result = run('runShadow')

then: 'tests that runShadow executed and exited'
assert result.output.contains('TestApp: Hello World! (foo)')
Expand Down Expand Up @@ -105,7 +105,7 @@ class ApplicationSpec extends PluginSpecification {
runShadow {
args 'foo'
doFirst {
project.logger.lifecycle("Running application with JDK \${it.javaLauncher.get().metadata.languageVersion.asInt()}")
logger.lifecycle("Running application with JDK \${it.javaLauncher.get().metadata.languageVersion.asInt()}")
Goooler marked this conversation as resolved.
Show resolved Hide resolved
}
}
""".stripIndent()
Expand All @@ -120,7 +120,7 @@ class ApplicationSpec extends PluginSpecification {
""".stripIndent()

when:
BuildResult result = run('runShadow', '--stacktrace')
BuildResult result = run('runShadow')

then: 'tests that runShadow executed and exited'
assert result.output.contains('Running application with JDK 17')
Expand Down Expand Up @@ -183,7 +183,7 @@ class ApplicationSpec extends PluginSpecification {
settingsFile << "rootProject.name = 'myapp'"

when:
run('shadowDistZip', '--stacktrace')
run('shadowDistZip')

then: 'Check that the distribution zip was created'
File zip = getFile('build/distributions/myapp-shadow-1.0.zip')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class ConfigurationCacheSpec extends PluginSpecification {
settingsFile << "rootProject.name = 'myapp'"

when:
run('--configuration-cache', 'shadowJar')
def result = run('--configuration-cache', 'shadowJar')
run('shadowJar')
def result = run('shadowJar')

then:
result.output.contains("Reusing configuration cache.")
Expand All @@ -65,9 +65,9 @@ class ConfigurationCacheSpec extends PluginSpecification {
""".stripIndent()

when:
run('--configuration-cache', 'shadowJar')
run('shadowJar')
output.delete()
def result = run('--configuration-cache', 'shadowJar')
def result = run('shadowJar')

then:
contains(output, ['a.properties', 'b.properties'])
Expand Down Expand Up @@ -117,9 +117,9 @@ class ConfigurationCacheSpec extends PluginSpecification {
def output = getFile('server/build/libs/server-all.jar')

when:
run('--configuration-cache', 'shadowJar', '-s')
run('shadowJar', '-s')
output.delete()
def result = run('--configuration-cache', 'shadowJar', '-s')
def result = run('shadowJar', '-s')

then:
output.exists()
Expand Down Expand Up @@ -160,8 +160,8 @@ class ConfigurationCacheSpec extends PluginSpecification {
""".stripIndent()

when:
run('--configuration-cache', 'shadowJar', '-s')
def result = run('--configuration-cache', 'shadowJar', '-s')
run('shadowJar', '-s')
def result = run('shadowJar', '-s')

then:
result.output.contains(":lib:shadowJar UP-TO-DATE")
Expand Down
Loading