Search in sources :

Example 11 with Feature

use of org.apache.sling.provisioning.model.Feature in project sling by apache.

the class ModelPreprocessor method searchSlingstartDependencies.

/**
     * Search for dependent slingstart/slingfeature artifacts and remove them from the effective model.
     * @throws MavenExecutionException
     */
private List<Model> searchSlingstartDependencies(final Environment env, final ProjectInfo info, final Model rawModel, final Model effectiveModel) throws MavenExecutionException {
    // slingstart or slingfeature
    final List<Model> dependencies = new ArrayList<>();
    for (final Feature feature : effectiveModel.getFeatures()) {
        for (final RunMode runMode : feature.getRunModes()) {
            for (final ArtifactGroup group : runMode.getArtifactGroups()) {
                final List<org.apache.sling.provisioning.model.Artifact> removeList = new ArrayList<>();
                for (final org.apache.sling.provisioning.model.Artifact a : group) {
                    if (a.getType().equals(BuildConstants.PACKAGING_SLINGSTART) || a.getType().equals(BuildConstants.PACKAGING_PARTIAL_SYSTEM)) {
                        final Dependency dep = new Dependency();
                        dep.setGroupId(a.getGroupId());
                        dep.setArtifactId(a.getArtifactId());
                        dep.setVersion(a.getVersion());
                        dep.setType(BuildConstants.PACKAGING_PARTIAL_SYSTEM);
                        if (a.getType().equals(BuildConstants.PACKAGING_SLINGSTART)) {
                            dep.setClassifier(BuildConstants.PACKAGING_PARTIAL_SYSTEM);
                        } else {
                            dep.setClassifier(a.getClassifier());
                        }
                        dep.setScope(Artifact.SCOPE_PROVIDED);
                        env.logger.debug("- adding dependency " + ModelUtils.toString(dep));
                        info.project.getDependencies().add(dep);
                        // if it's a project from the current reactor build, we can't resolve it right now
                        final String key = a.getGroupId() + ":" + a.getArtifactId();
                        final ProjectInfo depInfo = env.modelProjects.get(key);
                        if (depInfo != null) {
                            env.logger.debug("Found reactor " + a.getType() + " dependency : " + a);
                            final Model model = addDependencies(env, depInfo);
                            if (model == null) {
                                throw new MavenExecutionException("Recursive model dependency list including project " + info.project, (File) null);
                            }
                            dependencies.add(model);
                            info.includedModels.put(a, depInfo.localModel);
                        } else {
                            env.logger.debug("Found external " + a.getType() + " dependency: " + a);
                            // "external" dependency, we can already resolve it
                            final File modelFile = resolveSlingstartArtifact(env, info.project, dep);
                            FileReader r = null;
                            try {
                                r = new FileReader(modelFile);
                                final Model model = ModelReader.read(r, modelFile.getAbsolutePath());
                                info.includedModels.put(a, model);
                                final Map<Traceable, String> errors = ModelUtility.validate(model);
                                if (errors != null) {
                                    throw new MavenExecutionException("Unable to read model file from " + modelFile + " : " + errors, modelFile);
                                }
                                final Model fullModel = processSlingstartDependencies(env, info, dep, model);
                                dependencies.add(fullModel);
                            } catch (final IOException ioe) {
                                throw new MavenExecutionException("Unable to read model file from " + modelFile, ioe);
                            } finally {
                                try {
                                    if (r != null) {
                                        r.close();
                                    }
                                } catch (final IOException io) {
                                // ignore
                                }
                            }
                        }
                        removeList.add(a);
                    }
                }
                for (final org.apache.sling.provisioning.model.Artifact r : removeList) {
                    group.remove(r);
                    final Feature localModelFeature = rawModel.getFeature(feature.getName());
                    if (localModelFeature != null) {
                        final RunMode localRunMode = localModelFeature.getRunMode(runMode.getNames());
                        if (localRunMode != null) {
                            final ArtifactGroup localAG = localRunMode.getArtifactGroup(group.getStartLevel());
                            if (localAG != null) {
                                localAG.remove(r);
                            }
                        }
                    }
                }
            }
        }
    }
    return dependencies;
}
Also used : ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) MavenExecutionException(org.apache.maven.MavenExecutionException) RunMode(org.apache.sling.provisioning.model.RunMode) Model(org.apache.sling.provisioning.model.Model) FileReader(java.io.FileReader) Traceable(org.apache.sling.provisioning.model.Traceable) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) File(java.io.File)

Example 12 with Feature

use of org.apache.sling.provisioning.model.Feature 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 13 with Feature

use of org.apache.sling.provisioning.model.Feature in project sling by apache.

the class PreparePackageMojo method buildBootstrapFile.

