package br.com.ingenieux.launchify.mojo;
import java.io.File;
import java.net.URL;
import java.util.Collection;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.vfs2.VFS;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.jfrog.maven.annomojo.annotations.MojoGoal;
import org.jfrog.maven.annomojo.annotations.MojoParameter;
import br.com.ingenieux.launchify.core.Scanner;
import br.com.ingenieux.launchify.spi.LaunchifySPI;
import br.com.ingenieux.launchify.spi.hashdot.HashdotSPI;
import br.com.ingenieux.launchify.spi.launch4j.Launch4JSPI;
@MojoGoal("create-local-stubs")
public class CreateLocalStubs extends AbstractLaunchifyMojo {
private final String[] DEFAULT_SOURCE_PATHS = new String[] { new File(
new File(System.getProperty("user.dir")), "target/classes")
.getAbsolutePath() };
@MojoParameter(description = "List of Paths to Scan for Classes")
String[] sourcePaths = DEFAULT_SOURCE_PATHS;
@MojoParameter(defaultValue = "${project.basedir}/target/classes/bin", expression = "${launchify.targetDirectory}", description = "Where to place the generated stubs")
String targetDirectory;
@MojoParameter(expression = "${launchify.spi}", description = "SPI Adapter to Use")
String spiAdapter = SystemUtils.IS_OS_WINDOWS ? Launch4JSPI.class.getName()
: HashdotSPI.class.getName();
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
try {
executeInternal();
} catch (Exception exc) {
throw new MojoExecutionException(exc.getMessage());
}
}
private void executeInternal() throws Exception {
this.sourcePaths = DEFAULT_SOURCE_PATHS;
LaunchifySPI spi = getSPI(new Scanner(this.sourcePaths),
super.collectDependencies());
spi.execute();
}
private LaunchifySPI getSPI(Scanner scanner, Collection<URL> classpathUrls) {
try {
LaunchifySPI spi = (LaunchifySPI) Class.forName(this.spiAdapter)
.newInstance();
spi.setApplicationEntries(scanner.getApplicationEntries());
spi.setClasspathEntries(classpathUrls);
spi.setDestinationDirectory(VFS.getManager().resolveFile(
this.targetDirectory));
spi.setParent(this);
return spi;
} catch (Exception exc) {
throw new RuntimeException(exc);
}
}
}