Search in sources :

Example 1 with MavenFileFilterRequest

use of org.apache.maven.shared.filtering.MavenFileFilterRequest in project maven-plugins by apache.

the class ChangesMojo method getChangesFromFile.

/* --------------------------------------------------------------------- */
/* Private methods */
/* --------------------------------------------------------------------- */
/**
 * Parses specified changes.xml file. It also makes filtering if needed. If specified file doesn't exist it will log
 * warning and return <code>null</code>.
 *
 * @param changesXml changes xml file to parse
 * @param project maven project to parse changes for
 * @param additionalProperties additional properties used for filtering
 * @return parsed <code>ChangesXML</code> instance or null if file doesn't exist
 * @throws MavenReportException if any errors occurs while parsing
 */
private ChangesXML getChangesFromFile(File changesXml, MavenProject project, Properties additionalProperties) throws MavenReportException {
    if (!changesXml.exists()) {
        getLog().warn("changes.xml file " + changesXml.getAbsolutePath() + " does not exist.");
        return null;
    }
    if (filteringChanges) {
        if (!filteredOutputDirectory.exists()) {
            filteredOutputDirectory.mkdirs();
        }
        XmlStreamReader xmlStreamReader = null;
        try {
            // so we get encoding from the file itself
            xmlStreamReader = new XmlStreamReader(changesXml);
            String encoding = xmlStreamReader.getEncoding();
            File resultFile = new File(filteredOutputDirectory, project.getGroupId() + "." + project.getArtifactId() + "-changes.xml");
            final MavenFileFilterRequest mavenFileFilterRequest = new MavenFileFilterRequest(changesXml, resultFile, true, project, Collections.<String>emptyList(), false, encoding, session, additionalProperties);
            mavenFileFilter.copyFile(mavenFileFilterRequest);
            changesXml = resultFile;
            xmlStreamReader.close();
            xmlStreamReader = null;
        } catch (IOException e) {
            throw new MavenReportException("Exception during filtering changes file : " + e.getMessage(), e);
        } catch (MavenFilteringException e) {
            throw new MavenReportException("Exception during filtering changes file : " + e.getMessage(), e);
        } finally {
            IOUtil.close(xmlStreamReader);
        }
    }
    return new ChangesXML(changesXml, getLog());
}
Also used : MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) MavenFileFilterRequest(org.apache.maven.shared.filtering.MavenFileFilterRequest) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) IOException(java.io.IOException) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 2 with MavenFileFilterRequest

use of org.apache.maven.shared.filtering.MavenFileFilterRequest in project maven-plugins by apache.

the class ProcessRemoteResourcesMojo method setupRequest.

private MavenFileFilterRequest setupRequest(Resource resource, File source, File file) {
    MavenFileFilterRequest req = new MavenFileFilterRequest();
    req.setFrom(source);
    req.setTo(file);
    req.setFiltering(resource.isFiltering());
    req.setMavenProject(project);
    req.setMavenSession(mavenSession);
    req.setInjectProjectBuildFilters(true);
    if (encoding != null) {
        req.setEncoding(encoding);
    }
    if (filterDelimiters != null && !filterDelimiters.isEmpty()) {
        LinkedHashSet<String> delims = new LinkedHashSet<String>();
        if (useDefaultFilterDelimiters) {
            delims.addAll(req.getDelimiters());
        }
        for (String delim : filterDelimiters) {
            if (delim == null) {
                delims.add("${*}");
            } else {
                delims.add(delim);
            }
        }
        req.setDelimiters(delims);
    }
    return req;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MavenFileFilterRequest(org.apache.maven.shared.filtering.MavenFileFilterRequest)

Example 3 with MavenFileFilterRequest

use of org.apache.maven.shared.filtering.MavenFileFilterRequest in project maven-plugins by apache.

the class ProcessRemoteResourcesMojo method copyResourceIfExists.

protected boolean copyResourceIfExists(File file, String relFileName, VelocityContext context) throws IOException, MojoExecutionException {
    for (Resource resource : resources) {
        File resourceDirectory = new File(resource.getDirectory());
        if (!resourceDirectory.exists()) {
            continue;
        }
        // TODO - really should use the resource includes/excludes and name mapping
        File source = new File(resourceDirectory, relFileName);
        File templateSource = new File(resourceDirectory, relFileName + TEMPLATE_SUFFIX);
        if (!source.exists() && templateSource.exists()) {
            source = templateSource;
        }
        if (source.exists() && !source.equals(file)) {
            if (source == templateSource) {
                Reader reader = null;
                Writer writer = null;
                DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold, file);
                try {
                    if (encoding != null) {
                        reader = new InputStreamReader(new FileInputStream(source), encoding);
                        writer = new OutputStreamWriter(os, encoding);
                    } else {
                        reader = ReaderFactory.newPlatformReader(source);
                        writer = WriterFactory.newPlatformWriter(os);
                    }
                    velocity.evaluate(context, writer, "", reader);
                    writer.close();
                    writer = null;
                    reader.close();
                    reader = null;
                } catch (ParseErrorException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (MethodInvocationException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (ResourceNotFoundException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } finally {
                    IOUtil.close(writer);
                    IOUtil.close(reader);
                }
                fileWriteIfDiffers(os);
            } else if (resource.isFiltering()) {
                MavenFileFilterRequest req = setupRequest(resource, source, file);
                try {
                    fileFilter.copyFile(req);
                } catch (MavenFilteringException e) {
                    throw new MojoExecutionException("Error filtering resource: " + source, e);
                }
            } else {
                FileUtils.copyFile(source, file);
            }
            // exclude the original (so eclipse doesn't complain about duplicate resources)
            resource.addExclude(relFileName);
            return true;
        }
    }
    return false;
}
Also used : InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) Resource(org.apache.maven.model.Resource) RemoteResourcesBundleXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) SupplementalDataModelXpp3Reader(org.apache.maven.plugin.resources.remote.io.xpp3.SupplementalDataModelXpp3Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader) FileInputStream(java.io.FileInputStream) MavenFileFilterRequest(org.apache.maven.shared.filtering.MavenFileFilterRequest) OutputStreamWriter(java.io.OutputStreamWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) FileWriter(java.io.FileWriter)

Aggregations

MavenFileFilterRequest (org.apache.maven.shared.filtering.MavenFileFilterRequest)3 File (java.io.File)2 MavenFilteringException (org.apache.maven.shared.filtering.MavenFilteringException)2 FileInputStream (java.io.FileInputStream)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 LinkedHashSet (java.util.LinkedHashSet)1 XmlStreamReader (org.apache.commons.io.input.XmlStreamReader)1 DeferredFileOutputStream (org.apache.commons.io.output.DeferredFileOutputStream)1 Resource (org.apache.maven.model.Resource)1 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 RemoteResourcesBundleXpp3Reader (org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Reader)1