Search in sources :

Example 6 with InvalidUserCodeException

use of org.gradle.api.InvalidUserCodeException in project gradle by gradle.

the class DefaultVersionMappingStrategy method findStrategyForVariant.

@Override
public VariantVersionMappingStrategyInternal findStrategyForVariant(ImmutableAttributes variantAttributes) {
    DefaultVariantVersionMappingStrategy strategy = createDefaultMappingStrategy(variantAttributes);
    // Apply strategies for "all variants"
    for (Action<? super VariantVersionMappingStrategy> action : mappingsForAllVariants) {
        action.execute(strategy);
    }
    // Then use attribute specific mapping
    if (!attributeBasedMappings.isEmpty()) {
        AttributeMatcher matcher = schema.matcher();
        Set<ImmutableAttributes> candidates = attributeBasedMappings.keySet();
        List<ImmutableAttributes> matches = matcher.matches(candidates, variantAttributes, AttributeMatchingExplanationBuilder.NO_OP);
        if (matches.size() == 1) {
            Collection<Action<? super VariantVersionMappingStrategy>> actions = attributeBasedMappings.get(matches.get(0));
            for (Action<? super VariantVersionMappingStrategy> action : actions) {
                action.execute(strategy);
            }
        } else if (matches.size() > 1) {
            throw new InvalidUserCodeException("Unable to find a suitable version mapping strategy for " + variantAttributes);
        }
    }
    return strategy;
}
Also used : AttributeMatcher(org.gradle.internal.component.model.AttributeMatcher) Action(org.gradle.api.Action) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) VariantVersionMappingStrategy(org.gradle.api.publish.VariantVersionMappingStrategy)

Example 7 with InvalidUserCodeException

use of org.gradle.api.InvalidUserCodeException in project gradle by gradle.

the class DefaultVersionMappingStrategy method findStrategyForVariant.

@Override
public VariantVersionMappingStrategyInternal findStrategyForVariant(ImmutableAttributes variantAttributes) {
    DefaultVariantVersionMappingStrategy strategy = createDefaultMappingStrategy(variantAttributes);
    // Apply strategies for "all variants"
    for (Action<? super VariantVersionMappingStrategy> action : mappingsForAllVariants) {
        action.execute(strategy);
    }
    // Then use attribute specific mapping
    if (!attributeBasedMappings.isEmpty()) {
        AttributeMatcher matcher = schema.matcher();
        Set<ImmutableAttributes> candidates = attributeBasedMappings.keySet();
        List<ImmutableAttributes> matches = matcher.matches(candidates, variantAttributes, AttributeMatchingExplanationBuilder.NO_OP);
        if (matches.size() == 1) {
            Collection<Action<? super VariantVersionMappingStrategy>> actions = attributeBasedMappings.get(matches.get(0));
            for (Action<? super VariantVersionMappingStrategy> action : actions) {
                action.execute(strategy);
            }
        } else if (matches.size() > 1) {
            throw new InvalidUserCodeException("Unable to find a suitable version mapping strategy for " + variantAttributes);
        }
    }
    return strategy;
}
Also used : AttributeMatcher(org.gradle.internal.component.model.AttributeMatcher) Action(org.gradle.api.Action) ImmutableAttributes(org.gradle.api.internal.attributes.ImmutableAttributes) InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) DefaultVariantVersionMappingStrategy(org.gradle.api.publish.internal.versionmapping.DefaultVariantVersionMappingStrategy) DefaultVariantVersionMappingStrategy(org.gradle.api.publish.internal.versionmapping.DefaultVariantVersionMappingStrategy) VariantVersionMappingStrategy(org.gradle.api.publish.VariantVersionMappingStrategy)

Example 8 with InvalidUserCodeException

use of org.gradle.api.InvalidUserCodeException in project gradle by gradle.

the class ScalaBasePlugin method configureConfigurations.