/**
     * Build the bootstrap file for the given packaging run mode
     */
private void buildBootstrapFile(final Model model, final String packageRunMode, final File outputDir) throws MojoExecutionException {
    final StringBuilder sb = new StringBuilder();
    final Feature launchpadFeature = model.getFeature(ModelConstants.FEATURE_LAUNCHPAD);
    if (launchpadFeature != null) {
        final RunMode launchpadRunMode = launchpadFeature.getRunMode();
        if (launchpadRunMode != null) {
            final Configuration c = launchpadRunMode.getConfiguration(ModelConstants.CFG_LAUNCHPAD_BOOTSTRAP);
            if (c != null) {
                sb.append(c.getProperties().get(c.getPid()));
                sb.append('\n');
            }
        }
        final RunMode packageRM = launchpadFeature.getRunMode(new String[] { packageRunMode });
        if (packageRM != null) {
            final Configuration c = packageRM.getConfiguration(ModelConstants.CFG_LAUNCHPAD_BOOTSTRAP);
            if (c != null) {
                sb.append(c.getProperties().get(c.getPid()));
                sb.append('\n');
            }
        }
    }
    if (sb.length() > 0) {
        final File file = new File(outputDir, BOOTSTRAP_FILE);
        getLog().debug(String.format("Creating bootstrap file at %s", file.getPath()));
        try {
            FileUtils.fileWrite(file, sb.toString());
        } catch (final IOException ioe) {
            throw new MojoExecutionException("Unable to write bootstrap file.", ioe);
        }
    }
}
Also used : Configuration(org.apache.sling.provisioning.model.Configuration) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RunMode(org.apache.sling.provisioning.model.RunMode) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) File(java.io.File)

Example 14 with Feature

use of org.apache.sling.provisioning.model.Feature in project sling by apache.

the class PreparePackageMojo method buildSettings.

/**
     * Build the settings for the given packaging run mode
     */
