Search in sources :

Example 1 with MavenDependency

use of org.gradle.api.publish.maven.MavenDependency in project gradle by gradle.

the class MavenPomFileGenerator method addDependencyManagement.

private void addDependencyManagement(MavenDependency dependency, String scope) {
    Dependency mavenDependency = new Dependency();
    mavenDependency.setGroupId(dependency.getGroupId());
    mavenDependency.setArtifactId(dependency.getArtifactId());
    mavenDependency.setVersion(mapToMavenSyntax(dependency.getVersion()));
    mavenDependency.setScope(scope);
    DependencyManagement dependencyManagement = getModel().getDependencyManagement();
    if (dependencyManagement == null) {
        dependencyManagement = new DependencyManagement();
        getModel().setDependencyManagement(dependencyManagement);
    }
    dependencyManagement.addDependency(mavenDependency);
}
Also used : MavenDependency(org.gradle.api.publish.maven.MavenDependency) Dependency(org.apache.maven.model.Dependency) DependencyManagement(org.apache.maven.model.DependencyManagement)

Example 2 with MavenDependency

use of org.gradle.api.publish.maven.MavenDependency in project gradle by gradle.

the class MavenPomFileGenerator method addDependency.

private void addDependency(MavenDependencyInternal dependency, String artifactId, String scope, String type, String classifier, boolean optional) {
    Dependency mavenDependency = new Dependency();
    String groupId = dependency.getGroupId();
    String dependencyVersion = dependency.getVersion();
    String projectPath = dependency.getProjectPath();
    ImmutableAttributes attributes = attributesForScope(scope);
    ModuleVersionIdentifier resolvedVersion = versionMappingStrategy.findStrategyForVariant(attributes).maybeResolveVersion(groupId, artifactId, projectPath);
    mavenDependency.setGroupId(resolvedVersion != null ? resolvedVersion.getGroup() : groupId);
    mavenDependency.setArtifactId(resolvedVersion != null ? resolvedVersion.getName() : artifactId);
    mavenDependency.setVersion(resolvedVersion != null ? resolvedVersion.getVersion() : mapToMavenSyntax(dependencyVersion));
    mavenDependency.setType(type);
    mavenDependency.setScope(scope);
    mavenDependency.setClassifier(classifier);
    if (optional) {
        // Not using setOptional(optional) in order to avoid <optional>false</optional> in the common case
        mavenDependency.setOptional(true);
    }
    for (ExcludeRule excludeRule : dependency.getExcludeRules()) {
        Exclusion exclusion = new Exclusion();
        exclusion.setGroupId(GUtil.elvis(excludeRule.getGroup(), "*"));
        exclusion.setArtifactId(GUtil.elvis(excludeRule.getModule(), "*"));
        mavenDependency.addExclusion(exclusion);
    }
    model.addDependency(mavenDependency);
}
Also used : ModuleVersionIdentifier(org.gradle.api.artifacts.ModuleVersionIdentifier) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) Exclusion(org.apache.maven.model.Exclusion) MavenDependency(org.gradle.api.publish.maven.MavenDependency) Dependency(org.apache.maven.model.Dependency) ExcludeRule(org.gradle.api.artifacts.ExcludeRule)

Example 3 with MavenDependency

use of org.gradle.api.publish.maven.MavenDependency in project gradle by gradle.

the class DefaultMavenPublication method from.

public void from(SoftwareComponent component) {
    if (this.component != null) {
        throw new InvalidUserDataException(String.format("Maven publication '%s' cannot include multiple components", name));
    }
    this.component = (SoftwareComponentInternal) component;
    Set<ArtifactKey> seenArtifacts = Sets.newHashSet();
    Set<ModuleDependency> seenDependencies = Sets.newHashSet();
    Set<DependencyConstraint> seenConstraints = Sets.newHashSet();
    for (UsageContext usageContext : getSortedUsageContexts()) {
        // TODO Need a smarter way to map usage to artifact classifier
        for (PublishArtifact publishArtifact : usageContext.getArtifacts()) {
            ArtifactKey key = new ArtifactKey(publishArtifact.getFile(), publishArtifact.getClassifier(), publishArtifact.getExtension());
            if (seenArtifacts.add(key)) {
                artifact(publishArtifact);
            }
        }
        Set<MavenDependencyInternal> dependencies = dependenciesFor(usageContext.getUsage());
        for (ModuleDependency dependency : usageContext.getDependencies()) {
            if (seenDependencies.add(dependency)) {
                if (dependency instanceof ProjectDependency) {
                    addProjectDependency((ProjectDependency) dependency, dependencies);
                } else {
                    addModuleDependency(dependency, dependencies);
                }
            }
        }
        Set<MavenDependency> dependencyConstraints = dependencyConstraintsFor(usageContext.getUsage());
        for (DependencyConstraint dependency : usageContext.getDependencyConstraints()) {
            if (seenConstraints.add(dependency) && !dependency.getVersionConstraint().getPreferredVersion().isEmpty()) {
                addDependencyConstraint(dependency, dependencyConstraints);
            }
        }
    }
}
Also used : ModuleDependency(org.gradle.api.artifacts.ModuleDependency) DependencyConstraint(org.gradle.api.artifacts.DependencyConstraint) ProjectDependency(org.gradle.api.artifacts.ProjectDependency) MavenDependencyInternal(org.gradle.api.publish.maven.internal.dependencies.MavenDependencyInternal) UsageContext(org.gradle.api.internal.component.UsageContext) InvalidUserDataException(org.gradle.api.InvalidUserDataException) PublishArtifact(org.gradle.api.artifacts.PublishArtifact) DefaultMavenDependency(org.gradle.api.publish.maven.internal.dependencies.DefaultMavenDependency) MavenDependency(org.gradle.api.publish.maven.MavenDependency)

