Search in sources :

Example 16 with Model

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

the class ProjectHelper method getRawModel.

/**
     * Get the raw model from the project
     * @param project The maven projet
     * @return The raw local model
     * @throws MojoExecutionException If reading fails
     */
public static Model getRawModel(final MavenProject project) throws MojoExecutionException {
    Model result = (Model) project.getContextValue(RAW_MODEL_CACHE);
    if (result == null) {
        try {
            final String text = (String) project.getContextValue(RAW_MODEL_TXT);
            if (text == null) {
                throw new MojoExecutionException("No provisioning model found in project.");
            }
            final StringReader r = new StringReader(text);
            result = ModelReader.read(r, project.getId());
            project.setContextValue(RAW_MODEL_CACHE, result);
        } catch (final IOException ioe) {
            throw new MojoExecutionException(ioe.getMessage(), ioe);
        }
    }
    return result;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Model(org.apache.sling.provisioning.model.Model) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 17 with Model

use of org.apache.sling.provisioning.model.Model 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)

Example 18 with Model

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

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

the class PreparePackageMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    final Model model = ProjectHelper.getEffectiveModel(this.project, getResolverOptions());
    execute(model);
}
Also used : Model(org.apache.sling.provisioning.model.Model)

Example 20 with Model

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

the class RepoinitTextProvider method extractFromModel.

private String extractFromModel(String sourceInfo, String rawText, String modelSection) throws IOException {
    final StringReader reader = new StringReader(rawText);
    final Model model = ModelReader.read(reader, sourceInfo);
    final StringBuilder sb = new StringBuilder();
    if (modelSection == null) {
        throw new IllegalStateException("Model section name is null, cannot read model");
    }
    for (final Feature feature : model.getFeatures()) {
        for (final Section section : feature.getAdditionalSections(modelSection)) {
            sb.append("# ").append(modelSection).append(" from ").append(feature.getName()).append("\n");
            sb.append("# ").append(section.getComment()).append("\n");
            sb.append(section.getContents()).append("\n");
        }
    }
    return sb.toString();
}
Also used : StringReader(java.io.StringReader) Model(org.apache.sling.provisioning.model.Model) Feature(org.apache.sling.provisioning.model.Feature) Section(org.apache.sling.provisioning.model.Section)

Aggregations

Model (org.apache.sling.provisioning.model.Model)23 IOException (java.io.IOException)12 File (java.io.File)11 StringReader (java.io.StringReader)8 Feature (org.apache.sling.provisioning.model.Feature)7 Traceable (org.apache.sling.provisioning.model.Traceable)7 Artifact (org.apache.sling.provisioning.model.Artifact)6 MavenExecutionException (org.apache.maven.MavenExecutionException)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)5 Test (org.junit.Test)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 ModelReader (org.apache.sling.provisioning.model.io.ModelReader)4 FileInputStream (java.io.FileInputStream)3 Reader (java.io.Reader)3 StringWriter (java.io.StringWriter)3 Map (java.util.Map)3 Manifest (java.util.jar.Manifest)3 FileOutputStream (java.io.FileOutputStream)2 FileReader (java.io.FileReader)2