run-script-maven-plugin/src/main/java/org/lcsim
diff -u -r1.7 -r1.8
--- RunScriptMavenPluginMojo.java 20 Feb 2009 06:06:26 -0000 1.7
+++ RunScriptMavenPluginMojo.java 16 Mar 2009 18:40:35 -0000 1.8
@@ -2,6 +2,8 @@
import java.io.BufferedWriter;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
@@ -22,27 +24,35 @@
{
/**
* Directory where the run script should be installed.
- * The default is bin/ in the project's base directory.
- *
- * @parameter expression="${basedir}/bin"
+ * @parameter expression="${project.build.directory}/bin"
*/
private String binDir;
/**
* Name of the script.
- * The default is the project's name.
- *
* @parameter expression="${project.artifactId}"
*/
private String scriptName;
/**
* Name of the main class to run.
- * No default.
- * @parameter expression="mainClass"
+ * @parameter
* @required
*/
private String mainClass;
+
+ /**
+ * The library directory where project's jar dependencies will be copied.
+ * By default, the script will instead use the jar locations from the local repository.
+ * @parameter
+ */
+ private String libDir;
+
+ /**
+ * Java options to be added to the run script.
+ * @parameter
+ */
+ private String javaOptions;
/**
* The MavenProject variable for accessing project's artifacts.
@@ -50,20 +60,43 @@
*/
private MavenProject mavenProject;
+ /**
+ * The plugin's execution method.
+ */
public void execute() throws MojoExecutionException
- {
- String classPath = makeClassPath(makeClassPathEntries());
+ {
+ // Make the class path entries using the local repository.
+ List<String> classPathEntries = makeClassPathEntries();
+
+ // If a library directory is specified, copy the dependencies there,
+ // and get back a new set of class path entries.
+ if (libDir != null)
+ {
+ classPathEntries = copyDepsToLibDir(libDir, classPathEntries);
+ }
+
+ // Make a single class path from the class path entries.
+ String classPath = makeClassPath(classPathEntries);
- // FIXME: need way to set options, possibly support different platforms (bash,windows)
- String runLine = "java -Xmx1024m -XX:MaxPermSize=128m -cp \"" + classPath + "\" " + mainClass + " $*";
-
- // Make bin dir.
- (new File(binDir)).mkdirs();
+ // Setup the java command.
+ String runLine = "java ";
+ if (javaOptions != null)
+ runLine += javaOptions;
+ runLine += " -cp \"" + classPath + "\" " + mainClass + " $*";
+
+ // Create the bin directory if it doesn't exist already.
+ File binDirFile = new File(binDir);
+ if (!binDirFile.exists())
+ {
+ binDirFile.mkdirs();
+ binDirFile.setExecutable(true);
+ binDirFile.setWritable(true);
+ }
- // Full path to script.
+ // Full path to the script.
File scriptFile = new File(binDir + File.separator + scriptName.replace(" ", ""));
- // Write out script to file.
+ // Create the run script in the bin dir.
Writer output = null;
try {
output = new BufferedWriter(new FileWriter(scriptFile));
@@ -75,31 +108,22 @@
throw new RuntimeException(x);
}
- // Set Unix permissions.
- String os = System.getProperties().getProperty("os.name");
- if (os.contains("Linux") || os.contains("Mac OS X"))
- {
- try {
- Runtime.getRuntime().exec("chmod 755 " + scriptFile.getCanonicalPath());
- }
- catch (IOException x)
- {
- throw new RuntimeException(x);
- }
- }
-
- // TODO: Need to set permissions for Windows or defaults are okay?
+ // Set run script permissions.
+ scriptFile.setExecutable(true);
+ scriptFile.setReadable(true);
+ scriptFile.setWritable(false);
}
/**
- * Makes a list of class path entries with the project's jar and its dependencies.
+ * Makes a list of class path entries including this project's dependencies
+ * and its own jar.
* @return A list of class path entries.
*/
private List<String> makeClassPathEntries()
{
List<String> classPathEntries = new ArrayList<String>();
- // Project's deps.
+ // Project's dependencies.
for (Object o : mavenProject.getArtifacts())
{
Artifact artifact = (Artifact)o;
@@ -112,8 +136,9 @@
}
}
- // Project's jar.
- try {
+ // The project's jar file.
+ try
+ {
classPathEntries.add(mavenProject.getArtifact().getFile().getCanonicalPath());
}
catch (IOException x)
@@ -125,7 +150,7 @@
}
/**
- * Creates a class path suitable for using in a run script.
+ * Creates a class path with entries separated by colons.
* @param classPathEntries A list of class path entries.
* @return A string with the class path.
*/
@@ -141,4 +166,72 @@
classPath = classPath.substring(0,classPath.length()-1);
return classPath;
}
+
+ /**
+ * Copy the dependencies to the lib directory and return a set of classpath entries
+ * for the copied jars.
+ * @param libDir
+ * @param classPathEntries
+ * @return
+ */
+ private List<String> copyDepsToLibDir(String libDir, List<String> classPathEntries)
+ {
+ List<String> newClassPathEntries = new ArrayList<String>();
+ File libDirFile = new File(libDir);
+ if (!libDirFile.exists())
+ {
+ libDirFile.mkdir();
+ libDirFile.setExecutable(true);
+ libDirFile.setReadable(true);
+ libDirFile.setWritable(true);
+ }
+ for (String depPath : classPathEntries)
+ {
+ File repoFile = new File(depPath);
+ String libPath = libDirFile.getAbsolutePath() + File.separator + repoFile.getName();
+ File libFile = new File(libPath);
+ try
+ {
+ //System.out.println("copying " + repoFile.getCanonicalPath() + " to " + libFile.getCanonicalPath());
+ copyFile(repoFile, libFile);
+ }
+ catch (Exception x)
+ {
+ throw new RuntimeException("Problem copying " + repoFile.getAbsolutePath() + " to " + libFile.getAbsolutePath() + ".");
+ }
+ //System.out.println("adding new classpath entry " + libPath);
+ newClassPathEntries.add(libPath);
+ }
+ return newClassPathEntries;
+ }
+
+ /**
+ * Simple file copy utility method.
+ * @param in
+ * @param out
+ * @throws Exception
+ */
+ private static void copyFile(File in, File out) throws Exception
+ {
+ FileInputStream fis = new FileInputStream(in);
+ FileOutputStream fos = new FileOutputStream(out);
+ try
+ {
+ byte[] buf = new byte[1024];
+ int i = 0;
+ while ((i = fis.read(buf)) != -1)
+ {
+ fos.write(buf, 0, i);
+ }
+ }
+ catch (Exception x)
+ {
+ throw x;
+ }
+ finally
+ {
+ if (fis != null) fis.close();
+ if (fos != null) fos.close();
+ }
+ }
}