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());
}
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;
}
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;
}
Aggregations