Example 4 with MavenDependency

use of org.gradle.api.publish.maven.MavenDependency in project gradle by gradle.

the class MavenPomFileGenerator method addDependency.

private void addDependency(MavenDependencyInternal dependency, String artifactId, String scope, String type, String classifier) {
    Dependency mavenDependency = new Dependency();
    mavenDependency.setGroupId(dependency.getGroupId());
    mavenDependency.setArtifactId(artifactId);
    mavenDependency.setVersion(mapToMavenSyntax(dependency.getVersion()));
    mavenDependency.setType(type);
    mavenDependency.setScope(scope);
    mavenDependency.setClassifier(classifier);
    for (ExcludeRule excludeRule : dependency.getExcludeRules()) {
        Exclusion exclusion = new Exclusion();
        exclusion.setGroupId(GUtil.elvis(excludeRule.getGroup(), "*"));
        exclusion.setArtifactId(GUtil.elvis(excludeRule.getModule(), "*"));
        mavenDependency.addExclusion(exclusion);
    }
    getModel().addDependency(mavenDependency);
}
Also used : Exclusion(org.apache.maven.model.Exclusion) MavenDependency(org.gradle.api.publish.maven.MavenDependency) Dependency(org.apache.maven.model.Dependency) ExcludeRule(org.gradle.api.artifacts.ExcludeRule)

Example 5 with MavenDependency

use of org.gradle.api.publish.maven.MavenDependency in project gradle by gradle.

the class GenerateMavenPom method doGenerate.

@TaskAction
public void doGenerate() {
    MavenPomInternal pomInternal = (MavenPomInternal) getPom();
    MavenPomFileGenerator pomGenerator = new MavenPomFileGenerator(pomInternal.getProjectIdentity(), getVersionRangeMapper());
    pomGenerator.setPackaging(pomInternal.getPackaging());
    for (MavenDependency mavenDependency : pomInternal.getApiDependencyManagement()) {
        pomGenerator.addApiDependencyManagement(mavenDependency);
    }
    for (MavenDependency mavenDependency : pomInternal.getRuntimeDependencyManagement()) {
        pomGenerator.addRuntimeDependencyManagement(mavenDependency);
    }
    for (MavenDependencyInternal runtimeDependency : pomInternal.getApiDependencies()) {
        pomGenerator.addApiDependency(runtimeDependency);
    }
    for (MavenDependencyInternal runtimeDependency : pomInternal.getRuntimeDependencies()) {
        pomGenerator.addRuntimeDependency(runtimeDependency);
    }
    pomGenerator.withXml(pomInternal.getXmlAction());
    pomGenerator.writeTo(getDestination());
}
Also used : MavenPomInternal(org.gradle.api.publish.maven.internal.publication.MavenPomInternal) MavenPomFileGenerator(org.gradle.api.publish.maven.internal.tasks.MavenPomFileGenerator) MavenDependencyInternal(org.gradle.api.publish.maven.internal.dependencies.MavenDependencyInternal) MavenDependency(org.gradle.api.publish.maven.MavenDependency) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

MavenDependency (org.gradle.api.publish.maven.MavenDependency)8 Dependency (org.apache.maven.model.Dependency)4 MavenDependencyInternal (org.gradle.api.publish.maven.internal.dependencies.MavenDependencyInternal)4 ExcludeRule (org.gradle.api.artifacts.ExcludeRule)3 DependencyManagement (org.apache.maven.model.DependencyManagement)2 Exclusion (org.apache.maven.model.Exclusion)2 DependencyConstraint (org.gradle.api.artifacts.DependencyConstraint)2 ModuleDependency (org.gradle.api.artifacts.ModuleDependency)2 ModuleVersionIdentifier (org.gradle.api.artifacts.ModuleVersionIdentifier)2 ProjectDependency (org.gradle.api.artifacts.ProjectDependency)2 PublishArtifact (org.gradle.api.artifacts.PublishArtifact)2 ImmutableAttributes (org.gradle.api.internal.attributes.ImmutableAttributes)2 UsageContext (org.gradle.api.internal.component.UsageContext)2 DefaultMavenDependency (org.gradle.api.publish.maven.internal.dependencies.DefaultMavenDependency)2 MavenPomInternal (org.gradle.api.publish.maven.internal.publication.MavenPomInternal)2 MavenPomFileGenerator (org.gradle.api.publish.maven.internal.tasks.MavenPomFileGenerator)2 InvalidUserDataException (org.gradle.api.InvalidUserDataException)1 Capability (org.gradle.api.capabilities.Capability)1 DefaultExcludeRule (org.gradle.api.internal.artifacts.DefaultExcludeRule)1 DefaultProjectDependencyConstraint (org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependencyConstraint)1