Search in sources :

Example 16 with MavenXpp3Reader

use of org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader in project ceylon by eclipse.

the class AetherResolverImpl method getDependencies.

@Override
public DependencyDescriptor getDependencies(InputStream pomXml, String name, String version) throws IOException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    try {
        model = reader.read(pomXml);
    } catch (XmlPullParserException e) {
        throw new IOException(e);
    }
    return new ModelDependencyDescriptor(model);
}
Also used : Model(org.eclipse.ceylon.aether.apache.maven.model.Model) MavenXpp3Reader(org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.eclipse.ceylon.aether.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Example 17 with MavenXpp3Reader

use of org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader in project mule by mulesoft.

the class MavenUtils method getPomModelFolder.

/**
 * Returns the {@link Model} from a given artifact folder
 *
 * @param artifactFolder folder containing the exploded artifact file.
 * @return the {@link Model} from the {@value ArtifactPluginDescriptor#MULE_PLUGIN_POM} file if available
 * @throws ArtifactDescriptorCreateException if the folder does not contain a {@value ArtifactPluginDescriptor#MULE_PLUGIN_POM}
 *         file or the file can' be loaded
 */
public static Model getPomModelFolder(File artifactFolder) {
    final File mulePluginPom = lookupPomFromMavenLocation(artifactFolder);
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    try (FileReader mulePluginPomFileReader = new FileReader(mulePluginPom)) {
        model = reader.read(mulePluginPomFileReader);
    } catch (IOException | XmlPullParserException e) {
        throw new ArtifactDescriptorCreateException(format("There was an issue reading '%s' for the plugin '%s'", mulePluginPom.getName(), artifactFolder.getAbsolutePath()), e);
    }
    return model;
}
Also used : Model(org.apache.maven.model.Model) ArtifactDescriptorCreateException(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptorCreateException) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileUtils.createFile(org.mule.runtime.core.api.util.FileUtils.createFile) File(java.io.File)

Example 18 with MavenXpp3Reader

use of org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader in project spoon by INRIA.

the class MavenLauncher method readPOM.

/**
 * Extract the information from the pom
 * @param path the path to the pom
 * @param parent the parent pom
 * @return the extracted model
 * @throws IOException when the file does not exist
 * @throws XmlPullParserException when the file is corrupted
 */
private InheritanceModel readPOM(String path, InheritanceModel parent) throws IOException, XmlPullParserException {
    if (!path.endsWith(".xml") && !path.endsWith(".pom")) {
        path = Paths.get(path, "pom.xml").toString();
    }
    File pomFile = new File(path);
    if (!pomFile.exists()) {
        return null;
    }
    MavenXpp3Reader pomReader = new MavenXpp3Reader();
    try (FileReader reader = new FileReader(pomFile)) {
        Model model = pomReader.read(reader);
        InheritanceModel inheritanceModel = new InheritanceModel(model, parent, pomFile.getParentFile());
        for (String module : model.getModules()) {
            inheritanceModel.addModule(readPOM(Paths.get(pomFile.getParent(), module).toString(), inheritanceModel));
        }
        return inheritanceModel;
    }
}
Also used : Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) File(java.io.File)

Example 19 with MavenXpp3Reader

use of org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader in project maven-archetype by apache.

the class DefaultOldArchetype method processTemplates.

private void processTemplates(File pomFile, String outputDirectory, Context context, ArchetypeDescriptor descriptor, String packageName, Model parentModel) throws ArchetypeTemplateProcessingException {
    if (!pomFile.exists()) {
        processTemplate(outputDirectory, context, ARCHETYPE_POM, new TemplateDescriptor(), false, null);
    }
    // ---------------------------------------------------------------------
    // Model generated for the new archetype, so process it now
    // ---------------------------------------------------------------------
    Model generatedModel;
    Reader pomReader = null;
    try {
        pomReader = ReaderFactory.newXmlReader(pomFile);
        MavenXpp3Reader reader = new MavenXpp3Reader();
        generatedModel = reader.read(pomReader);
    } catch (IOException e) {
        throw new ArchetypeTemplateProcessingException("Error reading POM", e);
    } catch (XmlPullParserException e) {
        throw new ArchetypeTemplateProcessingException("Error reading POM", e);
    } finally {
        IOUtil.close(pomReader);
    }
    if (parentModel != null) {
        Parent parent = new Parent();
        parent.setGroupId(parentModel.getGroupId());
        if (parent.getGroupId() == null) {
            parent.setGroupId(parentModel.getParent().getGroupId());
        }
        parent.setArtifactId(parentModel.getArtifactId());
        parent.setVersion(parentModel.getVersion());
        if (parent.getVersion() == null) {
            parent.setVersion(parentModel.getParent().getVersion());
        }
        generatedModel.setParent(parent);
        Writer pomWriter = null;
        try {
            pomWriter = WriterFactory.newXmlWriter(pomFile);
            MavenXpp3Writer writer = new MavenXpp3Writer();
            writer.write(pomWriter, generatedModel);
        } catch (IOException e) {
            throw new ArchetypeTemplateProcessingException("Error rewriting POM", e);
        } finally {
            IOUtil.close(pomWriter);
        }
    }
    // XXX: Following POM processing block may be a candidate for
    // refactoring out into service methods or moving to
    // createProjectDirectoryStructure(outputDirectory)
    Build build = generatedModel.getBuild();
    boolean overrideSrcDir = false;
    boolean overrideResourceDir = false;
    boolean overrideTestSrcDir = false;
    boolean overrideTestResourceDir = false;
    boolean foundBuildElement = build != null;
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("********************* Debug info for resources created from generated Model ***********************");
        getLogger().debug("Was build element found in generated POM?: " + foundBuildElement);
    }
    // create source directory if specified in POM
    if (foundBuildElement && null != build.getSourceDirectory()) {
        getLogger().debug("Overriding default source directory ");
        overrideSrcDir = true;
        String srcDirectory = build.getSourceDirectory();
        srcDirectory = StringUtils.replace(srcDirectory, "\\", "/");
        FileUtils.mkdir(getOutputDirectory(outputDirectory, srcDirectory));
    }
    // create script source directory if specified in POM
    if (foundBuildElement && null != build.getScriptSourceDirectory()) {
        getLogger().debug("Overriding default script source directory ");
        String scriptSourceDirectory = build.getScriptSourceDirectory();
        scriptSourceDirectory = StringUtils.replace(scriptSourceDirectory, "\\", "/");
        FileUtils.mkdir(getOutputDirectory(outputDirectory, scriptSourceDirectory));
    }
    // create resource director(y/ies) if specified in POM
    if (foundBuildElement && build.getResources().size() > 0) {
        getLogger().debug("Overriding default resource directory ");
        overrideResourceDir = true;
        Iterator<?> resourceItr = build.getResources().iterator();
        while (resourceItr.hasNext()) {
            Resource resource = (Resource) resourceItr.next();
            String resourceDirectory = resource.getDirectory();
            resourceDirectory = StringUtils.replace(resourceDirectory, "\\", "/");
            FileUtils.mkdir(getOutputDirectory(outputDirectory, resourceDirectory));
        }
    }
    // create test source directory if specified in POM
    if (foundBuildElement && null != build.getTestSourceDirectory()) {
        getLogger().debug("Overriding default test directory ");
        overrideTestSrcDir = true;
        String testDirectory = build.getTestSourceDirectory();
        testDirectory = StringUtils.replace(testDirectory, "\\", "/");
        FileUtils.mkdir(getOutputDirectory(outputDirectory, testDirectory));
    }
    // create test resource directory if specified in POM
    if (foundBuildElement && build.getTestResources().size() > 0) {
        getLogger().debug("Overriding default test resource directory ");
        overrideTestResourceDir = true;
        Iterator<?> testResourceItr = build.getTestResources().iterator();
        while (testResourceItr.hasNext()) {
            Resource resource = (Resource) testResourceItr.next();
            String testResourceDirectory = resource.getDirectory();
            testResourceDirectory = StringUtils.replace(testResourceDirectory, "\\", "/");
            FileUtils.mkdir(getOutputDirectory(outputDirectory, testResourceDirectory));
        }
    }
    getLogger().debug("********************* End of debug info from resources from generated POM ***********************");
    if (descriptor.getSources().size() > 0) {
        if (!overrideSrcDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_SOURCE_DIR);
            processSources(outputDirectory, context, descriptor, packageName, DEFAULT_SOURCE_DIR);
        } else {
            processSources(outputDirectory, context, descriptor, packageName, build.getSourceDirectory());
        }
    }
    if (descriptor.getResources().size() > 0) {
        if (!overrideResourceDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_RESOURCE_DIR);
        }
        processResources(outputDirectory, context, descriptor, packageName);
    }
    if (descriptor.getTestSources().size() > 0) {
        if (!overrideTestSrcDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_TEST_SOURCE_DIR);
            processTestSources(outputDirectory, context, descriptor, packageName, DEFAULT_TEST_SOURCE_DIR);
        } else {
            processTestSources(outputDirectory, context, descriptor, packageName, build.getTestSourceDirectory());
        }
    }
    if (descriptor.getTestResources().size() > 0) {
        if (!overrideTestResourceDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_TEST_RESOURCE_DIR);
        }
        processTestResources(outputDirectory, context, descriptor, packageName);
    }
    if (descriptor.getSiteResources().size() > 0) {
        processSiteResources(outputDirectory, context, descriptor, packageName);
    }
}
Also used : Parent(org.apache.maven.model.Parent) Resource(org.apache.maven.model.Resource) SAXReader(org.dom4j.io.SAXReader) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) TemplateDescriptor(org.apache.maven.archetype.old.descriptor.TemplateDescriptor) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) XMLWriter(org.dom4j.io.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) Writer(java.io.Writer) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 20 with MavenXpp3Reader

use of org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader in project maven-archetype by apache.

the class DefaultOldArchetype method createArchetype.

public void createArchetype(ArchetypeGenerationRequest request, File archetypeFile) throws ArchetypeDescriptorException, ArchetypeTemplateProcessingException {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("basedir", request.getOutputDirectory());
    parameters.put(Constants.PACKAGE, request.getPackage());
    parameters.put("packageName", request.getPackage());
    parameters.put(Constants.GROUP_ID, request.getGroupId());
    parameters.put(Constants.ARTIFACT_ID, request.getArtifactId());
    parameters.put(Constants.VERSION, request.getVersion());
    // ---------------------------------------------------------------------
    if (getLogger().isInfoEnabled()) {
        getLogger().info("----------------------------------------------------------------------------");
        getLogger().info("Using following parameters for creating project from Old (1.x) Archetype: " + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion());
        getLogger().info("----------------------------------------------------------------------------");
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            String parameterName = entry.getKey();
            String parameterValue = entry.getValue();
            getLogger().info("Parameter: " + parameterName + ", Value: " + parameterValue);
        }
    }
    // ----------------------------------------------------------------------
    // Load the descriptor
    // ----------------------------------------------------------------------
    ArchetypeDescriptorBuilder builder = new ArchetypeDescriptorBuilder();
    ArchetypeDescriptor descriptor;
    URLClassLoader archetypeJarLoader;
    InputStream is = null;
    try {
        URL[] urls = new URL[1];
        urls[0] = archetypeFile.toURL();
        archetypeJarLoader = new URLClassLoader(urls);
        is = getStream(ARCHETYPE_DESCRIPTOR, archetypeJarLoader);
        if (is == null) {
            is = getStream(ARCHETYPE_OLD_DESCRIPTOR, archetypeJarLoader);
        }
        if (is == null) {
            throw new ArchetypeDescriptorException("The " + ARCHETYPE_DESCRIPTOR + " descriptor cannot be found.");
        }
        descriptor = builder.build(new XmlStreamReader(is));
    } catch (IOException e) {
        throw new ArchetypeDescriptorException("Error reading the " + ARCHETYPE_DESCRIPTOR + " descriptor.", e);
    } catch (XmlPullParserException e) {
        throw new ArchetypeDescriptorException("Error reading the " + ARCHETYPE_DESCRIPTOR + " descriptor.", e);
    } finally {
        IOUtil.close(is);
    }
    // ----------------------------------------------------------------------
    // 
    // ----------------------------------------------------------------------
    String artifactId = request.getArtifactId();
    File parentPomFile = new File(request.getOutputDirectory(), ARCHETYPE_POM);
    File outputDirectoryFile;
    boolean creating;
    File pomFile;
    if (parentPomFile.exists() && descriptor.isAllowPartial() && artifactId == null) {
        outputDirectoryFile = new File(request.getOutputDirectory());
        creating = false;
        pomFile = parentPomFile;
    } else {
        if (artifactId == null) {
            throw new ArchetypeTemplateProcessingException("Artifact ID must be specified when creating a new project from an archetype.");
        }
        outputDirectoryFile = new File(request.getOutputDirectory(), artifactId);
        creating = true;
        if (outputDirectoryFile.exists()) {
            if (descriptor.isAllowPartial()) {
                creating = false;
            } else {
                throw new ArchetypeTemplateProcessingException("Directory " + outputDirectoryFile.getName() + " already exists - please run from a clean directory");
            }
        }
        pomFile = new File(outputDirectoryFile, ARCHETYPE_POM);
    }
    if (creating) {
        if (request.getGroupId() == null) {
            throw new ArchetypeTemplateProcessingException("Group ID must be specified when creating a new project from an archetype.");
        }
        if (request.getVersion() == null) {
            throw new ArchetypeTemplateProcessingException("Version must be specified when creating a new project from an archetype.");
        }
    }
    String outputDirectory = outputDirectoryFile.getAbsolutePath();
    String packageName = request.getPackage();
    // ----------------------------------------------------------------------
    // Set up the Velocity context
    // ----------------------------------------------------------------------
    Context context = new VelocityContext();
    context.put(Constants.PACKAGE, packageName);
    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        context.put(entry.getKey(), entry.getValue());
    }
    // ----------------------------------------------------------------------
    // Process the templates
    // ----------------------------------------------------------------------
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(archetypeJarLoader);
    Model parentModel = null;
    if (creating) {
        if (parentPomFile.exists()) {
            Reader fileReader = null;
            try {
                fileReader = ReaderFactory.newXmlReader(parentPomFile);
                MavenXpp3Reader reader = new MavenXpp3Reader();
                parentModel = reader.read(fileReader);
                if (!"pom".equals(parentModel.getPackaging())) {
                    throw new ArchetypeTemplateProcessingException("Unable to add module to the current project as it is not of packaging type 'pom'");
                }
            } catch (IOException e) {
                throw new ArchetypeTemplateProcessingException("Unable to read parent POM", e);
            } catch (XmlPullParserException e) {
                throw new ArchetypeTemplateProcessingException("Unable to read parent POM", e);
            } finally {
                IOUtil.close(fileReader);
            }
            parentModel.getModules().add(artifactId);
        }
    }
    try {
        processTemplates(pomFile, outputDirectory, context, descriptor, packageName, parentModel);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
    if (parentModel != null) {
        /*
        // TODO: would be nice to just write out with the xpp3 writer again, except that it loses a bunch of info and
        // reformats, so the module is just baked in as a string instead.
            FileWriter fileWriter = null;

            try
            {
                fileWriter = new FileWriter( parentPomFile );

                MavenXpp3Writer writer = new MavenXpp3Writer();
                writer.write( fileWriter, parentModel );
            }
            catch ( IOException e )
            {
                throw new ArchetypeTemplateProcessingException( "Unable to rewrite parent POM", e );
            }
            finally
            {
                IOUtil.close( fileWriter );
            }
*/
        Reader fileReader = null;
        boolean added;
        StringWriter w = new StringWriter();
        try {
            fileReader = ReaderFactory.newXmlReader(parentPomFile);
            added = addModuleToParentPom(artifactId, fileReader, w);
        } catch (IOException e) {
            throw new ArchetypeTemplateProcessingException("Unable to rewrite parent POM", e);
        } catch (DocumentException e) {
            throw new ArchetypeTemplateProcessingException("Unable to rewrite parent POM", e);
        } finally {
            IOUtil.close(fileReader);
        }
        if (added) {
            Writer out = null;
            try {
                out = WriterFactory.newXmlWriter(parentPomFile);
                IOUtil.copy(w.toString(), out);
            } catch (IOException e) {
                throw new ArchetypeTemplateProcessingException("Unable to rewrite parent POM", e);
            } finally {
                IOUtil.close(out);
            }
        }
    }
    // ----------------------------------------------------------------------
    if (getLogger().isInfoEnabled()) {
        getLogger().info("project created from Old (1.x) Archetype in dir: " + outputDirectory);
    }
}
Also used : HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) SAXReader(org.dom4j.io.SAXReader) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) URL(java.net.URL) StringWriter(java.io.StringWriter) ArchetypeDescriptorBuilder(org.apache.maven.archetype.old.descriptor.ArchetypeDescriptorBuilder) DocumentException(org.dom4j.DocumentException) URLClassLoader(java.net.URLClassLoader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) InputStream(java.io.InputStream) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) ArchetypeDescriptor(org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor) URLClassLoader(java.net.URLClassLoader) Model(org.apache.maven.model.Model) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) XMLWriter(org.dom4j.io.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) Writer(java.io.Writer)

Aggregations

MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)65 Model (org.apache.maven.model.Model)59 IOException (java.io.IOException)39 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)33 FileReader (java.io.FileReader)27 File (java.io.File)18 FileNotFoundException (java.io.FileNotFoundException)17 Reader (java.io.Reader)15 Path (java.nio.file.Path)11 PluginException (org.bimserver.shared.exceptions.PluginException)11 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)10 SPluginBundle (org.bimserver.interfaces.objects.SPluginBundle)9 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)9 DelegatingClassLoader (org.bimserver.plugins.classloaders.DelegatingClassLoader)8 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)6 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)6 ArrayList (java.util.ArrayList)5 JarFile (java.util.jar.JarFile)5