org.linqs.plugins.ListClasses Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of list-classes-maven-plugin Show documentation
Show all versions of list-classes-maven-plugin Show documentation
A Maven plugin that list the packages of the project and drops them into a properties file.
The newest version!
package org.linqs.plugins;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
/**
* Create a listing of all the classes in a properties file.
*/
@Mojo(name = "list", defaultPhase = LifecyclePhase.INITIALIZE)
public class ListClasses extends AbstractMojo {
public static final String DEFAULT_OUTPUT_FILENAME = "classlist.properties";
public static final String DEFAULT_KEY_PREFIX = "classlist";
public static final String DEFAULT_KEY_SUFFIX = "classes";
@Parameter(defaultValue = DEFAULT_OUTPUT_FILENAME)
private String outputFilename;
@Parameter(defaultValue = DEFAULT_KEY_PREFIX)
private String keyPrefix;
@Parameter(defaultValue = DEFAULT_KEY_SUFFIX)
private String keySuffix;
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;
public void execute() throws MojoExecutionException {
List classNames = new ArrayList();
@SuppressWarnings("unchecked")
List roots = (List)(project.getCompileSourceRoots());
for (String root : roots) {
Path rootPath = Paths.get(root);
for (String path : getFiles(root)) {
Path relPath = rootPath.relativize(Paths.get(path));
if (!relPath.getFileName().toString().endsWith(".java")) {
continue;
}
classNames.add(relativePathToClassName(relPath));
}
}
String outputPath = Paths.get(project.getBuild().getOutputDirectory(), outputFilename).toString();
try (FileWriter writer = new FileWriter(outputPath)) {
for (String name : classNames) {
writer.write(String.format("%s.%s=%s\n", keyPrefix, keySuffix, name));
}
} catch (IOException ex) {
throw new RuntimeException("Unable to write class list: " + outputPath, ex);
}
}
/**
* Take a relative path (that starts at the first package)
* for a file and convert it into a fully qualified class name.
*/
private String relativePathToClassName(Path relPath) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < relPath.getNameCount(); i++) {
if (i == (relPath.getNameCount() - 1)) {
builder.append(relPath.getName(i).toString().replaceFirst("\\.java$", ""));
} else {
builder.append(relPath.getName(i).toString());
builder.append(".");
}
}
return builder.toString();
}
private List getFiles(String root) {
final List paths = new ArrayList();
try {
Files.walkFileTree(Paths.get(root), new SimpleFileVisitor() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
paths.add(file.toString());
return FileVisitResult.CONTINUE;
}
});
} catch (IOException ex) {
throw new RuntimeException("Unable to walk directory: " + root, ex);
}
return paths;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy