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

Use the file resolver in LazyPublishArtifact #18715

Merged
merged 15 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NameValidatorTest extends Specification {
@Shared
def domainObjectContainersWithValidation = [
["artifact types", new DefaultArtifactTypeContainer(TestUtil.instantiatorFactory().decorateLenient(), AttributeTestUtil.attributesFactory(), CollectionCallbackActionDecorator.NOOP)],
["configurations", new DefaultConfigurationContainer(null, TestUtil.instantiatorFactory().decorateLenient(), domainObjectContext(), Mock(ListenerManager), null, null, Mock(FileCollectionFactory), null, null, null, null, null, AttributeTestUtil.attributesFactory(), null, null, null, null, null, Stub(DocumentationRegistry), CollectionCallbackActionDecorator.NOOP, null, null, TestUtil.domainObjectCollectionFactory(), null, TestUtil.objectFactory())],
["configurations", new DefaultConfigurationContainer(null, TestUtil.instantiatorFactory().decorateLenient(), domainObjectContext(), Mock(ListenerManager), null, null, Mock(FileCollectionFactory), null, null, null, null, null, AttributeTestUtil.attributesFactory(), null, null, null, null, null, Stub(DocumentationRegistry), CollectionCallbackActionDecorator.NOOP, null, null, TestUtil.domainObjectCollectionFactory(), null, TestUtil.objectFactory(), TestFiles.resolver())],
["flavors", new DefaultFlavorContainer(TestUtil.instantiatorFactory().decorateLenient(), CollectionCallbackActionDecorator.NOOP)],
["source sets", new DefaultSourceSetContainer(TestFiles.resolver(), null, TestUtil.instantiatorFactory().decorateLenient(), TestUtil.objectFactory(), CollectionCallbackActionDecorator.NOOP)]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ ConfigurationContainerInternal createConfigurationContainer(Instantiator instant
WorkerThreadRegistry workerThreadRegistry,
DomainObjectCollectionFactory domainObjectCollectionFactory,
NotationParser<Object, ComponentSelector> moduleSelectorNotationParser,
ObjectFactory objectFactory) {
ObjectFactory objectFactory,
FileResolver fileResolver) {
return instantiator.newInstance(DefaultConfigurationContainer.class,
configurationResolver,
instantiator,
Expand All @@ -371,7 +372,8 @@ ConfigurationContainerInternal createConfigurationContainer(Instantiator instant
workerThreadRegistry,
domainObjectCollectionFactory,
moduleSelectorNotationParser,
objectFactory
objectFactory,
fileResolver
);
}

Expand Down Expand Up @@ -464,8 +466,8 @@ DefaultComponentModuleMetadataHandler createComponentModuleMetadataHandler(Insta
return instantiator.newInstance(DefaultComponentModuleMetadataHandler.class, moduleIdentifierFactory);
}

ArtifactHandler createArtifactHandler(Instantiator instantiator, DependencyMetaDataProvider dependencyMetaDataProvider, ConfigurationContainerInternal configurationContainer, DomainObjectContext context) {
NotationParser<Object, ConfigurablePublishArtifact> publishArtifactNotationParser = new PublishArtifactNotationParserFactory(instantiator, dependencyMetaDataProvider, taskResolverFor(context)).create();
ArtifactHandler createArtifactHandler(Instantiator instantiator, DependencyMetaDataProvider dependencyMetaDataProvider, ConfigurationContainerInternal configurationContainer, DomainObjectContext context, FileResolver fileResolver) {
NotationParser<Object, ConfigurablePublishArtifact> publishArtifactNotationParser = new PublishArtifactNotationParserFactory(instantiator, dependencyMetaDataProvider, taskResolverFor(context), fileResolver).create();
return instantiator.newInstance(DefaultArtifactHandler.class, configurationContainer, publishArtifactNotationParser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.gradle.api.internal.attributes.ImmutableAttributesFactory;
import org.gradle.api.internal.collections.DomainObjectCollectionFactory;
import org.gradle.api.internal.file.FileCollectionFactory;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.notations.ComponentIdentifierParserFactory;
import org.gradle.api.internal.project.ProjectStateRegistry;
import org.gradle.api.internal.tasks.TaskResolver;
Expand Down Expand Up @@ -111,7 +112,8 @@ public DefaultConfigurationContainer(ConfigurationResolver resolver,
WorkerThreadRegistry workerThreadRegistry,
DomainObjectCollectionFactory domainObjectCollectionFactory,
NotationParser<Object, ComponentSelector> moduleSelectorNotationParser,
ObjectFactory objectFactory) {
ObjectFactory objectFactory,
FileResolver fileResolver) {
super(Configuration.class, instantiator, new Configuration.Namer(), callbackDecorator);
this.resolver = resolver;
this.instantiator = instantiator;
Expand All @@ -124,7 +126,7 @@ public DefaultConfigurationContainer(ConfigurationResolver resolver,
this.userCodeApplicationContext = userCodeApplicationContext;
this.workerThreadRegistry = workerThreadRegistry;
this.domainObjectCollectionFactory = domainObjectCollectionFactory;
this.artifactNotationParser = new PublishArtifactNotationParserFactory(instantiator, dependencyMetaDataProvider, taskResolver).create();
this.artifactNotationParser = new PublishArtifactNotationParserFactory(instantiator, dependencyMetaDataProvider, taskResolver, fileResolver).create();
this.capabilityNotationParser = new CapabilityNotationParserFactory(true).create();
this.attributesFactory = attributesFactory;
this.projectStateRegistry = projectStateRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskDependency;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.internal.deprecation.DeprecationLogger;

import java.io.File;
import java.util.Date;
Expand All @@ -40,7 +41,16 @@ public class LazyPublishArtifact implements PublishArtifactInternal {
private final FileResolver fileResolver;
private PublishArtifactInternal delegate;

/**
* @deprecated Provide a {@link FileResolver} instead using {@link LazyPublishArtifact#LazyPublishArtifact(Provider, FileResolver)}.
*/
@Deprecated
public LazyPublishArtifact(Provider<?> provider) {
DeprecationLogger.deprecateInternalApi("constructor LazyPublishArtifact(Provider<?>)")
.replaceWith("constructor LazyPublishArtifact(Provider<?>, FileResolver)")
.willBeRemovedInGradle8()
.withUpgradeGuideSection(7, "lazypublishartifact_fileresolver")
.nagUser();
this.provider = Providers.internal(provider);
this.version = null;
this.fileResolver = null;
Expand All @@ -52,10 +62,10 @@ public LazyPublishArtifact(Provider<?> provider, FileResolver fileResolver) {
this.fileResolver = fileResolver;
}

public LazyPublishArtifact(Provider<?> provider, String version) {
public LazyPublishArtifact(Provider<?> provider, String version, FileResolver fileResolver) {
this.provider = Providers.internal(provider);
this.version = version;
this.fileResolver = null;
this.fileResolver = fileResolver;
}

@Override
Expand Down Expand Up @@ -100,9 +110,11 @@ private PublishArtifactInternal getDelegate() {
delegate = new ArchivePublishArtifact((AbstractArchiveTask) value);
} else if (value instanceof Task) {
delegate = fromFile(((Task) value).getOutputs().getFiles().getSingleFile());
} else if (value instanceof CharSequence && fileResolver != null) {
delegate = fromFile(fileResolver.resolve(value.toString()));
} else if (fileResolver != null) {
delegate = fromFile(fileResolver.resolve(value));
} else {
// This case can be removed once the deprecated constructors are removed,
// because the file resolver will always be present.
throw new InvalidUserDataException(String.format("Cannot convert provided value (%s) to a file.", value));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact;
import org.gradle.api.internal.artifacts.publish.DecoratingPublishArtifact;
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.internal.tasks.TaskResolver;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
Expand All @@ -43,11 +44,13 @@ public class PublishArtifactNotationParserFactory implements Factory<NotationPar
private final Instantiator instantiator;
private final DependencyMetaDataProvider metaDataProvider;
private final TaskResolver taskResolver;
private final FileResolver fileResolver;

public PublishArtifactNotationParserFactory(Instantiator instantiator, DependencyMetaDataProvider metaDataProvider, TaskResolver taskResolver) {
public PublishArtifactNotationParserFactory(Instantiator instantiator, DependencyMetaDataProvider metaDataProvider, TaskResolver taskResolver, FileResolver fileResolver) {
this.instantiator = instantiator;
this.metaDataProvider = metaDataProvider;
this.taskResolver = taskResolver;
this.fileResolver = fileResolver;
}

@Override
Expand Down Expand Up @@ -111,7 +114,7 @@ protected PublishArtifact parseMap(@MapKey("file") File file) {
private class FileProviderNotationConverter extends TypedNotationConverter<Provider<?>, ConfigurablePublishArtifact> {
@SuppressWarnings("unchecked")
FileProviderNotationConverter() {
super((Class)Provider.class);
super((Class<Provider<?>>) (Class<?>) Provider.class);
}

@Override
Expand All @@ -124,7 +127,7 @@ public void describe(DiagnosticsVisitor visitor) {
@Override
protected ConfigurablePublishArtifact parseType(Provider<?> notation) {
Module module = metaDataProvider.getModule();
return instantiator.newInstance(DecoratingPublishArtifact.class, new LazyPublishArtifact(notation, module.getVersion()));
return instantiator.newInstance(DecoratingPublishArtifact.class, new LazyPublishArtifact(notation, module.getVersion(), fileResolver));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.gradle.api.internal.artifacts.dsl.dependencies.DependencyLockingProvi
import org.gradle.api.internal.artifacts.ivyservice.dependencysubstitution.DependencySubstitutionRules
import org.gradle.api.internal.artifacts.ivyservice.moduleconverter.LocalComponentMetadataBuilder
import org.gradle.api.internal.file.FileCollectionFactory
import org.gradle.api.internal.file.TestFiles
import org.gradle.api.internal.initialization.RootScriptDomainObjectContext
import org.gradle.api.internal.project.ProjectStateRegistry
import org.gradle.api.internal.tasks.TaskResolver
Expand Down Expand Up @@ -80,7 +81,7 @@ class DefaultConfigurationContainerSpec extends Specification {
private DefaultConfigurationContainer configurationContainer = new DefaultConfigurationContainer(resolver, instantiator, domainObjectContext, listenerManager, metaDataProvider,
metaDataBuilder, fileCollectionFactory, globalSubstitutionRules, vcsMappingsInternal, componentIdentifierFactory, buildOperationExecutor, taskResolver,
immutableAttributesFactory, moduleIdentifierFactory, componentSelectorConverter, dependencyLockingProvider, projectStateRegistry, calculatedValueContainerFactory, documentationRegistry,
domainObjectCollectionCallbackActionDecorator, userCodeApplicationContext, Mock(WorkerThreadRegistry), TestUtil.domainObjectCollectionFactory(), Mock(NotationParser), TestUtil.objectFactory())
domainObjectCollectionCallbackActionDecorator, userCodeApplicationContext, Mock(WorkerThreadRegistry), TestUtil.domainObjectCollectionFactory(), Mock(NotationParser), TestUtil.objectFactory(), TestFiles.resolver())

def "adds and gets"() {
1 * domainObjectContext.identityPath("compile") >> Path.path(":build:compile")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class DefaultConfigurationContainerTest extends Specification {
Mock(WorkerThreadRegistry),
TestUtil.domainObjectCollectionFactory(),
Mock(NotationParser),
TestUtil.objectFactory()
TestUtil.objectFactory(),
TestFiles.resolver()
)

def addsNewConfigurationWhenConfiguringSelf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.gradle.api.internal.artifacts.dsl

import org.gradle.api.InvalidUserDataException
import org.gradle.api.Task
import org.gradle.api.artifacts.ConfigurablePublishArtifact
import org.gradle.api.artifacts.PublishArtifact
Expand All @@ -26,24 +25,29 @@ import org.gradle.api.internal.artifacts.configurations.DependencyMetaDataProvid
import org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact
import org.gradle.api.internal.artifacts.publish.DecoratingPublishArtifact
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact
import org.gradle.api.internal.file.TestFiles
import org.gradle.api.internal.provider.ProviderInternal
import org.gradle.api.internal.tasks.TaskDependencyResolveContext
import org.gradle.api.internal.tasks.TaskResolver
import org.gradle.api.tasks.bundling.AbstractArchiveTask
import org.gradle.internal.reflect.Instantiator
import org.gradle.internal.typeconversion.NotationParser
import org.gradle.internal.typeconversion.UnsupportedNotationException
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.gradle.util.TestUtil
import org.gradle.util.internal.TextUtil
import org.junit.Rule
import spock.lang.Specification

import java.awt.*

class PublishArtifactNotationParserFactoryTest extends Specification {
@Rule
public final TestNameTestDirectoryProvider tmpDir = new TestNameTestDirectoryProvider(getClass())
final DependencyMetaDataProvider provider = Mock()
final TaskResolver taskResolver = Mock()
final Instantiator instantiator = TestUtil.instantiatorFactory().decorateLenient()
final PublishArtifactNotationParserFactory publishArtifactNotationParserFactory = new PublishArtifactNotationParserFactory(instantiator, provider, taskResolver)
final PublishArtifactNotationParserFactory publishArtifactNotationParserFactory = new PublishArtifactNotationParserFactory(instantiator, provider, taskResolver, TestFiles.resolver(tmpDir.getTestDirectory()))
final NotationParser<Object, PublishArtifact> publishArtifactNotationParser = publishArtifactNotationParserFactory.create();

def setup() {
Expand Down Expand Up @@ -241,14 +245,19 @@ class PublishArtifactNotationParserFactoryTest extends Specification {

given:
def publishArtifact = publishArtifactNotationParser.parseNotation(provider)
provider.get() >> "broken"
provider.get() >> new Object() {
@Override
String toString() {
return "broken"
}
}

when:
publishArtifact.file

then:
def e = thrown(InvalidUserDataException)
e.message == "Cannot convert provided value (broken) to a file."
def e = thrown(UnsupportedNotationException)
e.message.startsWith("Cannot convert the provided notation to a File or URI: broken.")
}

def createArtifactFromFileInMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ To ignore directories in Gradle 8.0 and later, the input property needs to be an
Finally, using `@link:{javadocPath}/org/gradle/api/tasks/InputDirectory.html[InputDirectory]` implies `@IgnoreEmptyDirectories`, so no changes are necessary when using this annotation.
The same is true for `@link:{javadocPath}/org/gradle/api/tasks/TaskInputs.html#dir-java.lang.Object-[inputs.dir()]` when registering an input directory via the runtime API.

[[lazypublishartifact_fileresolver]]
==== Using LazyPublishArtifact without a FileResolver is deprecated

When using a LazyPublishArtifact without a FileResolver, a different file resolution strategy is used, which duplicates
some logic in the FileResolver. To improve consistency, LazyPublishArtifact should be used with a FileResolver, and will
require it in the future.

This also affects other internal APIs that use LazyPublishArtifact, which now also have deprecation warnings where needed.

[[changes_7.3]]
== Upgrading from 7.2 and earlier

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
import org.gradle.api.internal.plugins.DefaultArtifactPublicationSet;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaPlugin;
Expand Down Expand Up @@ -129,7 +130,7 @@ public void execute(Ear ear) {
deploymentDescriptor.setDescription(project.getDescription());
}
}
project.getExtensions().getByType(DefaultArtifactPublicationSet.class).addCandidate(new LazyPublishArtifact(ear));
project.getExtensions().getByType(DefaultArtifactPublicationSet.class).addCandidate(new LazyPublishArtifact(ear, ((ProjectInternal) project).getFileResolver()));

project.getTasks().withType(Ear.class).configureEach(new Action<Ear>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.internal.artifacts.dsl.LazyPublishArtifact;
import org.gradle.api.internal.attributes.ImmutableAttributesFactory;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.ProviderFactory;
Expand Down Expand Up @@ -166,7 +167,7 @@ private Stream<CppBinary> getDebugStaticHostStream() {
task.getArchiveClassifier().set("cpp-api-headers");
task.getArchiveFileName().set("cpp-api-headers.zip");
});
library.getMainPublication().addArtifact(new LazyPublishArtifact(headersZip));
library.getMainPublication().addArtifact(new LazyPublishArtifact(headersZip, ((ProjectInternal) project).getFileResolver()));
});

library.getBinaries().realizeNow();
Expand Down