private void configureConfigurations(final Project project, final Usage incrementalAnalysisUsage, ScalaPluginExtension scalaPluginExtension) {
    DependencyHandler dependencyHandler = project.getDependencies();
    ConfigurationInternal plugins = (ConfigurationInternal) project.getConfigurations().create(SCALA_COMPILER_PLUGINS_CONFIGURATION_NAME);
    plugins.setTransitive(false);
    plugins.setCanBeConsumed(false);
    jvmEcosystemUtilities.configureAsRuntimeClasspath(plugins);
    Configuration zinc = project.getConfigurations().create(ZINC_CONFIGURATION_NAME);
    zinc.setVisible(false);
    zinc.setDescription("The Zinc incremental compiler to be used for this Scala project.");
    ((DeprecatableConfiguration) zinc).deprecateForConsumption(deprecation -> deprecation.willBecomeAnErrorInGradle8().withUpgradeGuideSection(7, "plugin_configuration_consumption"));
    zinc.getResolutionStrategy().eachDependency(rule -> {
        if (rule.getRequested().getGroup().equals("com.typesafe.zinc") && rule.getRequested().getName().equals("zinc")) {
            rule.useTarget("org.scala-sbt:zinc_" + DEFAULT_SCALA_ZINC_VERSION + ":" + DEFAULT_ZINC_VERSION);
            rule.because("Typesafe Zinc is no longer maintained.");
        }
    });
    zinc.defaultDependencies(dependencies -> {
        dependencies.add(dependencyHandler.create("org.scala-sbt:zinc_" + DEFAULT_SCALA_ZINC_VERSION + ":" + scalaPluginExtension.getZincVersion().get()));
        // Add safeguard and clear error if the user changed the scala version when using default zinc
        zinc.getIncoming().afterResolve(resolvableDependencies -> {
            resolvableDependencies.getResolutionResult().allComponents(component -> {
                if (component.getModuleVersion() != null && component.getModuleVersion().getName().equals("scala-library")) {
                    if (!component.getModuleVersion().getVersion().startsWith(DEFAULT_SCALA_ZINC_VERSION)) {
                        throw new InvalidUserCodeException("The version of 'scala-library' was changed while using the default Zinc version. " + "Version " + component.getModuleVersion().getVersion() + " is not compatible with org.scala-sbt:zinc_" + DEFAULT_SCALA_ZINC_VERSION + ":" + DEFAULT_ZINC_VERSION);
                    }
                }
            });
        });
    });
    zinc.getDependencyConstraints().add(dependencyHandler.getConstraints().create(Log4jBannedVersion.LOG4J2_CORE_COORDINATES, constraint -> constraint.version(version -> {
        version.require(Log4jBannedVersion.LOG4J2_CORE_REQUIRED_VERSION);
        version.reject(Log4jBannedVersion.LOG4J2_CORE_VULNERABLE_VERSION_RANGE);
    })));
    final Configuration incrementalAnalysisElements = project.getConfigurations().create("incrementalScalaAnalysisElements");
    incrementalAnalysisElements.setVisible(false);
    incrementalAnalysisElements.setDescription("Incremental compilation analysis files");
    incrementalAnalysisElements.setCanBeResolved(false);
    incrementalAnalysisElements.setCanBeConsumed(true);
    incrementalAnalysisElements.getAttributes().attribute(USAGE_ATTRIBUTE, incrementalAnalysisUsage);
    AttributeMatchingStrategy<Usage> matchingStrategy = dependencyHandler.getAttributesSchema().attribute(USAGE_ATTRIBUTE);
    matchingStrategy.getDisambiguationRules().add(UsageDisambiguationRules.class, actionConfiguration -> {
        actionConfiguration.params(incrementalAnalysisUsage);
        actionConfiguration.params(objectFactory.named(Usage.class, Usage.JAVA_API));
        actionConfiguration.params(objectFactory.named(Usage.class, Usage.JAVA_RUNTIME));
    });
}
Also used : ProjectComponentIdentifier(org.gradle.api.artifacts.component.ProjectComponentIdentifier) BiFunction(java.util.function.BiFunction) InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) Task(org.gradle.api.Task) Provider(org.gradle.api.provider.Provider) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) MultipleCandidatesDetails(org.gradle.api.attributes.MultipleCandidatesDetails) TaskProvider(org.gradle.api.tasks.TaskProvider) Log4jBannedVersion(org.gradle.internal.logging.util.Log4jBannedVersion) ConfigurationInternal(org.gradle.api.internal.artifacts.configurations.ConfigurationInternal) ExtensionAware(org.gradle.api.plugins.ExtensionAware) Transformer(org.gradle.api.Transformer) ScalaRuntime(org.gradle.api.tasks.ScalaRuntime) CompileOptions(org.gradle.api.tasks.compile.CompileOptions) DefaultScalaPluginExtension(org.gradle.api.internal.tasks.scala.DefaultScalaPluginExtension) ImmutableSet(com.google.common.collect.ImmutableSet) Project(org.gradle.api.Project) SourceDirectorySet(org.gradle.api.file.SourceDirectorySet) ArtifactView(org.gradle.api.artifacts.ArtifactView) JvmEcosystemUtilities(org.gradle.api.plugins.jvm.internal.JvmEcosystemUtilities) AttributeDisambiguationRule(org.gradle.api.attributes.AttributeDisambiguationRule) Spec(org.gradle.api.specs.Spec) AttributeMatchingStrategy(org.gradle.api.attributes.AttributeMatchingStrategy) SerializableLambdas.spec(org.gradle.api.internal.lambdas.SerializableLambdas.spec) JavaPluginExtension(org.gradle.api.plugins.JavaPluginExtension) DeprecatableConfiguration(org.gradle.internal.deprecation.DeprecatableConfiguration) JavaToolchainService(org.gradle.jvm.toolchain.JavaToolchainService) Callable(java.util.concurrent.Callable) FileCollection(org.gradle.api.file.FileCollection) ComponentIdentifier(org.gradle.api.artifacts.component.ComponentIdentifier) SourceSet(org.gradle.api.tasks.SourceSet) Inject(javax.inject.Inject) Configuration(org.gradle.api.artifacts.Configuration) ScalaDoc(org.gradle.api.tasks.scala.ScalaDoc) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler) ScalaSourceDirectorySet(org.gradle.api.tasks.ScalaSourceDirectorySet) ScalaCompile(org.gradle.api.tasks.scala.ScalaCompile) Usage(org.gradle.api.attributes.Usage) InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) Action(org.gradle.api.Action) ReportingExtension(org.gradle.api.reporting.ReportingExtension) Jar(org.gradle.jvm.tasks.Jar) JvmPluginsHelper(org.gradle.api.plugins.internal.JvmPluginsHelper) JavaToolchainSpec(org.gradle.jvm.toolchain.JavaToolchainSpec) File(java.io.File) USAGE_ATTRIBUTE(org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE) ObjectFactory(org.gradle.api.model.ObjectFactory) Convention(org.gradle.api.plugins.Convention) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Plugin(org.gradle.api.Plugin) IncrementalCompileOptions(org.gradle.api.tasks.scala.IncrementalCompileOptions) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler) Usage(org.gradle.api.attributes.Usage) DeprecatableConfiguration(org.gradle.internal.deprecation.DeprecatableConfiguration) Configuration(org.gradle.api.artifacts.Configuration) InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) DeprecatableConfiguration(org.gradle.internal.deprecation.DeprecatableConfiguration) ConfigurationInternal(org.gradle.api.internal.artifacts.configurations.ConfigurationInternal)

