List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory JAR
String JAR
To view the source code for org.apache.commons.compress.archivers ArchiveStreamFactory JAR.
Click Source Link
From source file:org.apache.tomcat.maven.plugin.tomcat8.run.AbstractStandaloneWarMojo.java
public void execute() throws MojoExecutionException, MojoFailureException { if (!"war".equals(project.getPackaging())) { throw new MojoFailureException("Pacakaging must be of type war for standalone-war goal."); }//from w ww .j a v a 2 s .c o m File warExecFile = new File(buildDirectory, finalName); if (warExecFile.exists()) { warExecFile.delete(); } File execWarJar = new File(buildDirectory, finalName); FileOutputStream execWarJarOutputStream = null; ArchiveOutputStream os = null; File tmpPropertiesFile = null; File tmpManifestFile = null; FileOutputStream tmpPropertiesFileOutputStream = null; PrintWriter tmpManifestWriter = null; try { tmpPropertiesFile = new File(buildDirectory, "war-exec.properties"); if (tmpPropertiesFile.exists()) { tmpPropertiesFile.delete(); } tmpPropertiesFile.getParentFile().mkdirs(); tmpManifestFile = new File(buildDirectory, "war-exec.manifest"); if (tmpManifestFile.exists()) { tmpManifestFile.delete(); } tmpPropertiesFileOutputStream = new FileOutputStream(tmpPropertiesFile); execWarJar.getParentFile().mkdirs(); execWarJar.createNewFile(); execWarJarOutputStream = new FileOutputStream(execWarJar); tmpManifestWriter = new PrintWriter(tmpManifestFile); // store : //* wars in the root: foo.war //* tomcat jars //* file tomcat.standalone.properties with possible values : // * useServerXml=true/false to use directly the one provided // * enableNaming=true/false // * wars=foo.war|contextpath;bar.war ( |contextpath is optionnal if empty use the war name ) // * accessLogValveFormat= // * connectorhttpProtocol: HTTP/1.1 or org.apache.coyote.http11.Http11NioProtocol // * codeSourceContextPath=path parameter, default is project.artifactId //* optionnal: conf/ with usual tomcat configuration files //* MANIFEST with Main-Class Properties properties = new Properties(); properties.put(Tomcat8Runner.ARCHIVE_GENERATION_TIMESTAMP_KEY, Long.toString(System.currentTimeMillis())); properties.put(Tomcat8Runner.ENABLE_NAMING_KEY, Boolean.toString(enableNaming)); properties.put(Tomcat8Runner.ACCESS_LOG_VALVE_FORMAT_KEY, accessLogValveFormat); properties.put(Tomcat8Runner.HTTP_PROTOCOL_KEY, connectorHttpProtocol); properties.put(Tomcat8Runner.CODE_SOURCE_CONTEXT_PATH, path); os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, execWarJarOutputStream); extractJarToArchive(new JarFile(projectArtifact.getFile()), os, null); if (serverXml != null && serverXml.exists()) { os.putArchiveEntry(new JarArchiveEntry("conf/server.xml")); IOUtils.copy(new FileInputStream(serverXml), os); os.closeArchiveEntry(); properties.put(Tomcat8Runner.USE_SERVER_XML_KEY, Boolean.TRUE.toString()); } else { properties.put(Tomcat8Runner.USE_SERVER_XML_KEY, Boolean.FALSE.toString()); } os.putArchiveEntry(new JarArchiveEntry("conf/web.xml")); IOUtils.copy(getClass().getResourceAsStream("/conf/web.xml"), os); os.closeArchiveEntry(); properties.store(tmpPropertiesFileOutputStream, "created by Apache Tomcat Maven plugin"); tmpPropertiesFileOutputStream.flush(); tmpPropertiesFileOutputStream.close(); os.putArchiveEntry(new JarArchiveEntry(Tomcat8RunnerCli.STAND_ALONE_PROPERTIES_FILENAME)); IOUtils.copy(new FileInputStream(tmpPropertiesFile), os); os.closeArchiveEntry(); // add tomcat classes for (Artifact pluginArtifact : pluginArtifacts) { if (StringUtils.equals("org.apache.tomcat", pluginArtifact.getGroupId()) // || StringUtils.equals("org.apache.tomcat.embed", pluginArtifact.getGroupId()) // || StringUtils.equals("org.eclipse.jdt.core.compiler", pluginArtifact.getGroupId()) // || StringUtils.equals("commons-cli", pluginArtifact.getArtifactId()) // || StringUtils.equals("tomcat8-war-runner", pluginArtifact.getArtifactId())) { JarFile jarFile = new JarFile(pluginArtifact.getFile()); extractJarToArchive(jarFile, os, null); } } // add extra dependencies if (extraDependencies != null && !extraDependencies.isEmpty()) { for (Dependency dependency : extraDependencies) { String version = dependency.getVersion(); if (StringUtils.isEmpty(version)) { version = findArtifactVersion(dependency); } if (StringUtils.isEmpty(version)) { throw new MojoExecutionException("Dependency '" + dependency.getGroupId() + "':'" + dependency.getArtifactId() + "' does not have version specified"); } // String groupId, String artifactId, String version, String scope, String type Artifact artifact = artifactFactory.createArtifact(dependency.getGroupId(), // dependency.getArtifactId(), // version, // dependency.getScope(), // dependency.getType()); artifactResolver.resolve(artifact, this.remoteRepos, this.local); JarFile jarFile = new JarFile(artifact.getFile()); extractJarToArchive(jarFile, os, excludes); } } Manifest manifest = new Manifest(); Manifest.Attribute mainClassAtt = new Manifest.Attribute(); mainClassAtt.setName("Main-Class"); mainClassAtt.setValue(mainClass); manifest.addConfiguredAttribute(mainClassAtt); manifest.write(tmpManifestWriter); tmpManifestWriter.flush(); tmpManifestWriter.close(); os.putArchiveEntry(new JarArchiveEntry("META-INF/MANIFEST.MF")); IOUtils.copy(new FileInputStream(tmpManifestFile), os); os.closeArchiveEntry(); if (attachArtifact) { //MavenProject project, String artifactType, String artifactClassifier, File artifactFile projectHelper.attachArtifact(project, attachArtifactClassifierType, attachArtifactClassifier, execWarJar); } if (extraResources != null) { for (ExtraResource extraResource : extraResources) { DirectoryScanner directoryScanner = new DirectoryScanner(); directoryScanner.setBasedir(extraResource.getDirectory()); directoryScanner.addDefaultExcludes(); directoryScanner.setExcludes(toStringArray(extraResource.getExcludes())); directoryScanner.setIncludes(toStringArray(extraResource.getIncludes())); directoryScanner.scan(); for (String includeFile : directoryScanner.getIncludedFiles()) { getLog().debug("include file:" + includeFile); os.putArchiveEntry(new JarArchiveEntry(includeFile)); IOUtils.copy(new FileInputStream(new File(extraResource.getDirectory(), includeFile)), os); os.closeArchiveEntry(); } } } if (tomcatConfigurationFilesDirectory != null && tomcatConfigurationFilesDirectory.exists()) { // Because its the tomcat default dir for configs String aConfigOutputDir = "conf/"; copyDirectoryContentIntoArchive(tomcatConfigurationFilesDirectory, aConfigOutputDir, os); } } catch (ManifestException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (ArchiveException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (ArtifactNotFoundException e) { throw new MojoExecutionException(e.getMessage(), e); } catch (ArtifactResolutionException e) { throw new MojoExecutionException(e.getMessage(), e); } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(tmpManifestWriter); IOUtils.closeQuietly(execWarJarOutputStream); IOUtils.closeQuietly(tmpPropertiesFileOutputStream); } }
From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java
private void addFilesToArchive(ArchiveOutputStream taos, File file) throws IOException { // Create an entry for the file //taos.putArchiveEntry(new TarArchiveEntry(file, file.getParentFile().toURI().relativize(file.toURI()).toString())); switch (archivingFormat) { case ArchiveStreamFactory.TAR: taos.putArchiveEntry(new TarArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break;//from w w w. j a v a 2 s .c om case ArchiveStreamFactory.ZIP: taos.putArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break; case ArchiveStreamFactory.JAR: taos.putArchiveEntry(new JarArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())))); break; case ArchiveStreamFactory.AR: taos.putArchiveEntry(new ArArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break; case ArchiveStreamFactory.CPIO: taos.putArchiveEntry(new CpioArchiveEntry(file, FilenameUtils.separatorsToUnix( Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))); break; } if (file.isFile()) { // Add the file to the archive BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(bis, taos); taos.closeArchiveEntry(); bis.close(); } else if (file.isDirectory()) { // close the archive entry taos.closeArchiveEntry(); // go through all the files in the directory and using recursion, add them to the archive for (File childFile : file.listFiles()) { addFilesToArchive(taos, childFile); } } }
From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java
private void validateArchivingFormat() { if (!archivingFormat.equals(ArchiveStreamFactory.CPIO) && !archivingFormat.equals(ArchiveStreamFactory.TAR) && !archivingFormat.equals(ArchiveStreamFactory.JAR) && !archivingFormat.equals(ArchiveStreamFactory.AR) && !archivingFormat.equals(ArchiveStreamFactory.ZIP) && !archivingFormat.equals("exploded")) { throw new PackageToolException(PackagingToolReturnInfo.PKG_ASSEMBLER_INVALID_PARAMS, String.format(/*from w w w. jav a 2 s . co m*/ "Specified archiving format <%s> is not supported. The supported archiving " + "formats are: %s, %s, %s, %s, %s, %s.", archivingFormat, ArchiveStreamFactory.AR, ArchiveStreamFactory.CPIO, ArchiveStreamFactory.JAR, ArchiveStreamFactory.TAR, ArchiveStreamFactory.ZIP, "exploded")); } }
From source file:org.deventropy.shared.utils.DirectoryArchiverUtil.java
/** * Create a Jar archive with all the contents of the directory. Optionally push the contents down a directory level * or two. A Manifest file is automatically added. * //from w ww . j a v a 2 s. com * @param archiveFile The final archive file location. The file location must be writable. * @param srcDirectory The source directory. * @param rootPathPrefix The root prefix. Multiple directory parts should be separated by <code>/</code>. * @throws IOException Exception reading the source directory or writing to the destination file. */ public static void createJarArchiveOfDirectory(final String archiveFile, final File srcDirectory, final String rootPathPrefix) throws IOException { createArchiveOfDirectory(archiveFile, srcDirectory, rootPathPrefix, ArchiveStreamFactory.JAR, UTF_8_NAME, new JarArchiverCreateProcessor()); }
From source file:org.structr.websocket.command.UnarchiveCommand.java
@Override public void processMessage(WebSocketMessage webSocketData) { final Set<String> supportedByArchiveStreamFactory = new HashSet<>( Arrays.asList(new String[] { ArchiveStreamFactory.AR, ArchiveStreamFactory.ARJ, ArchiveStreamFactory.CPIO, ArchiveStreamFactory.DUMP, ArchiveStreamFactory.JAR, ArchiveStreamFactory.TAR, ArchiveStreamFactory.ZIP })); final SecurityContext securityContext = getWebSocket().getSecurityContext(); final App app = StructrApp.getInstance(securityContext); try {// w ww . ja v a 2s . c o m final String id = (String) webSocketData.getId(); final File file; try (final Tx tx = app.tx()) { file = app.get(File.class, id); if (file == null) { getWebSocket().send( MessageBuilder.status().code(400).message("File not found: ".concat(id)).build(), true); return; } final String fileExtension = StringUtils.substringAfterLast(file.getName(), "."); if (!supportedByArchiveStreamFactory.contains(fileExtension)) { getWebSocket().send(MessageBuilder.status().code(400) .message("Unsupported archive format: ".concat(fileExtension)).build(), true); return; } tx.success(); } // no transaction here since this is a bulk command unarchive(securityContext, file); } catch (Throwable t) { t.printStackTrace(); String msg = t.toString(); try (final Tx tx = app.tx()) { // return error message getWebSocket().send( MessageBuilder.status().code(400) .message("Could not unarchive file: ".concat((msg != null) ? msg : "")).build(), true); tx.success(); } catch (FrameworkException ignore) { } } }