
com.tpspay.postmaven.postbuild.GenerateBuildMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of postbuild-maven-plugin Show documentation
Show all versions of postbuild-maven-plugin Show documentation
A maven build plugin for building Postilion nodes.
package com.tpspay.postmaven.postbuild;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
/**
* Goal to generate the build.generated.py file
*
* @goal post-buildpy
*
* @phase generate-sources
*
* @author John Oxley <[email protected]>
*/
public class GenerateBuildMojo extends AbstractPostilionBuildMojo {
private static final String NL = System.getProperty("line.separator");
/**
* Location of the generated build.py file
*
* @parameter default-value="build.py"
*/
private String buildFile;
/**
* @parameter default-value="${project}"
* @required
* @readonly
*/
private MavenProject project;
public static String generateClasspaths(List fileList) {
StringBuilder sb = new StringBuilder();
for (String file : fileList) {
sb.append(NL);
sb.append("\"");
sb.append(file.replace("\\", "\\\\"));
sb.append("\",");
}
return sb.toString().substring(NL.length());
}
public void execute() throws MojoExecutionException, MojoFailureException {
try {
getLog().info("Generating build.generated.py");
String classpaths = getClasspaths();
File orig = new File(buildFile);
File generated = new File(buildGeneratedFile);
if (generated.exists()) {
getLog().info("Deleting existing " + generated.getAbsolutePath());
generated.delete();
}
getLog().info("Original file: " + orig.getAbsolutePath());
getLog().info("Generated file: " + generated.getAbsolutePath());
BufferedReader reader = new BufferedReader(new FileReader(orig));
FileWriter writer = new FileWriter(generated);
String line = reader.readLine();
while (line != null) {
String write = line;
if (line.contains("${postilion.dependencies}")) {
write = classpaths;
}
if (line.contains("${project.version}")) {
write = line.replace("${project.version}", project.getVersion());
}
writer.write(write);
writer.write(NL);
line = reader.readLine();
}
writer.close();
reader.close();
} catch (IOException e) {
throw new MojoExecutionException("Problem deleting build directory", e);
}
}
private String getClasspaths() {
File dir = new File("target/dependencies");
if (!dir.exists()) {
return null;
}
File[] files = dir.listFiles();
if (files.length == 0) {
return null;
}
List fileList = new ArrayList();
for (File f : files) {
fileList.add(f.getPath());
}
return generateClasspaths(fileList);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy