org.gradle.integtests.resolve.transform.ArtifactTransformCachingIntegrationTest.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.integtests.resolve.transform
import org.gradle.api.internal.artifacts.ivyservice.CacheLayout
import org.gradle.api.internal.artifacts.transform.DefaultTransformationWorkspace
import org.gradle.cache.internal.LeastRecentlyUsedCacheCleanup
import org.gradle.integtests.fixtures.AbstractHttpDependencyResolutionTest
import org.gradle.integtests.fixtures.cache.FileAccessTimeJournalFixture
import org.gradle.test.fixtures.file.TestFile
import org.gradle.test.fixtures.server.http.BlockingHttpServer
import org.junit.Rule
import spock.lang.Unroll
import java.util.regex.Pattern
import static java.util.concurrent.TimeUnit.MILLISECONDS
import static java.util.concurrent.TimeUnit.SECONDS
import static org.gradle.test.fixtures.ConcurrentTestUtil.poll
class ArtifactTransformCachingIntegrationTest extends AbstractHttpDependencyResolutionTest implements FileAccessTimeJournalFixture {
private final static long MAX_CACHE_AGE_IN_DAYS = LeastRecentlyUsedCacheCleanup.DEFAULT_MAX_AGE_IN_DAYS_FOR_RECREATABLE_CACHE_ENTRIES
@Rule BlockingHttpServer blockingHttpServer = new BlockingHttpServer()
def setup() {
settingsFile << """
rootProject.name = 'root'
include 'lib'
include 'util'
include 'app'
"""
buildFile << resolveTask
}
def "transform is applied to each file once per build"() {
given:
buildFile << declareAttributes() << multiProjectWithJarSizeTransform() << withJarTasks() << withLibJarDependency("lib3.jar")
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files: [lib1.jar.txt, lib2.jar.txt, lib3.jar.txt]") == 2
output.count("ids: [lib1.jar.txt (project :lib), lib2.jar.txt (project :lib), lib3.jar.txt (lib3.jar)]") == 2
output.count("components: [project :lib, project :lib, lib3.jar]") == 2
output.count("Transformed") == 3
isTransformed("lib1.jar", "lib1.jar.txt")
isTransformed("lib2.jar", "lib2.jar.txt")
isTransformed("lib3.jar", "lib3.jar.txt")
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files: [lib1.jar.txt, lib2.jar.txt, lib3.jar.txt]") == 2
output.count("Transformed") == 0
}
def "task cannot write into transform directory"() {
def forbiddenPath = ".transforms/not-allowed.txt"
buildFile << """
subprojects {
task badTask {
outputs.file { project.layout.buildDirectory.file("${forbiddenPath}") }
doLast { }
}
}
"""
when:
fails "badTask", "--continue"
then:
['lib', 'app', 'util'].each {
failure.assertHasDescription("A problem was found with the configuration of task ':${it}:badTask'.")
failure.assertHasCause("The output ${file("${it}/build/${forbiddenPath}")} must not be in a reserved location.")
}
}
def "scheduled transformation is invoked before consuming task is executed"() {
given:
buildFile << declareAttributes() << multiProjectWithJarSizeTransform() << withJarTasks()
when:
succeeds ":util:resolve"
def transformationPosition1 = output.indexOf("> Transform artifact lib1.jar (project :lib) with FileSizer")
def transformationPosition2 = output.indexOf("> Transform artifact lib2.jar (project :lib) with FileSizer")
def taskPosition = output.indexOf("> Task :util:resolve")
then:
transformationPosition1 >= 0
transformationPosition2 >= 0
taskPosition >= 0
transformationPosition1 < taskPosition
transformationPosition2 < taskPosition
}
def "scheduled transformation is only invoked once per subject"() {
given:
settingsFile << """
include 'util2'
"""
buildFile << declareAttributes() << multiProjectWithJarSizeTransform() << withJarTasks()
buildFile << """
project(':util2') {
dependencies {
compile project(':lib')
}
}
"""
when:
succeeds ":util:resolve", ":util2:resolve"
then:
output.count("> Transform artifact lib1.jar (project :lib) with FileSizer") == 1
output.count("> Transform artifact lib2.jar (project :lib) with FileSizer") == 1
}
def "scheduled chained transformation is only invoked once per subject"() {
given:
settingsFile << """
include 'app1'
include 'app2'
"""
buildFile << """
def color = Attribute.of('color', String)
allprojects {
dependencies {
attributesSchema {
attribute(color)
}
}
configurations {
compile {
attributes.attribute color, 'blue'
}
}
task resolveRed(type: Resolve) {
artifacts = configurations.compile.incoming.artifactView {
attributes { it.attribute(color, 'red') }
}.artifacts
}
task resolveYellow(type: Resolve) {
artifacts = configurations.compile.incoming.artifactView {
attributes { it.attribute(color, 'yellow') }
}.artifacts
}
}
configure([project(':app1'), project(':app2')]) {
dependencies {
compile project(':lib')
registerTransform {
from.attribute(Attribute.of('color', String), "blue")
to.attribute(Attribute.of('color', String), "green")
artifactTransform(MakeBlueToGreenThings)
}
registerTransform {
from.attribute(Attribute.of('color', String), "green")
to.attribute(Attribute.of('color', String), "red")
artifactTransform(MakeGreenToRedThings)
}
registerTransform {
from.attribute(Attribute.of('color', String), "green")
to.attribute(Attribute.of('color', String), "yellow")
artifactTransform(MakeGreenToYellowThings)
}
}
}
class MakeGreenToRedThings extends ArtifactTransform {
List transform(File input) {
assert outputDirectory.directory && outputDirectory.list().length == 0
def output = new File(outputDirectory, input.name + ".red")
println "Transforming \${input.name} to \${output.name}"
println "Input exists: \${input.exists()}"
output.text = String.valueOf(input.length())
return [output]
}
}
class MakeGreenToYellowThings extends ArtifactTransform {
List transform(File input) {
assert outputDirectory.directory && outputDirectory.list().length == 0
def output = new File(outputDirectory, input.name + ".yellow")
println "Transforming \${input.name} to \${output.name}"
println "Input exists: \${input.exists()}"
output.text = String.valueOf(input.length())
return [output]
}
}
class MakeBlueToGreenThings extends ArtifactTransform {
List transform(File input) {
assert outputDirectory.directory && outputDirectory.list().length == 0
def output = new File(outputDirectory, input.name + ".green")
println "Transforming \${input.name} to \${output.name}"
println "Input exists: \${input.exists()}"
output.text = String.valueOf(input.length())
return [output]
}
}
""" << withJarTasks()
when:
run ":app1:resolveRed", ":app2:resolveYellow"
then:
output.count("> Transform artifact lib1.jar (project :lib) with MakeBlueToGreenThings") == 1
output.count("> Transform artifact lib2.jar (project :lib) with MakeBlueToGreenThings") == 1
output.count("> Transform artifact lib1.jar (project :lib) with MakeGreenToYellowThings") == 1
output.count("> Transform artifact lib2.jar (project :lib) with MakeGreenToYellowThings") == 1
output.count("> Transform artifact lib1.jar (project :lib) with MakeGreenToRedThings") == 1
output.count("> Transform artifact lib2.jar (project :lib) with MakeGreenToRedThings") == 1
}
def "executes transform immediately when required during task graph building"() {
buildFile << declareAttributes() << withJarTasks() << """
import org.gradle.api.artifacts.transform.TransformParameters
abstract class MakeGreen implements TransformAction {
@InputArtifact
abstract Provider getInputArtifact()
@Override
void transform(TransformOutputs outputs) {
outputs.file(inputArtifact.get().asFile.name + ".green").text = "very green"
}
}
project(':util') {
dependencies {
compile project(':lib')
}
}
project(':app') {
dependencies {
compile project(':util')
registerTransform(MakeGreen) {
from.attribute(artifactType, 'jar')
to.attribute(artifactType, 'green')
}
}
configurations {
green {
extendsFrom(compile)
canBeResolved = true
canBeConsumed = false
attributes {
attribute(artifactType, 'green')
}
}
}
tasks.register("resolveAtConfigurationTime").configure {
inputs.files(configurations.green)
configurations.green.each { println it }
doLast { }
}
tasks.register("declareTransformAsInput").configure {
inputs.files(configurations.green)
doLast {
configurations.green.each { println it }
}
}
tasks.register("withDependency").configure {
dependsOn("resolveAtConfigurationTime")
}
tasks.register("toBeFinalized").configure {
// We require the task via a finalizer, so the transform node is in UNKNOWN state.
finalizedBy("declareTransformAsInput")
}
}
"""
when:
run(":app:toBeFinalized", "withDependency", "--info")
then:
output.count("Transforming artifact lib1.jar (project :lib) with MakeGreen") == 2
output.count("Transforming artifact lib2.jar (project :lib) with MakeGreen") == 2
}
def "each file is transformed once per set of configuration parameters"() {
given:
buildFile << declareAttributes() << withJarTasks() << withLibJarDependency("lib3.jar") << """
class TransformWithMultipleTargets extends ArtifactTransform {
private String target
@javax.inject.Inject
TransformWithMultipleTargets(String target) {
this.target = target
}
List transform(File input) {
assert input.exists()
assert outputDirectory.directory && outputDirectory.list().length == 0
if (target.equals("size")) {
def outSize = new File(outputDirectory, input.name + ".size")
println "Transformed \$input.name to \$outSize.name into \$outputDirectory"
outSize.text = String.valueOf(input.length())
return [outSize]
} else if (target.equals("hash")) {
def outHash = new File(outputDirectory, input.name + ".hash")
println "Transformed \$input.name to \$outHash.name into \$outputDirectory"
outHash.text = 'hash'
return [outHash]
}
}
}
allprojects {
dependencies {
registerTransform {
from.attribute(artifactType, 'jar')
to.attribute(artifactType, 'size')
artifactTransform(TransformWithMultipleTargets) {
params('size')
}
}
registerTransform {
from.attribute(artifactType, 'jar')
to.attribute(artifactType, 'hash')
artifactTransform(TransformWithMultipleTargets) {
params('hash')
}
}
}
task resolveSize(type: Resolve) {
artifacts = configurations.compile.incoming.artifactView {
attributes { it.attribute(artifactType, 'size') }
}.artifacts
identifier = "1"
}
task resolveHash(type: Resolve) {
artifacts = configurations.compile.incoming.artifactView {
attributes { it.attribute(artifactType, 'hash') }
}.artifacts
identifier = "2"
}
task resolve {
dependsOn(resolveHash, resolveSize)
}
}
project(':util') {
dependencies {
compile project(':lib')
}
}
project(':app') {
dependencies {
compile project(':util')
}
}
"""
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files 1: [lib1.jar.size, lib2.jar.size, lib3.jar.size]") == 2
output.count("ids 1: [lib1.jar.size (project :lib), lib2.jar.size (project :lib), lib3.jar.size (lib3.jar)]") == 2
output.count("components 1: [project :lib, project :lib, lib3.jar]") == 2
output.count("files 2: [lib1.jar.hash, lib2.jar.hash, lib3.jar.hash]") == 2
output.count("ids 2: [lib1.jar.hash (project :lib), lib2.jar.hash (project :lib), lib3.jar.hash (lib3.jar)]") == 2
output.count("components 2: [project :lib, project :lib, lib3.jar]") == 2
output.count("Transformed") == 6
isTransformed("lib1.jar", "lib1.jar.size")
isTransformed("lib2.jar", "lib2.jar.size")
isTransformed("lib3.jar", "lib3.jar.size")
isTransformed("lib1.jar", "lib1.jar.hash")
isTransformed("lib2.jar", "lib2.jar.hash")
isTransformed("lib3.jar", "lib3.jar.hash")
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files 1: [lib1.jar.size, lib2.jar.size, lib3.jar.size]") == 2
output.count("Transformed") == 0
}
def "can use custom type that does not implement equals() for transform configuration"() {
given:
buildFile << declareAttributes() << withJarTasks() << """
class CustomType implements Serializable {
String value
}
class TransformWithMultipleTargets extends ArtifactTransform {
private CustomType target
@javax.inject.Inject
TransformWithMultipleTargets(CustomType target) {
this.target = target
}
List transform(File input) {
assert input.exists()
assert outputDirectory.directory && outputDirectory.list().length == 0
if (target.value == "size") {
def outSize = new File(outputDirectory, input.name + ".size")
println "Transformed \$input.name to \$outSize.name into \$outputDirectory"
outSize.text = String.valueOf(input.length())
return [outSize]
}
if (target.value == "hash") {
def outHash = new File(outputDirectory, input.name + ".hash")
println "Transformed \$input.name to \$outHash.name into \$outputDirectory"
outHash.text = 'hash'
return [outHash]
}
}
}
allprojects {
dependencies {
registerTransform {
from.attribute(artifactType, 'jar')
to.attribute(artifactType, 'size')
artifactTransform(TransformWithMultipleTargets) {
params(new CustomType(value: 'size'))
}
}
registerTransform {
from.attribute(artifactType, 'jar')
to.attribute(artifactType, 'hash')
artifactTransform(TransformWithMultipleTargets) {
params(new CustomType(value: 'hash'))
}
}
}
task resolveSize(type: Resolve) {
artifacts = configurations.compile.incoming.artifactView {
attributes { it.attribute(artifactType, 'size') }
}.artifacts
identifier = "1"
}
task resolveHash(type: Resolve) {
artifacts = configurations.compile.incoming.artifactView {
attributes { it.attribute(artifactType, 'hash') }
}.artifacts
identifier = "2"
}
task resolve {
dependsOn(resolveSize, resolveHash)
}
}
project(':util') {
dependencies {
compile project(':lib')
}
}
project(':app') {
dependencies {
compile project(':util')
}
}
"""
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files 1: [lib1.jar.size, lib2.jar.size]") == 2
output.count("files 2: [lib1.jar.hash, lib2.jar.hash]") == 2
output.count("Transformed") == 4
isTransformed("lib1.jar", "lib1.jar.size")
isTransformed("lib2.jar", "lib2.jar.size")
isTransformed("lib1.jar", "lib1.jar.hash")
isTransformed("lib2.jar", "lib2.jar.hash")
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files 1: [lib1.jar.size, lib2.jar.size]") == 2
output.count("Transformed") == 0
}
@Unroll
def "can use configuration parameter of type #type"() {
given:
buildFile << declareAttributes() << withJarTasks() << """
class TransformWithMultipleTargets extends ArtifactTransform {
private $type target
@javax.inject.Inject
TransformWithMultipleTargets($type target) {
this.target = target
}
List transform(File input) {
assert input.exists()
assert outputDirectory.directory && outputDirectory.list().length == 0
def outSize = new File(outputDirectory, input.name + ".value")
println "Transformed \$input.name to \$outSize.name into \$outputDirectory"
outSize.text = String.valueOf(input.length()) + String.valueOf(target)
return [outSize]
}
}
allprojects {
dependencies {
registerTransform {
from.attribute(artifactType, 'jar')
to.attribute(artifactType, 'value')
artifactTransform(TransformWithMultipleTargets) {
params($value)
}
}
}
task resolve1(type: Resolve) {
identifier = "1"
}
task resolve2(type: Resolve) {
identifier = "2"
}
configure([resolve1, resolve2]) {
artifacts = configurations.compile.incoming.artifactView {
attributes { it.attribute(artifactType, 'value') }
}.artifacts
}
task resolve {
dependsOn(resolve1, resolve2)
}
}
project(':util') {
dependencies {
compile project(':lib')
}
}
project(':app') {
dependencies {
compile project(':util')
}
}
"""
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files 1: [lib1.jar.value, lib2.jar.value]") == 2
output.count("files 2: [lib1.jar.value, lib2.jar.value]") == 2
output.count("Transformed") == 2
isTransformed("lib1.jar", "lib1.jar.value")
isTransformed("lib2.jar", "lib2.jar.value")
when:
succeeds ":util:resolve", ":app:resolve"
then:
output.count("files 1: [lib1.jar.value, lib2.jar.value]") == 2
output.count("Transformed") == 0
where:
type | value
"boolean" | "true"
"int" | "123"
"List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy