Search in sources :

Example 1 with SoftwareComponent

use of org.gradle.api.component.SoftwareComponent in project gradle by gradle.

the class IvyPluginPublishingRules method addMainPublication.

@Mutate
public void addMainPublication(PublishingExtension publishing, GradlePluginDevelopmentExtension pluginDevelopment, ServiceRegistry services) {
    if (!pluginDevelopment.isAutomatedPublishing()) {
        return;
    }
    SoftwareComponentContainer componentContainer = services.get(SoftwareComponentContainer.class);
    SoftwareComponent component = componentContainer.getByName("java");
    PublicationContainer publications = publishing.getPublications();
    createIvyPluginPublication(component, publications);
}
Also used : PublicationContainer(org.gradle.api.publish.PublicationContainer) SoftwareComponent(org.gradle.api.component.SoftwareComponent) SoftwareComponentContainer(org.gradle.api.component.SoftwareComponentContainer) Mutate(org.gradle.model.Mutate)

Example 2 with SoftwareComponent

use of org.gradle.api.component.SoftwareComponent in project gradle by gradle.

the class DefaultProjectDependencyPublicationResolver method resolve.

@Override
public <T> T resolve(Class<T> coordsType, ProjectDependency dependency) {
    // Could probably apply some caching and some immutable types
    ProjectInternal dependencyProject = (ProjectInternal) dependency.getDependencyProject();
    // Ensure target project is configured
    projectConfigurer.configureFully(dependencyProject);
    List<ProjectPublication> publications = new ArrayList<ProjectPublication>();
    for (ProjectPublication publication : publicationRegistry.getPublications(dependencyProject.getPath())) {
        if (!publication.isLegacy() && publication.getCoordinates(coordsType) != null) {
            publications.add(publication);
        }
    }
    if (publications.isEmpty()) {
        // Project has no publications: simply use the project name in place of the dependency name
        if (coordsType.isAssignableFrom(ModuleVersionIdentifier.class)) {
            return coordsType.cast(new DefaultModuleVersionIdentifier(dependency.getGroup(), dependencyProject.getName(), dependency.getVersion()));
        }
        throw new UnsupportedOperationException(String.format("Could not find any publications of type %s in %s.", coordsType.getSimpleName(), dependencyProject.getDisplayName()));
    }
    // Select all entry points. An entry point is a publication that does not contain a component whose parent is also published
    Set<SoftwareComponent> ignored = new HashSet<SoftwareComponent>();
    for (ProjectPublication publication : publications) {
        if (publication.getComponent() != null && publication.getComponent() instanceof ComponentWithVariants) {
            ComponentWithVariants parent = (ComponentWithVariants) publication.getComponent();
            ignored.addAll(parent.getVariants());
        }
    }
    Set<ProjectPublication> topLevel = new LinkedHashSet<ProjectPublication>();
    for (ProjectPublication publication : publications) {
        if (!publication.isAlias() && (publication.getComponent() == null || !ignored.contains(publication.getComponent()))) {
            topLevel.add(publication);
        }
    }
    // See if all entry points have the same identifier
    Iterator<ProjectPublication> iterator = topLevel.iterator();
    T candidate = iterator.next().getCoordinates(coordsType);
    while (iterator.hasNext()) {
        T alternative = iterator.next().getCoordinates(coordsType);
        if (!candidate.equals(alternative)) {
            TreeFormatter formatter = new TreeFormatter();
            formatter.node("Publishing is not able to resolve a dependency on a project with multiple publications that have different coordinates.");
            formatter.node("Found the following publications in " + dependencyProject.getDisplayName());
            formatter.startChildren();
            for (ProjectPublication publication : topLevel) {
                formatter.node(publication.getDisplayName().getCapitalizedDisplayName() + " with coordinates " + publication.getCoordinates(coordsType));
            }
            formatter.endChildren();
            throw new UnsupportedOperationException(formatter.toString());
        }
    }
    return candidate;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DefaultModuleVersionIdentifier(org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier) ProjectInternal(org.gradle.api.internal.project.ProjectInternal) ArrayList(java.util.ArrayList) ComponentWithVariants(org.gradle.api.component.ComponentWithVariants) TreeFormatter(org.gradle.internal.text.TreeFormatter) SoftwareComponent(org.gradle.api.component.SoftwareComponent) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with SoftwareComponent

use of org.gradle.api.component.SoftwareComponent in project gradle by gradle.

the class NativeBasePlugin method addLifecycleTasks.

private void addLifecycleTasks(final TaskContainer tasks, final SoftwareComponentContainer components) {
    components.withType(ComponentWithBinaries.class, new Action<ComponentWithBinaries>() {

        @Override
        public void execute(final ComponentWithBinaries component) {
            // Register each child of each component
            component.getBinaries().whenElementKnown(new Action<SoftwareComponent>() {

                @Override
                public void execute(SoftwareComponent binary) {
                    components.add(binary);
                }
            });
            if (component instanceof ProductionComponent) {
                // Add an assemble task for each binary and also wire the development binary in to the `assemble` task
                component.getBinaries().whenElementFinalized(ComponentWithOutputs.class, new Action<ComponentWithOutputs>() {

                    @Override
                    public void execute(ComponentWithOutputs binary) {
                        // Determine which output to produce at development time.
                        FileCollection outputs = binary.getOutputs();
                        Names names = ((ComponentWithNames) binary).getNames();
                        Task lifecycleTask = tasks.create(names.getTaskName("assemble"));
                        lifecycleTask.dependsOn(outputs);
                        if (binary == ((ProductionComponent) component).getDevelopmentBinary().get()) {
                            tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME).dependsOn(outputs);
                        }
                    }
                });
            }
        }
    });
}
Also used : ComponentWithNames(org.gradle.language.nativeplatform.internal.ComponentWithNames) Names(org.gradle.language.nativeplatform.internal.Names) Action(org.gradle.api.Action) Task(org.gradle.api.Task) AbstractLinkTask(org.gradle.nativeplatform.tasks.AbstractLinkTask) ComponentWithBinaries(org.gradle.language.ComponentWithBinaries) ComponentWithOutputs(org.gradle.language.ComponentWithOutputs) ProductionComponent(org.gradle.language.ProductionComponent) FileCollection(org.gradle.api.file.FileCollection) ComponentWithNames(org.gradle.language.nativeplatform.internal.ComponentWithNames) SoftwareComponent(org.gradle.api.component.SoftwareComponent)

Example 4 with SoftwareComponent

use of org.gradle.api.component.SoftwareComponent in project gradle by gradle.

the class NativeBasePlugin method addPublicationsFromVariants.

private void addPublicationsFromVariants(final Project project, final SoftwareComponentContainer components) {
    project.getPluginManager().withPlugin("maven-publish", plugin -> {
        components.withType(PublicationAwareComponent.class, component -> {
            project.getExtensions().configure(PublishingExtension.class, publishing -> {
                final ComponentWithVariants mainVariant = component.getMainPublication();
                publishing.getPublications().create("main", MavenPublication.class, publication -> {
                    MavenPublicationInternal publicationInternal = (MavenPublicationInternal) publication;
                    publicationInternal.getMavenProjectIdentity().getArtifactId().set(component.getBaseName());
                    publicationInternal.from(mainVariant);
                    publicationInternal.publishWithOriginalFileName();
                });
                Set<? extends SoftwareComponent> variants = mainVariant.getVariants();
                if (variants instanceof DomainObjectSet) {
                    ((DomainObjectSet<? extends SoftwareComponent>) variants).all(child -> addPublicationFromVariant(child, publishing, project));
                } else {
                    for (SoftwareComponent variant : variants) {
                        addPublicationFromVariant(variant, publishing, project);
                    }
                }
            });
        });
    });
}
Also used : ComponentWithVariants(org.gradle.api.component.ComponentWithVariants) MavenPublicationInternal(org.gradle.api.publish.maven.internal.publication.MavenPublicationInternal) SoftwareComponent(org.gradle.api.component.SoftwareComponent) DomainObjectSet(org.gradle.api.DomainObjectSet)

Example 5 with SoftwareComponent

use of org.gradle.api.component.SoftwareComponent in project gradle by gradle.

the class DefaultProjectDependencyPublicationResolver method resolve.