private void buildSettings(final Model model, final String packageRunMode, final File outputDir) throws MojoExecutionException {
    final Properties settings = new Properties();
    final Feature launchpadFeature = model.getFeature(ModelConstants.FEATURE_LAUNCHPAD);
    if (launchpadFeature != null) {
        final RunMode launchpadRunMode = launchpadFeature.getRunMode();
        if (launchpadRunMode != null) {
            for (final Map.Entry<String, String> entry : launchpadRunMode.getSettings()) {
                settings.put(entry.getKey(), deescapeVariablePlaceholders(entry.getValue()));
            }
        }
    }
    final Feature bootFeature = model.getFeature(ModelConstants.FEATURE_BOOT);
    if (bootFeature != null) {
        final RunMode bootRunMode = bootFeature.getRunMode();
        if (bootRunMode != null) {
            for (final Map.Entry<String, String> entry : bootRunMode.getSettings()) {
                settings.put(entry.getKey(), deescapeVariablePlaceholders(entry.getValue()));
            }
        }
    }
    for (final Feature f : model.getFeatures()) {
        final RunMode packageRM = f.getRunMode(new String[] { packageRunMode });
        if (packageRM != null) {
            for (final Map.Entry<String, String> entry : packageRM.getSettings()) {
                settings.put(entry.getKey(), deescapeVariablePlaceholders(entry.getValue()));
            }
        }
    }
    if (settings.size() > 0) {
        final File settingsFile = new File(outputDir, PROPERTIES_FILE);
        getLog().debug(String.format("Creating settings at %s", settingsFile.getPath()));
        FileWriter writer = null;
        try {
            writer = new FileWriter(settingsFile);
            settings.store(writer, null);
        } catch (final IOException ioe) {
            throw new MojoExecutionException("Unable to write properties file.", ioe);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RunMode(org.apache.sling.provisioning.model.RunMode) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Properties(java.util.Properties) Feature(org.apache.sling.provisioning.model.Feature) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 15 with Feature

use of org.apache.sling.provisioning.model.Feature in project sling by apache.

the class RepositoryMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    final File artifactDir = new File(this.project.getBuild().getDirectory(), DIR_NAME);
    this.getLog().info("Creating repository in '" + artifactDir.getPath() + "'...");
    // artifacts
    final Model model = ProjectHelper.getEffectiveModel(this.project, getResolverOptions());
    for (final Feature feature : model.getFeatures()) {
        for (final RunMode runMode : feature.getRunModes()) {
            for (final ArtifactGroup group : runMode.getArtifactGroups()) {
                for (final org.apache.sling.provisioning.model.Artifact artifact : group) {
                    copyArtifactToRepository(artifact, artifactDir);
                }
            }
        }
    }
    // base artifact - only if launchpad feature is available
    if (model.getFeature(ModelConstants.FEATURE_LAUNCHPAD) != null) {
        try {
            final org.apache.sling.provisioning.model.Artifact baseArtifact = ModelUtils.findBaseArtifact(model);
            final org.apache.sling.provisioning.model.Artifact appArtifact = new org.apache.sling.provisioning.model.Artifact(baseArtifact.getGroupId(), baseArtifact.getArtifactId(), baseArtifact.getVersion(), BuildConstants.CLASSIFIER_APP, BuildConstants.TYPE_JAR);
            copyArtifactToRepository(appArtifact, artifactDir);
        } catch (final MavenExecutionException mee) {
            throw new MojoExecutionException(mee.getMessage(), mee.getCause());
        }
    }
    // models
    Model rawModel = ProjectHelper.getRawModel(this.project);
    if (usePomVariables) {
        rawModel = ModelUtility.applyVariables(rawModel, new PomVariableResolver(project));
    }
    if (usePomDependencies) {
        rawModel = ModelUtility.applyArtifactVersions(rawModel, new PomArtifactVersionResolver(project, allowUnresolvedPomDependencies));
    }
    final String classifier = (project.getPackaging().equals(BuildConstants.PACKAGING_PARTIAL_SYSTEM) ? null : BuildConstants.PACKAGING_PARTIAL_SYSTEM);
    final org.apache.sling.provisioning.model.Artifact rawModelArtifact = new org.apache.sling.provisioning.model.Artifact(this.project.getGroupId(), this.project.getArtifactId(), this.project.getVersion(), classifier, BuildConstants.TYPE_TXT);
    final File rawModelFile = getRepositoryFile(artifactDir, rawModelArtifact);
    Writer writer = null;
    try {
        writer = new FileWriter(rawModelFile);
        ModelWriter.write(writer, rawModel);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to write model to " + rawModelFile, e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
    // and write model to target
    writer = null;
    try {
        writer = new FileWriter(new File(this.project.getBuild().getDirectory(), repositoryModelName));
        ModelWriter.write(writer, rawModel);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to write model to " + rawModelFile, e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
    for (final Map.Entry<String, String> entry : ProjectHelper.getDependencyModel(this.project).entrySet()) {
        final org.apache.sling.provisioning.model.Artifact modelDepArtifact = org.apache.sling.provisioning.model.Artifact.fromMvnUrl(entry.getKey());
        final String modelClassifier = (modelDepArtifact.getType().equals(BuildConstants.PACKAGING_SLINGSTART) ? BuildConstants.PACKAGING_PARTIAL_SYSTEM : modelDepArtifact.getClassifier());
        final org.apache.sling.provisioning.model.Artifact modelArtifact = new org.apache.sling.provisioning.model.Artifact(modelDepArtifact.getGroupId(), modelDepArtifact.getArtifactId(), modelDepArtifact.getVersion(), modelClassifier, BuildConstants.TYPE_TXT);
        final File modelFile = getRepositoryFile(artifactDir, modelArtifact);
        Writer modelWriter = null;
        try {
            modelWriter = new FileWriter(modelFile);
            modelWriter.write(entry.getValue());
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to write model to " + modelFile, e);
        } finally {
            IOUtils.closeQuietly(modelWriter);
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Feature(org.apache.sling.provisioning.model.Feature) Artifact(org.apache.maven.artifact.Artifact) MavenExecutionException(org.apache.maven.MavenExecutionException) RunMode(org.apache.sling.provisioning.model.RunMode) Model(org.apache.sling.provisioning.model.Model) File(java.io.File) ArtifactGroup(org.apache.sling.provisioning.model.ArtifactGroup) Map(java.util.Map) FileWriter(java.io.FileWriter) ModelWriter(org.apache.sling.provisioning.model.io.ModelWriter) Writer(java.io.Writer)

Aggregations

Feature (org.apache.sling.provisioning.model.Feature)19 RunMode (org.apache.sling.provisioning.model.RunMode)15 File (java.io.File)9 IOException (java.io.IOException)9 ArtifactGroup (org.apache.sling.provisioning.model.ArtifactGroup)9 Model (org.apache.sling.provisioning.model.Model)7 Artifact (org.apache.sling.provisioning.model.Artifact)6 Traceable (org.apache.sling.provisioning.model.Traceable)6 HashMap (java.util.HashMap)5 Configuration (org.apache.sling.provisioning.model.Configuration)5 Map (java.util.Map)4 MavenExecutionException (org.apache.maven.MavenExecutionException)4 Artifact (org.apache.maven.artifact.Artifact)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 InputStream (java.io.InputStream)3 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 Section (org.apache.sling.provisioning.model.Section)3 FileWriter (java.io.FileWriter)2 Reader (java.io.Reader)2