Search in sources :

Example 1 with ResolverOptions

use of org.apache.sling.provisioning.model.ModelUtility.ResolverOptions in project sling by apache.

the class ModelPreprocessor method addDependencies.

/**
     * Add dependencies for a single project.
     * @param env The environment with all maven settings and projects
     * @param info The project to process.
     * @throws MavenExecutionException If anything goes wrong.
     */
private Model addDependencies(final Environment env, final ProjectInfo info) throws MavenExecutionException {
    if (info.done == true) {
        env.logger.debug("Return prepared model for " + info.project);
        return info.model;
    }
    // prevent recursion and multiple processing
    info.done = true;
    env.logger.debug("Processing project " + info.project);
    // read local model
    final String pattern = nodeValue(info.plugin, "modelPattern", AbstractSlingStartMojo.DEFAULT_MODEL_PATTERN);
    final String inlinedModel = nodeValue(info.plugin, "model", null);
    String scope = Artifact.SCOPE_PROVIDED;
    try {
        if (hasNodeValue(info.plugin, "modelDirectory")) {
            final String directory = nodeValue(info.plugin, "modelDirectory", null);
            info.localModel = readLocalModel(info.project, inlinedModel, new File(directory), pattern, env.logger);
        } else {
            // use multiple fallbacks here only in case the default model directory is not explicitly set
            File defaultModelDirectory = new File(info.project.getBasedir(), "src/main/provisioning");
            if (defaultModelDirectory.exists()) {
                env.logger.debug("Try to extract model from default provisioning directory " + defaultModelDirectory.getAbsolutePath());
                info.localModel = readLocalModel(info.project, inlinedModel, defaultModelDirectory, pattern, env.logger);
            } else {
                File defaultModelDirectoryInTest = new File(info.project.getBasedir(), "src/test/provisioning");
                env.logger.debug("Try to extract model from default test provisioning directory " + defaultModelDirectoryInTest.getAbsolutePath());
                info.localModel = readLocalModel(info.project, inlinedModel, defaultModelDirectoryInTest, pattern, env.logger);
                scope = Artifact.SCOPE_TEST;
            }
        }
    } catch (final IOException ioe) {
        throw new MavenExecutionException(ioe.getMessage(), ioe);
    }
    // process attachments
    processAttachments(env, info);
    // is the maven classpath supposed to be extended?
    info.extendMavenClassPath = !nodeBooleanValue(info.plugin, AbstractSlingStartMojo.CONFIGURATION_NAME_DISABLE_EXTENDING_CLASSPATH, false);
    // check for setting version
    if (nodeBooleanValue(info.plugin, "setFeatureVersions", false)) {
        for (final Feature f : info.localModel.getFeatures()) {
            if (f.getVersion() == null) {
                f.setVersion(cleanupVersion(info.project.getVersion()));
            }
        }
    }
    // prepare resolver options
    ResolverOptions resolverOptions = new ResolverOptions();
    if (nodeBooleanValue(info.plugin, "usePomVariables", false)) {
        resolverOptions.variableResolver(new PomVariableResolver(info.project));
    }
    if (nodeBooleanValue(info.plugin, "usePomDependencies", false)) {
        resolverOptions.artifactVersionResolver(new PomArtifactVersionResolver(info.project, nodeBooleanValue(info.plugin, "allowUnresolvedPomDependencies", false)));
    }
    final Model copyModel = new Model();
    this.mergeModels(copyModel, info.localModel);
    // we have to create an effective model to add the dependencies
    final Model effectiveModel = ModelUtility.getEffectiveModel(copyModel, resolverOptions);
    final List<Model> dependencies = searchSlingstartDependencies(env, info, copyModel, effectiveModel);
    info.model = new Model();
    for (final Model d : dependencies) {
        this.mergeModels(info.model, d);
    }
    this.mergeModels(info.model, copyModel);
    info.model = ModelUtility.getEffectiveModel(info.model, resolverOptions);
    final Map<Traceable, String> errors = ModelUtility.validate(info.model);
    if (errors != null) {
        throw new MavenExecutionException("Unable to create model file for " + info.project + " : " + errors, (File) null);
    }
    if (info.extendMavenClassPath) {
        addDependenciesFromModel(env, info, scope);
        env.logger.info("Extended Maven classpath (scope '" + scope + "') by the dependencies extracted from the Sling model.");
    } else {
        env.logger.debug("Do not enrich Maven classpath with Sling model!");
    }
    try {
        ProjectHelper.storeProjectInfo(info);
    } catch (final IOException ioe) {
        throw new MavenExecutionException(ioe.getMessage(), ioe);
    }
    return info.model;
}
Also used : MavenExecutionException(org.apache.maven.MavenExecutionException) Model(org.apache.sling.provisioning.model.Model) IOException(java.io.IOException) Traceable(org.apache.sling.provisioning.model.Traceable) File(java.io.File) Feature(org.apache.sling.provisioning.model.Feature) ResolverOptions(org.apache.sling.provisioning.model.ModelUtility.ResolverOptions)