@Override
public <T> T resolve(Class<T> coordsType, ProjectInternal project) {
    // Ensure target project is configured
    projectConfigurer.configureFully(project);
    List<ProjectComponentPublication> publications = new ArrayList<>();
    for (ProjectComponentPublication publication : publicationRegistry.getPublications(ProjectComponentPublication.class, project.getIdentityPath())) {
        if (!publication.isLegacy() && publication.getCoordinates(coordsType) != null) {
            publications.add(publication);
        }
    }
    if (publications.isEmpty()) {
        // Project has no publications: simply use the project name in place of the dependency name
        if (coordsType.isAssignableFrom(ModuleVersionIdentifier.class)) {
            return coordsType.cast(DefaultModuleVersionIdentifier.newId(project.getGroup().toString(), project.getName(), project.getVersion().toString()));
        }
        throw new UnsupportedOperationException(String.format("Could not find any publications of type %s in %s.", coordsType.getSimpleName(), project.getDisplayName()));
    }
    // Select all entry points. An entry point is a publication that does not contain a component whose parent is also published
    Set<SoftwareComponent> ignored = new HashSet<>();
    for (ProjectComponentPublication publication : publications) {
        if (publication.getComponent() != null && publication.getComponent() instanceof ComponentWithVariants) {
            ComponentWithVariants parent = (ComponentWithVariants) publication.getComponent();
            ignored.addAll(parent.getVariants());
        }
    }
    Set<ProjectComponentPublication> topLevel = new LinkedHashSet<>();
    Set<ProjectComponentPublication> topLevelWithComponent = new LinkedHashSet<>();
    for (ProjectComponentPublication publication : publications) {
        if (!publication.isAlias() && (publication.getComponent() == null || !ignored.contains(publication.getComponent()))) {
            topLevel.add(publication);
            if (publication.getComponent() != null) {
                topLevelWithComponent.add(publication);
            }
        }
    }
    if (topLevelWithComponent.size() == 1) {
        return topLevelWithComponent.iterator().next().getCoordinates(coordsType);
    }
    // See if all entry points have the same identifier
    Iterator<ProjectComponentPublication> iterator = topLevel.iterator();
    T candidate = iterator.next().getCoordinates(coordsType);
    while (iterator.hasNext()) {
        T alternative = iterator.next().getCoordinates(coordsType);
        if (!candidate.equals(alternative)) {
            TreeFormatter formatter = new TreeFormatter();
            formatter.node("Publishing is not able to resolve a dependency on a project with multiple publications that have different coordinates.");
            formatter.node("Found the following publications in " + project.getDisplayName());
            formatter.startChildren();
            for (ProjectComponentPublication publication : topLevel) {
                formatter.node(publication.getDisplayName().getCapitalizedDisplayName() + " with coordinates " + publication.getCoordinates(coordsType));
            }
            formatter.endChildren();
            throw new UnsupportedOperationException(formatter.toString());
        }
    }
    return candidate;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ComponentWithVariants(org.gradle.api.component.ComponentWithVariants) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter) SoftwareComponent(org.gradle.api.component.SoftwareComponent) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

SoftwareComponent (org.gradle.api.component.SoftwareComponent)11 ComponentWithVariants (org.gradle.api.component.ComponentWithVariants)5 ArrayList (java.util.ArrayList)3 ModuleVersionIdentifier (org.gradle.api.artifacts.ModuleVersionIdentifier)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 SoftwareComponentContainer (org.gradle.api.component.SoftwareComponentContainer)2 SoftwareComponentInternal (org.gradle.api.internal.component.SoftwareComponentInternal)2 UsageContext (org.gradle.api.internal.component.UsageContext)2 PublicationContainer (org.gradle.api.publish.PublicationContainer)2 Mutate (org.gradle.model.Mutate)2 JsonWriter (com.google.gson.stream.JsonWriter)1 Action (org.gradle.api.Action)1 DomainObjectSet (org.gradle.api.DomainObjectSet)1 Task (org.gradle.api.Task)1 ComponentWithCoordinates (org.gradle.api.component.ComponentWithCoordinates)1 FileCollection (org.gradle.api.file.FileCollection)1 DefaultModuleVersionIdentifier (org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier)1 ProjectInternal (org.gradle.api.internal.project.ProjectInternal)1