Example usage for org.apache.maven.model.io DefaultModelWriter DefaultModelWriter

List of usage examples for org.apache.maven.model.io DefaultModelWriter DefaultModelWriter

Introduction

In this page you can find the example usage for org.apache.maven.model.io DefaultModelWriter DefaultModelWriter.

Prototype

DefaultModelWriter

Source Link

Usage

From source file:com.francetelecom.clara.cloud.mvn.consumer.maven.PomGenerator.java

License:Apache License

/**
 * Generate a pom.xml file from a MavenProject description
 * @return/*from   w  w w  .ja v  a2 s.  com*/
 */
private String modelToStringXml(Model model) {

    DefaultModelWriter modelWriter = new DefaultModelWriter();

    StringWriter output = new StringWriter();
    String result = "";
    try {
        modelWriter.write(output, null, model);
        result = output.getBuffer().toString();
    } catch (IOException e) {
        logger.error("Cannot convert model to pom: " + e.getMessage());
        throw new TechnicalException("Cannot convert model to pom", e);
    }
    return result;
}

From source file:org.eclipse.scada.build.helper.ChangeManager.java

License:Open Source License

public ChangeManager(final Log log) {
    this.log = log;
    this.writer = new DefaultModelWriter();
    this.reader = new DefaultModelReader();
}

From source file:org.sonatype.maven.polyglot.groovy.GroovyModelReader.java

License:Open Source License

@Override
public Model read(final Reader input, final Map<String, ?> options) throws IOException {
    assert input != null;

    Model model;//from  w  w w. j a  v  a  2s  .  c o m

    try {
        model = doRead(input, options);
    } catch (Throwable t) {
        t = StackTraceUtils.sanitize(t);

        if (t instanceof IOException) {
            throw (IOException) t;
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        if (t instanceof Error) {
            throw (Error) t;
        }

        throw new RuntimeException(t);
    }

    // FIXME: Looks like there are cases where the model is loaded more than once
    executeManager.install(model, options);

    if (log.isDebugEnabled()) {
        DefaultModelWriter writer = new DefaultModelWriter();
        StringWriter buff = new StringWriter();
        writer.write(buff, null, model);
        log.debug("Read groovy model: \n" + buff);
    }

    return model;
}

From source file:org.sonatype.maven.polyglot.groovy.GroovyModelWriter.java

License:Open Source License

@Override
public void write(final Writer output, final Map<String, Object> options, final Model model)
        throws IOException {
    assert output != null;
    assert model != null;

    StringWriter buff = new StringWriter();
    DefaultModelWriter writer = new DefaultModelWriter();
    writer.write(buff, options, model);//  ww w.j  a v a 2  s .  co m

    Dom2Groovy converter = new Dom2Groovy(new IndentPrinter(new PrintWriter(output), "  "));

    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(buff.toString())));

        Element root = doc.getDocumentElement();
        NamedNodeMap attrs = root.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Attr attr = (Attr) attrs.item(i);
            root.removeAttribute(attr.getName());
        }
        // Not sure where this comes from but the above will not nuke it
        root.removeAttribute("xmlns:xsi");

        converter.print(doc);
        output.flush();
    } catch (ParserConfigurationException e) {
        throw (IOException) new IOException().initCause(e);
    } catch (SAXException e) {
        throw (IOException) new IOException().initCause(e);
    }
}

From source file:org.sourcepit.b2.directory.parser.internal.module.ModelBuilderTestHarness.java

License:Apache License

public static BasicModule initModuleDir(final File moduleDir, String groupId, String artifactId,
        String mavenVersion) throws IOException {
    Model model = new Model();
    model.setVersion("4.0.0");
    model.setGroupId(groupId);/*from w  w w  . j a  v  a  2  s  .  c  om*/
    model.setArtifactId(artifactId);
    model.setVersion(mavenVersion);

    new DefaultModelWriter().write(new File(moduleDir, "module.xml"), null, model);

    final ModuleModelFactory eFactory = ModuleModelFactory.eINSTANCE;
    final BasicModule module = eFactory.createBasicModule();
    module.setId(artifactId);
    module.setVersion(mavenVersion.replaceAll("-SNAPSHOT", ".qualifier"));
    module.setLayoutId("structured");
    module.setDirectory(moduleDir);

    return module;
}

From source file:org.sourcepit.b2.internal.generator.AbstractPomGenerator.java

License:Apache License

protected void writeMavenModel(File file, Model model) {
    try {//from w w w. j a v a 2s.  c  om
        new DefaultModelWriter().write(file, null, model);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.sourcepit.b2.internal.generator.BootPomSerializer.java

License:Apache License

private void writeMavenModel(final Model model, final File pomFile) throws IOException {
    new DefaultModelWriter().write(pomFile, null, model);
}

From source file:org.sourcepit.b2.internal.maven.MavenB2LifecycleParticipant.java

License:Apache License

private void processAttachments(MavenProject wrapperProject, File pomFile) {
    final List<Artifact> attachedArtifacts = wrapperProject.getAttachedArtifacts();
    if (attachedArtifacts == null) {
        return;//  w w w. ja v a  2s .  c  o  m
    }

    Xpp3Dom artifactsNode = new Xpp3Dom("artifacts");
    for (Artifact artifact : attachedArtifacts) {
        Xpp3Dom artifactNode = new Xpp3Dom("artifact");

        if (artifact.getClassifier() != null) {
            Xpp3Dom classifierNode = new Xpp3Dom("classifier");
            classifierNode.setValue(artifact.getClassifier());
            artifactNode.addChild(classifierNode);
        }

        Xpp3Dom typeNode = new Xpp3Dom("type");
        typeNode.setValue(artifact.getType());
        artifactNode.addChild(typeNode);

        Xpp3Dom fileNode = new Xpp3Dom("file");
        fileNode.setValue(artifact.getFile().getAbsolutePath());
        artifactNode.addChild(fileNode);

        artifactsNode.addChild(artifactNode);
    }

    Xpp3Dom configNode = new Xpp3Dom("configuration");
    configNode.addChild(artifactsNode);

    PluginExecution exec = new PluginExecution();
    exec.setId("b2-attach-artifatcs");
    exec.setPhase("initialize");
    exec.getGoals().add("attach-artifact");
    exec.setConfiguration(configNode);

    Plugin plugin = new Plugin();
    plugin.setGroupId("org.codehaus.mojo");
    plugin.setArtifactId("build-helper-maven-plugin");
    plugin.setVersion("1.7");
    plugin.getExecutions().add(exec);
    plugin.setInherited(false);

    Build build = new Build();
    build.getPlugins().add(plugin);

    Model model = new Model();
    model.setBuild(build);

    final Model moduleModel;
    try {
        moduleModel = new DefaultModelReader().read(pomFile, null);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    new ModelTemplateMerger().merge(moduleModel, model, false, null);
    try {
        new DefaultModelWriter().write(pomFile, null, moduleModel);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.sourcepit.common.maven.testing.ArtifactRepositoryFacade.java

License:Apache License

private void deploy(final File source, Model pom, boolean install, boolean deploy) {
    final ArtifactRepository localRepository = embeddedMaven.getLocalRepository();

    final File localRepo = new File(localRepository.getBasedir());

    final MavenExecutionResult2 result = embeddedMaven.buildStubProject(localRepo);

    final MavenSession session = legacySupport.getSession();
    try {//  w  w w. ja  v  a2 s .  c om
        final ArtifactInstaller installer = install ? plexus.lookup(ArtifactInstaller.class) : null;
        final ArtifactDeployer deployer = deploy ? plexus.lookup(ArtifactDeployer.class) : null;

        legacySupport.setSession(result.getSession());

        final ArtifactRepository deploymentRepository = embeddedMaven.getRemoteRepository();

        final Artifact artifact = embeddedMaven.createArtifact(pom);
        if (install) {
            installer.install(source, artifact, localRepository);
        }
        if (deploy) {
            deployer.deploy(source, artifact, deploymentRepository, localRepository);
        }

        if (!"pom".equals(pom.getPackaging())) {
            final Artifact pomArtifact = embeddedMaven.createProjectArtifact(pom);
            final File pomFile = File.createTempFile(pom.getArtifactId() + "-" + pom.getVersion(), ".pom",
                    localRepo);
            try {
                new DefaultModelWriter().write(pomFile, null, pom);
                if (install) {
                    installer.install(pomFile, pomArtifact, localRepository);
                }
                if (deploy) {
                    deployer.deploy(pomFile, pomArtifact, deploymentRepository, localRepository);
                }
            } finally {
                FileUtils.forceDelete(pomFile);
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    } finally {
        legacySupport.setSession(session);
    }
}

From source file:org.sourcepit.common.maven.testing.EmbeddedMaven.java

License:Apache License

public MavenExecutionResult2 buildStubProject(File projectDir) {
    final Model pom = new Model();
    pom.setModelVersion("4.0.0");
    pom.setGroupId("org.sourcepit");
    pom.setArtifactId("stub-project");
    pom.setVersion("1");

    try {//from   w w w .jav a2s.  c  o m
        final File projectPom = File.createTempFile("pom", ".xml", projectDir);
        try {
            new DefaultModelWriter().write(projectPom, null, pom);
            return buildProject(projectPom);
        } finally {
            FileUtils.forceDelete(projectPom);
        }
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}