Example 9 with InvalidUserCodeException

use of org.gradle.api.InvalidUserCodeException in project gradle by gradle.

the class InvalidPublicationChecker method validate.

public void validate() {
    if (variants.isEmpty()) {
        failWith("This publication must publish at least one variant");
    }
    checkVariantDependencyVersions();
    if (errors != null) {
        TreeFormatter formatter = new TreeFormatter();
        formatter.node("Invalid publication '" + publicationName + "'");
        formatter.startChildren();
        for (String error : errors) {
            formatter.node(error);
        }
        formatter.endChildren();
        if (explanations != null) {
            for (String explanation : explanations) {
                formatter.node(explanation);
            }
        }
        throw new InvalidUserCodeException(formatter.toString());
    }
}
Also used : InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) TreeFormatter(org.gradle.internal.logging.text.TreeFormatter)

Example 10 with InvalidUserCodeException

use of org.gradle.api.InvalidUserCodeException in project gradle by gradle.

the class DefaultComponentMetadataHandler method createSpecRuleActionForModule.

private SpecRuleAction<? super ComponentMetadataDetails> createSpecRuleActionForModule(Object id, RuleAction<? super ComponentMetadataDetails> ruleAction) {
    ModuleIdentifier moduleIdentifier;
    try {
        moduleIdentifier = moduleIdentifierNotationParser.parseNotation(id);
    } catch (UnsupportedNotationException e) {
        throw new InvalidUserCodeException(String.format(INVALID_SPEC_ERROR, id == null ? "null" : id.toString()), e);
    }
    Spec<ComponentMetadataDetails> spec = new ComponentMetadataDetailsMatchingSpec(moduleIdentifier);
    return new SpecRuleAction<>(ruleAction, spec);
}
Also used : InvalidUserCodeException(org.gradle.api.InvalidUserCodeException) SpecRuleAction(org.gradle.internal.rules.SpecRuleAction) UnsupportedNotationException(org.gradle.internal.typeconversion.UnsupportedNotationException) ModuleIdentifier(org.gradle.api.artifacts.ModuleIdentifier) ComponentMetadataDetails(org.gradle.api.artifacts.ComponentMetadataDetails)

Aggregations

InvalidUserCodeException (org.gradle.api.InvalidUserCodeException)12 UnsupportedNotationException (org.gradle.internal.typeconversion.UnsupportedNotationException)4 Action (org.gradle.api.Action)3 ModuleIdentifier (org.gradle.api.artifacts.ModuleIdentifier)3 File (java.io.File)2 Task (org.gradle.api.Task)2 ImmutableAttributes (org.gradle.api.internal.attributes.ImmutableAttributes)2 VariantVersionMappingStrategy (org.gradle.api.publish.VariantVersionMappingStrategy)2 AttributeMatcher (org.gradle.internal.component.model.AttributeMatcher)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1 BiFunction (java.util.function.BiFunction)1 Inject (javax.inject.Inject)1 InvokerHelper (org.codehaus.groovy.runtime.InvokerHelper)1 Plugin (org.gradle.api.Plugin)1 Project (org.gradle.api.Project)1 Transformer (org.gradle.api.Transformer)1 ArtifactView (org.gradle.api.artifacts.ArtifactView)1