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
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 12, 2019
1 parent 4adbee6 commit 9cbc952
Show file tree
Hide file tree
Showing 4 changed files with 53 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,21 @@ 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")

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 @@ -18,6 +18,7 @@

import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.provider.ProviderInternal;
import org.gradle.api.internal.tasks.TaskDependencyContainer;
import org.gradle.api.publish.ivy.IvyArtifact;
import org.gradle.api.publish.ivy.internal.publisher.IvyPublicationIdentity;
Expand Down Expand Up @@ -104,7 +105,7 @@ private FileNotationConverter(FileResolver fileResolver) {
public void convert(Object notation, NotationConvertResult<? super IvyArtifact> result) throws TypeConversionException {
File file = fileResolverNotationParser.parseNotation(notation);
IvyArtifact ivyArtifact = instantiator.newInstance(FileBasedIvyArtifact.class, file, publicationIdentity);
if (notation instanceof TaskDependencyContainer) {
if (dependsOnTask(notation)) {
ivyArtifact.builtBy(notation);
}
result.converted(ivyArtifact);
Expand All @@ -116,6 +117,14 @@ public void describe(DiagnosticsVisitor visitor) {
}
}

private boolean dependsOnTask(Object notation) {
if (notation instanceof ProviderInternal) {
ProviderInternal<?> provider = (ProviderInternal<?>) notation;
return provider.isContentProducedByTask() || provider.isValueProducedByTask();
}
return notation instanceof TaskDependencyContainer;
}

private class IvyArtifactMapNotationConverter extends MapNotationConverter<IvyArtifact> {
private final NotationParser<Object, IvyArtifact> sourceNotationParser;

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,7 @@ 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")

and:
resolveArtifacts(module) {
Expand All @@ -49,6 +54,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 @@ -18,6 +18,7 @@

import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.provider.ProviderInternal;
import org.gradle.api.internal.tasks.TaskDependencyContainer;
import org.gradle.api.publish.maven.MavenArtifact;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
Expand Down Expand Up @@ -104,7 +105,7 @@ private FileNotationConverter(FileResolver fileResolver) {
public void convert(Object notation, NotationConvertResult<? super MavenArtifact> result) throws TypeConversionException {
File file = fileResolverNotationParser.parseNotation(notation);
MavenArtifact mavenArtifact = instantiator.newInstance(FileBasedMavenArtifact.class, file);
if (notation instanceof TaskDependencyContainer) {
if (dependsOnTask(notation)) {
mavenArtifact.builtBy(notation);
}
result.converted(mavenArtifact);
Expand All @@ -116,6 +117,14 @@ public void describe(DiagnosticsVisitor visitor) {
}
}

private boolean dependsOnTask(Object notation) {
if (notation instanceof ProviderInternal) {
ProviderInternal<?> provider = (ProviderInternal<?>) notation;
return provider.isContentProducedByTask() || provider.isValueProducedByTask();
}
return notation instanceof TaskDependencyContainer;
}

private class MavenArtifactMapNotationConverter extends MapNotationConverter<MavenArtifact> {
private final NotationParser<Object, MavenArtifact> sourceNotationParser;

Expand Down

0 comments on commit 9cbc952

Please sign in to comment.