Example 2 with ResolverOptions

use of org.apache.sling.provisioning.model.ModelUtility.ResolverOptions in project sling by apache.

the class ModelPreprocessor method processSlingstartDependencies.

private Model processSlingstartDependencies(final Environment env, final ProjectInfo info, final Dependency dep, final Model rawModel) throws MavenExecutionException {
    env.logger.debug("Processing dependency " + dep);
    // we have to create an effective model to add the dependencies
    final Model effectiveModel = ModelUtility.getEffectiveModel(rawModel, new ResolverOptions());
    final List<Model> dependencies = searchSlingstartDependencies(env, info, rawModel, effectiveModel);
    Model mergingModel = new Model();
    for (final Model d : dependencies) {
        this.mergeModels(mergingModel, d);
    }
    this.mergeModels(mergingModel, rawModel);
    final Map<Traceable, String> errors = ModelUtility.validate(ModelUtility.getEffectiveModel(mergingModel, new ResolverOptions()));
    if (errors != null) {
        throw new MavenExecutionException("Unable to create model file for " + dep + " : " + errors, (File) null);
    }
    return mergingModel;
}
Also used : MavenExecutionException(org.apache.maven.MavenExecutionException) Model(org.apache.sling.provisioning.model.Model) Traceable(org.apache.sling.provisioning.model.Traceable) ResolverOptions(org.apache.sling.provisioning.model.ModelUtility.ResolverOptions)

Example 3 with ResolverOptions

use of org.apache.sling.provisioning.model.ModelUtility.ResolverOptions in project sling by apache.

the class CustomResolverTest method testCustomVariableResolverNoFilter.

@Test
public void testCustomVariableResolverNoFilter() throws Exception {
    final Model m = U.readCompleteTestModel();
    final VariableResolver r = new CustomVariableResolver();
    final Model effective = ModelUtility.getEffectiveModel(m, new ResolverOptions().variableResolver(r));
    final ArtifactGroup g = U.getGroup(effective, "example", "jackrabbit", 15);
    U.assertArtifact(g, "mvn:org.apache.sling/org.apache.sling.jcr.jackrabbit.server/#example#jackrabbit.version#/jar");
}
Also used : VariableResolver(org.apache.sling.provisioning.model.ModelUtility.VariableResolver) ResolverOptions(org.apache.sling.provisioning.model.ModelUtility.ResolverOptions) Test(org.junit.Test)

Example 4 with ResolverOptions

use of org.apache.sling.provisioning.model.ModelUtility.ResolverOptions in project sling by apache.

the class CustomResolverTest method testCustomArtifactVersionResolver.

@Test
public void testCustomArtifactVersionResolver() throws Exception {
    final Model m = U.readCompleteTestModel();
    final ArtifactVersionResolver r = new CustomArtifactVersionResolver();
    final Model effective = ModelUtility.getEffectiveModel(m, new ResolverOptions().artifactVersionResolver(r));
    final ArtifactGroup g = U.getGroup(effective, "example", null, 0);
    U.assertArtifact(g, "mvn:org.example/jar-without-version/9.9.9");
}
Also used : ResolverOptions(org.apache.sling.provisioning.model.ModelUtility.ResolverOptions) ArtifactVersionResolver(org.apache.sling.provisioning.model.ModelUtility.ArtifactVersionResolver) Test(org.junit.Test)

Aggregations

ResolverOptions (org.apache.sling.provisioning.model.ModelUtility.ResolverOptions)4 MavenExecutionException (org.apache.maven.MavenExecutionException)2 Model (org.apache.sling.provisioning.model.Model)2 Traceable (org.apache.sling.provisioning.model.Traceable)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 Feature (org.apache.sling.provisioning.model.Feature)1 ArtifactVersionResolver (org.apache.sling.provisioning.model.ModelUtility.ArtifactVersionResolver)1 VariableResolver (org.apache.sling.provisioning.model.ModelUtility.VariableResolver)1