use of org.codehaus.plexus.archiver.jar.JarArchiver in project cxf by apache.
the class AbstractCodegenMoho method runForked.
protected void runForked(Set<URI> classPath, String mainClassName, String[] args) throws MojoExecutionException {
getLog().info("Running code generation in fork mode...");
getLog().debug("Running code generation in fork mode with args " + Arrays.asList(args));
Commandline cmd = new Commandline();
// for JVM args
cmd.getShell().setQuotedArgumentsEnabled(true);
cmd.setWorkingDirectory(project.getBuild().getDirectory());
try {
cmd.setExecutable(getJavaExecutable().getAbsolutePath());
} catch (IOException e) {
getLog().debug(e);
throw new MojoExecutionException(e.getMessage(), e);
}
cmd.createArg().setLine(additionalJvmArgs);
File file = null;
try {
// file = new File("/tmp/test.jar");
file = FileUtils.createTempFile("cxf-codegen", ".jar");
JarArchiver jar = new JarArchiver();
jar.setDestFile(file.getAbsoluteFile());
Manifest manifest = new Manifest();
Attribute attr = new Attribute();
attr.setName("Class-Path");
StringBuilder b = new StringBuilder(8000);
for (URI cp : classPath) {
b.append(cp.toURL().toExternalForm()).append(' ');
}
attr.setValue(b.toString());
manifest.getMainSection().addConfiguredAttribute(attr);
attr = new Attribute();
attr.setName("Main-Class");
attr.setValue(mainClassName);
manifest.getMainSection().addConfiguredAttribute(attr);
jar.addConfiguredManifest(manifest);
jar.createArchive();
cmd.createArg().setValue("-jar");
String tmpFilePath = file.getAbsolutePath();
if (tmpFilePath.contains(" ")) {
// ensure the path is in double quotation marks if the path contain space
tmpFilePath = "\"" + tmpFilePath + "\"";
}
cmd.createArg().setValue(tmpFilePath);
} catch (Exception e1) {
throw new MojoExecutionException("Could not create runtime jar", e1);
}
cmd.addArguments(args);
StreamConsumer out = new StreamConsumer() {
public void consumeLine(String line) {
getLog().info(line);
}
};
final StringBuilder b = new StringBuilder();
StreamConsumer err = new StreamConsumer() {
public void consumeLine(String line) {
b.append(line);
b.append("\n");
getLog().warn(line);
}
};
int exitCode;
try {
exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
} catch (CommandLineException e) {
getLog().debug(e);
throw new MojoExecutionException(e.getMessage(), e);
}
String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
if (exitCode != 0) {
StringBuilder msg = new StringBuilder("\nExit code: ");
msg.append(exitCode);
msg.append('\n');
msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
throw new MojoExecutionException(msg.toString());
}
file.delete();
if (b.toString().contains("WSDL2Java Error")) {
StringBuilder msg = new StringBuilder();
msg.append(b.toString());
msg.append('\n');
msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
throw new MojoExecutionException(msg.toString());
}
}
use of org.codehaus.plexus.archiver.jar.JarArchiver in project tycho by eclipse.
the class PackagePluginMojo method makeJar.
private File makeJar(BuildOutputJar jar) throws MojoExecutionException {
String jarName = jar.getName();
BuildProperties buildProperties = pdeProject.getBuildProperties();
String customManifest = buildProperties.getJarToManifestMap().get(jarName);
try {
File jarFile = new File(project.getBasedir(), jarName);
JarArchiver archiver = new JarArchiver();
archiver.setDestFile(jarFile);
archiver.addDirectory(jar.getOutputDirectory());
if (customManifest != null) {
for (File sourceFolder : jar.getSourceFolders()) {
File manifestFile = new File(sourceFolder, customManifest);
if (manifestFile.isFile()) {
archiver.setManifest(manifestFile);
break;
}
}
}
archiver.createArchive();
return jarFile;
} catch (Exception e) {
throw new MojoExecutionException("Could not create jar " + jarName, e);
}
}
use of org.codehaus.plexus.archiver.jar.JarArchiver in project maven-plugins by apache.
the class AbstractWarPackagingTask method copyFile.
/**
* Copy file from source to destination. The directories up to <code>destination</code> will be created if they
* don't already exist. if the <code>onlyIfModified</code> flag is <tt>false</tt>, <code>destination</code> will be
* overwritten if it already exists. If the flag is <tt>true</tt> destination will be overwritten if it's not up to
* date.
*
* @param context the packaging context
* @param source an existing non-directory <code>File</code> to copy bytes from
* @param destination a non-directory <code>File</code> to write bytes to (possibly overwriting).
* @param targetFilename the relative path of the file from the webapp root directory
* @param onlyIfModified if true, copy the file only if the source has changed, always copy otherwise
* @return true if the file has been copied/updated, false otherwise
* @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be written to, or an
* IO error occurs during copying
*/
protected boolean copyFile(WarPackagingContext context, File source, File destination, String targetFilename, boolean onlyIfModified) throws IOException {
if (onlyIfModified && destination.lastModified() >= source.lastModified()) {
context.getLog().debug(" * " + targetFilename + " is up to date.");
return false;
} else {
if (source.isDirectory()) {
context.getLog().warn(" + " + targetFilename + " is packaged from the source folder");
try {
JarArchiver archiver = context.getJarArchiver();
archiver.addDirectory(source);
archiver.setDestFile(destination);
archiver.createArchive();
} catch (ArchiverException e) {
String msg = "Failed to create " + targetFilename;
context.getLog().error(msg, e);
IOException ioe = new IOException(msg);
ioe.initCause(e);
throw ioe;
}
} else {
FileUtils.copyFile(source.getCanonicalFile(), destination);
// preserve timestamp
destination.setLastModified(source.lastModified());
context.getLog().debug(" + " + targetFilename + " has been copied.");
}
return true;
}
}
use of org.codehaus.plexus.archiver.jar.JarArchiver in project cxf by apache.
the class AbstractCodegenMojo method runForked.
protected void runForked(Set<URI> classPath, String mainClassName, String[] args) throws MojoExecutionException {
getLog().info("Running code generation in fork mode...");
getLog().debug("Running code generation in fork mode with args " + Arrays.asList(args));
Commandline cmd = new Commandline();
// for JVM args
cmd.getShell().setQuotedArgumentsEnabled(true);
cmd.setWorkingDirectory(project.getBuild().getDirectory());
String javaPath = getJavaExecutable().getAbsolutePath();
cmd.setExecutable(javaPath);
setJvmForkArgs(javaPath);
cmd.createArg().setLine(additionalJvmArgs);
final File file;
try {
// file = new File("/tmp/test.jar");
file = FileUtils.createTempFile("cxf-codegen", ".jar");
JarArchiver jar = new JarArchiver();
jar.setDestFile(file.getAbsoluteFile());
Manifest manifest = new Manifest();
Attribute attr = new Attribute();
attr.setName("Class-Path");
StringBuilder b = new StringBuilder(8000);
for (URI cp : classPath) {
b.append(cp.toURL().toExternalForm()).append(' ');
}
attr.setValue(b.toString());
manifest.getMainSection().addConfiguredAttribute(attr);
attr = new Attribute();
attr.setName("Main-Class");
attr.setValue(mainClassName);
manifest.getMainSection().addConfiguredAttribute(attr);
jar.addConfiguredManifest(manifest);
jar.createArchive();
cmd.createArg().setValue("-jar");
String tmpFilePath = file.getAbsolutePath();
if (tmpFilePath.contains(" ")) {
// ensure the path is in double quotation marks if the path contain space
tmpFilePath = '"' + tmpFilePath + '"';
}
cmd.createArg().setValue(tmpFilePath);
} catch (Exception e1) {
throw new MojoExecutionException("Could not create runtime jar", e1);
}
cmd.addArguments(args);
StreamConsumer out = new StreamConsumer() {
public void consumeLine(String line) {
getLog().info(line);
}
};
final StringBuilder b = new StringBuilder();
StreamConsumer err = new StreamConsumer() {
public void consumeLine(String line) {
b.append(line);
b.append('\n');
getLog().warn(line);
}
};
int exitCode;
try {
exitCode = CommandLineUtils.executeCommandLine(cmd, out, err);
} catch (CommandLineException e) {
getLog().debug(e);
throw new MojoExecutionException(e.getMessage(), e);
}
String cmdLine = CommandLineUtils.toString(cmd.getCommandline());
if (exitCode != 0) {
StringBuilder msg = new StringBuilder("\nExit code: ");
msg.append(exitCode);
msg.append('\n');
msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
throw new MojoExecutionException(msg.toString());
}
file.delete();
if (b.toString().contains("WSDL2Java Error")) {
StringBuilder msg = new StringBuilder();
msg.append(b.toString());
msg.append('\n');
msg.append("Command line was: ").append(cmdLine).append('\n').append('\n');
throw new MojoExecutionException(msg.toString());
}
}
use of org.codehaus.plexus.archiver.jar.JarArchiver in project azure-tools-for-java by Microsoft.
the class JarUtils method buildJarFileToStagingPath.
public static Path buildJarFileToStagingPath(String stagingFolder, Module module) throws IOException {
final File stagingFolderFile = new File(stagingFolder);
if (!stagingFolderFile.exists()) {
stagingFolderFile.mkdirs();
}
final String moduleName = module.getName();
final String path = CompilerPaths.getModuleOutputPath(module, false);
final Path outputFile = Paths.get(stagingFolder, moduleName + ".jar");
final JarArchiver jar = new JarArchiver();
jar.setCompress(true);
jar.setDestFile(outputFile.toFile());
jar.addDirectory(new File(path));
final Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(new Attributes.Name("Created-By"), "Azure Intellij Plugin");
try {
jar.addConfiguredManifest(manifest);
} catch (ManifestException e) {
throw new AzureToolkitRuntimeException("Cannot create manifest for function jar.", e);
}
jar.createArchive();
return outputFile;
}
Aggregations