Skip to content

Commit

Permalink
Allow publication artifacts to be defined as files through providers
Browse files Browse the repository at this point in the history
*Updated solution based on feedback to #11329*

This used to work in Gradle 5.5 (see #11054).

The combination of #9467 and #6775 broke this.

A builtBy dependency is added if the artifact is a TaskDependencyContainer,
which all Providers are (#9467). This dependency was silently doing
nothing in case of a plain File/String. This became an error (#6775)
because a File/String is nothing that can be resolved to a task.

We now only add a builtBy dependency, if the provider value or content is
actually produced by a task.
  • Loading branch information
jjohannes committed Nov 13, 2019
1 parent da50a17 commit de22595
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
Expand Up @@ -25,12 +25,17 @@ class IvyPublishArtifactCustomizationIntegTest extends AbstractIvyPublishIntegTe
void "can publish custom artifacts"() {
given:
createBuildScripts("""
file("customFile.foo") << 'some foo'
file("customFile.bar") << 'some bar'
publications {
ivy(IvyPublication) {
artifact "customFile.txt"
artifact customDocsTask.outputFile
artifact regularFileTask.outputFile
artifact customJar
artifact provider { file("customFile.foo") }
artifact provider { "customFile.bar" }
}
}
""", """
Expand All @@ -46,19 +51,22 @@ class IvyPublishArtifactCustomizationIntegTest extends AbstractIvyPublishIntegTe

then:
module.assertPublished()
module.assertArtifactsPublished("ivy-2.4.xml", "ivyPublish-2.4.txt", "ivyPublish-2.4.html", "ivyPublish-2.4.reg", "ivyPublish-2.4.jar")
module.assertArtifactsPublished("ivy-2.4.xml", "ivyPublish-2.4.txt", "ivyPublish-2.4.foo", "ivyPublish-2.4.bar", "ivyPublish-2.4.html", "ivyPublish-2.4.reg", "ivyPublish-2.4.jar")
result.assertTasksExecuted(":customDocsTask", ":customJar", ":regularFileTask", ":generateDescriptorFileForIvyPublication", ":publishIvyPublicationToIvyRepository", ":publish")

and:
def ivy = module.parsedIvy
ivy.expectArtifact('ivyPublish', 'txt').hasType("txt").hasConf(null)
ivy.expectArtifact('ivyPublish', 'html').hasType("html").hasConf(null)
ivy.expectArtifact('ivyPublish', 'jar').hasType("jar").hasConf(null)
ivy.expectArtifact('ivyPublish', 'reg').hasType("reg").hasConf(null)
ivy.expectArtifact('ivyPublish', 'foo').hasType("foo").hasConf(null)
ivy.expectArtifact('ivyPublish', 'bar').hasType("bar").hasConf(null)

and:
resolveArtifacts(module) {
withoutModuleMetadata {
expectFiles "ivyPublish-2.4.html", "ivyPublish-2.4.jar", "ivyPublish-2.4.reg", "ivyPublish-2.4.txt"
expectFiles "ivyPublish-2.4.html", "ivyPublish-2.4.jar", "ivyPublish-2.4.reg", "ivyPublish-2.4.txt", "ivyPublish-2.4.foo", "ivyPublish-2.4.bar"
}
withModuleMetadata {
noComponentPublished()
Expand Down
Expand Up @@ -19,6 +19,7 @@
import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.tasks.TaskDependencyContainer;
import org.gradle.api.provider.Provider;
import org.gradle.api.publish.ivy.IvyArtifact;
import org.gradle.api.publish.ivy.internal.publisher.IvyPublicationIdentity;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
Expand Down Expand Up @@ -105,7 +106,15 @@ public void convert(Object notation, NotationConvertResult<? super IvyArtifact>
File file = fileResolverNotationParser.parseNotation(notation);
IvyArtifact ivyArtifact = instantiator.newInstance(FileBasedIvyArtifact.class, file, publicationIdentity);
if (notation instanceof TaskDependencyContainer) {
ivyArtifact.builtBy(notation);
TaskDependencyContainer taskDependencyContainer;
if (notation instanceof Provider) {
// wrap to disable special handling of providers by DefaultTaskDependency in this case
// (workaround for https://github.com/gradle/gradle/issues/11054)
taskDependencyContainer = context -> context.add(notation);
} else {
taskDependencyContainer = (TaskDependencyContainer) notation;
}
ivyArtifact.builtBy(taskDependencyContainer);
}
result.converted(ivyArtifact);
}
Expand Down
Expand Up @@ -23,11 +23,16 @@ class MavenPublishArtifactCustomizationIntegTest extends AbstractMavenPublishInt
def "can attach custom artifacts"() {
given:
createBuildScripts("""
file("customFile.foo") << 'some foo'
file("customFile.bar") << 'some bar'
publications {
mavenCustom(MavenPublication) {
artifact "customFile.txt"
artifact customJar
artifact regularFileTask.outputFile
artifact provider { file("customFile.foo") }
artifact provider { "customFile.bar" }
}
}
""")
Expand All @@ -37,7 +42,8 @@ class MavenPublishArtifactCustomizationIntegTest extends AbstractMavenPublishInt
then:
def module = mavenRepo.module("group", "projectText", "1.0")
module.assertPublished()
module.assertArtifactsPublished("projectText-1.0.pom", "projectText-1.0.txt", "projectText-1.0-customjar.jar", "projectText-1.0.reg")
module.assertArtifactsPublished("projectText-1.0.pom", "projectText-1.0.txt", "projectText-1.0.foo", "projectText-1.0.bar", "projectText-1.0-customjar.jar", "projectText-1.0.reg")
result.assertTasksExecuted(":customJar", ":regularFileTask", ":generatePomFileForMavenCustomPublication", ":publishMavenCustomPublicationToMavenRepository", ":publish")

and:
resolveArtifacts(module) {
Expand All @@ -49,6 +55,24 @@ class MavenPublishArtifactCustomizationIntegTest extends AbstractMavenPublishInt
expectFiles "projectText-1.0.txt"
}
}
resolveArtifacts(module) {
ext = 'foo'
withModuleMetadata {
noComponentPublished()
}
withoutModuleMetadata {
expectFiles "projectText-1.0.foo"
}
}
resolveArtifacts(module) {
ext = 'bar'
withModuleMetadata {
noComponentPublished()
}
withoutModuleMetadata {
expectFiles "projectText-1.0.bar"
}
}
resolveArtifacts(module) {
ext = 'reg'
withModuleMetadata {
Expand Down
Expand Up @@ -19,6 +19,7 @@
import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.tasks.TaskDependencyContainer;
import org.gradle.api.provider.Provider;
import org.gradle.api.publish.maven.MavenArtifact;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.internal.Factory;
Expand Down Expand Up @@ -105,7 +106,15 @@ public void convert(Object notation, NotationConvertResult<? super MavenArtifact
File file = fileResolverNotationParser.parseNotation(notation);
MavenArtifact mavenArtifact = instantiator.newInstance(FileBasedMavenArtifact.class, file);
if (notation instanceof TaskDependencyContainer) {
mavenArtifact.builtBy(notation);
TaskDependencyContainer taskDependencyContainer;
if (notation instanceof Provider) {
// wrap to disable special handling of providers by DefaultTaskDependency in this case
// (workaround for https://github.com/gradle/gradle/issues/11054)
taskDependencyContainer = context -> context.add(notation);
} else {
taskDependencyContainer = (TaskDependencyContainer) notation;
}
mavenArtifact.builtBy(taskDependencyContainer);
}
result.converted(mavenArtifact);
}
Expand Down

0 comments on commit de22595

Please sign in to comment.