com.fluxtion.maven.FluxtionScanToGenMojo Maven / Gradle / Ivy
package com.fluxtion.maven;
import org.apache.maven.artifact.Artifact;
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.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author greg higgins
*/
@Mojo(name = "scan",
requiresProject = true,
requiresDependencyResolution = ResolutionScope.COMPILE,
defaultPhase = LifecyclePhase.COMPILE
)
public class FluxtionScanToGenMojo extends AbstractMojo {
public static final String GENERATOR_METHOD = "scanAndGenerateFluxtionBuilders";
private static final String FLUXTION_GENERATOR_CLASS = "com.fluxtion.compiler.Fluxtion";
/**
* The output directory for build artifacts generated by fluxtion. Absolute paths are preceded with "/" otherwise
* the path relative to the project root directory
*/
@Parameter(property = "buildDirectory", defaultValue = "target/classes")
private String buildDirectory;
@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;
@Override
public void execute() throws MojoExecutionException {
if (System.getProperty("skipFluxtion") != null) {
getLog().info("Fluxtion generation skipped.");
} else {
try {
if (buildDirectory == null) {
buildDirectory = project.getBasedir().getCanonicalPath() + "/target/classes";
} else if (!buildDirectory.startsWith("/")) {
buildDirectory = project.getBasedir().getCanonicalPath() + "/" + buildDirectory;
}
URLClassLoader classLoader = buildFluxtionClassLoader();
Class> genClass = classLoader.loadClass(FLUXTION_GENERATOR_CLASS);
Method generatorMethod = genClass.getMethod(GENERATOR_METHOD, ClassLoader.class, File[].class);
generatorMethod.invoke(null, classLoader, new File[]{new File(buildDirectory)});
} catch (Exception exception) {
getLog().error(exception);
throw new MojoExecutionException("problem setting building fluxtion class loader", exception);
}
}
}
@SuppressWarnings("Unchecked")
private URLClassLoader buildFluxtionClassLoader() throws MalformedURLException {
List artifactList = project.getCompileArtifacts();
List elements = artifactList.stream().map(Artifact::getFile).map(File::getPath).collect(Collectors.toList());
URL[] urls = new URL[elements.size() + 1];
urls[0] = new File(buildDirectory).toURI().toURL();
String cp = buildDirectory;
for (int i = 0; i < elements.size(); i++) {
cp += File.pathSeparator + elements.get(i);
urls[i + 1] = new File(elements.get(i)).toURI().toURL();
}
getLog().debug("user classpath URL list:" + Arrays.toString(urls));
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
System.setProperty("FLUXTION.GENERATION.CLASSPATH", cp);
return classLoader;
}
}