Search in sources :

Example 71 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project maven-plugins by apache.

the class BundlePackMojo method readPom.

/**
 * Read the POM file.
 *
 * @param pom The file to read
 * @return A Maven Model
 * @throws MojoExecutionException if something goes wrong when reading the file
 */
private Model readPom(File pom) throws MojoExecutionException {
    Model model;
    XmlStreamReader reader = null;
    try {
        reader = ReaderFactory.newXmlReader(pom);
        model = new MavenXpp3Reader().read(reader);
        reader.close();
        reader = null;
    } catch (XmlPullParserException e) {
        throw new MojoExecutionException("Unable to parse POM at " + pom.getAbsolutePath() + ": " + e.getMessage(), e);
    } catch (FileNotFoundException e) {
        throw new MojoExecutionException("Unable to read POM at " + pom.getAbsolutePath() + ": " + e.getMessage(), e);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to read POM at " + pom.getAbsolutePath() + ": " + e.getMessage(), e);
    } finally {
        IOUtil.close(reader);
    }
    return model;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Model(org.apache.maven.model.Model) FileNotFoundException(java.io.FileNotFoundException) XmlStreamReader(org.codehaus.plexus.util.xml.XmlStreamReader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Example 72 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project maven-archetype by apache.

the class ArchetypeTest method testArchetype.

public void testArchetype() throws Exception {
    FileUtils.deleteDirectory(getTestFile("target/quickstart"));
    // ----------------------------------------------------------------------
    // This needs to be encapsulated in a maven test case.
    // ----------------------------------------------------------------------
    ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) getContainer().lookup(ArtifactRepositoryLayout.ROLE);
    String mavenRepoLocal = getTestFile("target/local-repository").toURI().toURL().toString();
    ArtifactRepository localRepository = new DefaultArtifactRepository("local", mavenRepoLocal, layout);
    List<ArtifactRepository> remoteRepositories = new ArrayList<ArtifactRepository>();
    String mavenRepoRemote = getTestFile("src/test/repository").toURI().toURL().toString();
    ArtifactRepository remoteRepository = new DefaultArtifactRepository("remote", mavenRepoRemote, layout);
    remoteRepositories.add(remoteRepository);
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    buildingRequest.setRemoteRepositories(remoteRepositories);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
    buildingRequest.setRepositorySession(repositorySession);
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest().setProjectBuildingRequest(buildingRequest).setPackage("org.apache.maven.quickstart").setGroupId("maven").setArtifactId("quickstart").setVersion("1.0-alpha-1-SNAPSHOT").setArchetypeGroupId("org.apache.maven.archetypes").setArchetypeArtifactId("maven-archetype-quickstart").setArchetypeVersion("1.0-alpha-1-SNAPSHOT").setLocalRepository(localRepository).setRemoteArtifactRepositories(remoteRepositories).setOutputDirectory(getTestFile("target").getAbsolutePath());
    // parameters.put( "name", "jason" );
    archetype.createArchetype(request, remoteRepository);
    // ----------------------------------------------------------------------
    // Set up the Velocity context
    // ----------------------------------------------------------------------
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("basedir", request.getOutputDirectory());
    parameters.put("package", request.getPackage());
    parameters.put("packageName", request.getPackage());
    parameters.put("groupId", request.getGroupId());
    parameters.put("artifactId", request.getArtifactId());
    parameters.put("version", request.getVersion());
    Context context = new VelocityContext();
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        context.put(entry.getKey(), entry.getValue());
    }
    // ----------------------------------------------------------------------
    // Validate POM generation
    // ----------------------------------------------------------------------
    ArtifactFactory artifactFactory = (ArtifactFactory) lookup(ArtifactFactory.class.getName());
    Artifact archetypeArtifact = artifactFactory.createArtifact(request.getArchetypeGroupId(), request.getArchetypeArtifactId(), request.getArchetypeVersion(), Artifact.SCOPE_RUNTIME, "jar");
    StringWriter writer = new StringWriter();
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getContextClassloader(archetypeArtifact, localRepository, remoteRepositories));
    try {
        VelocityComponent velocity = (VelocityComponent) lookup(VelocityComponent.class.getName());
        velocity.getEngine().mergeTemplate(OldArchetype.ARCHETYPE_RESOURCES + "/" + OldArchetype.ARCHETYPE_POM, context, writer);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
    Model generatedModel, templateModel;
    try {
        StringReader strReader = new StringReader(writer.toString());
        MavenXpp3Reader reader = new MavenXpp3Reader();
        templateModel = reader.read(strReader);
    } catch (IOException e) {
        throw new ArchetypeTemplateProcessingException("Error reading template POM", e);
    }
    File artifactDir = getTestFile("target", (String) parameters.get("artifactId"));
    File pomFile = getTestFile(artifactDir.getAbsolutePath(), OldArchetype.ARCHETYPE_POM);
    try {
        FileReader pomReader = new FileReader(pomFile);
        MavenXpp3Reader reader = new MavenXpp3Reader();
        generatedModel = reader.read(pomReader);
    } catch (IOException e) {
        throw new ArchetypeTemplateProcessingException("Error reading generated POM", e);
    } catch (XmlPullParserException e) {
        throw new ArchetypeTemplateProcessingException("Error reading generated POM", e);
    }
    assertEquals("Generated POM ArtifactId is not equivalent to expected result.", generatedModel.getArtifactId(), templateModel.getArtifactId());
    assertEquals("Generated POM GroupId is not equivalent to expected result.", generatedModel.getGroupId(), templateModel.getGroupId());
    assertEquals("Generated POM Id is not equivalent to expected result.", generatedModel.getId(), templateModel.getId());
    assertEquals("Generated POM Version is not equivalent to expected result.", generatedModel.getVersion(), templateModel.getVersion());
    assertEquals("Generated POM Packaging is not equivalent to expected result.", generatedModel.getPackaging(), templateModel.getPackaging());
    assertEquals("Generated POM Developers is not equivalent to expected result.", generatedModel.getDevelopers(), templateModel.getDevelopers());
    assertEquals("Generated POM Scm is not equivalent to expected result.", generatedModel.getScm(), templateModel.getScm());
}
Also used : HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) ArrayList(java.util.ArrayList) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) URLClassLoader(java.net.URLClassLoader) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArtifactRepositoryLayout(org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout) Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) Model(org.apache.maven.model.Model) VelocityComponent(org.codehaus.plexus.velocity.VelocityComponent) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 73 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project maven-archetype by apache.

the class DefaultArchetypeGenerator method generateArchetype.

public void generateArchetype(ArchetypeGenerationRequest request, ArchetypeGenerationResult result) {
    try {
        File archetypeFile = getArchetypeFile(request, request.getLocalRepository());
        generateArchetype(request, archetypeFile, result);
    } catch (IOException ex) {
        result.setCause(ex);
    } catch (ArchetypeException ex) {
        result.setCause(ex);
    } catch (XmlPullParserException ex) {
        result.setCause(ex);
    } catch (DocumentException ex) {
        result.setCause(ex);
    }
}
Also used : DocumentException(org.dom4j.DocumentException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) ArchetypeException(org.apache.maven.archetype.exception.ArchetypeException)

Example 74 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project maven-archetype by apache.

the class ArchetypeDescriptorBuilder method addTestResourceToDescriptor.

/**
 * Adds the test-resource element <code>resource</code> to the list of test-resources in the
 * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 * <i>filtered</i> if the attribute <code>filtered</code> was not
 * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
 * if its value is <code>&quot;false&quot;</code>, and the encoding specified
 * in the <code>encoding</code> attribute or the Java virtual machine's default if
 * it is not defined. If the <code>resource</code> is a property file (ends in
 * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
 * even if some other encoding is specified in the attribute.
 *
 * @param testResource a <code>&lt;resource&gt;</code> element from the <code>&lt;testResources&gt;</code>
 * @param descriptor   the <code>ArchetypeDescriptor</code> to add the test-resource template to.
 * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
 *                                value of the attribute <code>filtered</code> is no valid.
 */
private static void addTestResourceToDescriptor(Xpp3Dom testResource, ArchetypeDescriptor descriptor) throws XmlPullParserException {
    descriptor.addTestResource(testResource.getValue());
    if (testResource.getAttribute("filtered") != null) {
        TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
        try {
            testResourceDesc.setFiltered(getValueFilteredAttribute(testResource.getAttribute("filtered")));
        } catch (IllegalArgumentException iae) {
            throw new XmlPullParserException(iae.getMessage());
        }
    }
    if (testResource.getAttribute("encoding") != null) {
        TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
        try {
            testResourceDesc.setEncoding(testResource.getAttribute("encoding"));
        } catch (IllegalCharsetNameException icne) {
            throw new XmlPullParserException(testResource.getAttribute("encoding") + " is not a valid encoding.");
        } catch (UnsupportedCharsetException uce) {
            throw new XmlPullParserException(testResource.getAttribute("encoding") + " is not a supported encoding.");
        }
    }
    if (testResource.getValue().endsWith(".properties")) {
        TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
        testResourceDesc.setEncoding("iso-8859-1");
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException)

Example 75 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project maven-archetype by apache.

the class ArchetypeDescriptorBuilder method addSourceToDescriptor.

/**
 * Adds the source element <code>source</code> to the list of sources in the
 * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
 * attribute or the Java virtual machine's default if it is not defined.
 *
 * @param source     a <code>&lt;source&gt;</code> element from the <code>&lt;sources&gt;</code>
 * @param descriptor the <code>ArchetypeDescriptor</code> to add the source template to.
 * @throws XmlPullParserException if the encoding specified is not valid or supported.
 */
private static void addSourceToDescriptor(Xpp3Dom source, ArchetypeDescriptor descriptor) throws XmlPullParserException {
    descriptor.addSource(source.getValue());
    TemplateDescriptor sourceDesc = descriptor.getSourceDescriptor(source.getValue());
    sourceDesc.setFiltered(true);
    if (source.getAttribute("encoding") != null) {
        try {
            sourceDesc.setEncoding(source.getAttribute("encoding"));
        } catch (IllegalCharsetNameException icne) {
            throw new XmlPullParserException(source.getAttribute("encoding") + " is not a valid encoding.");
        } catch (UnsupportedCharsetException uce) {
            throw new XmlPullParserException(source.getAttribute("encoding") + " is not a supported encoding.");
        }
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException)

Aggregations

XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)79 IOException (java.io.IOException)73 File (java.io.File)37 Model (org.apache.maven.model.Model)32 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)30 FileNotFoundException (java.io.FileNotFoundException)20 Reader (java.io.Reader)20 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)19 FileReader (java.io.FileReader)15 FileInputStream (java.io.FileInputStream)12 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)11 StringReader (java.io.StringReader)10 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)10 Path (java.nio.file.Path)7 Artifact (org.eclipse.aether.artifact.Artifact)7 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)7 PluginException (org.bimserver.shared.exceptions.PluginException)6 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)6 HashMap (java.util.HashMap)5