List of usage examples for org.apache.commons.compress.archivers ArchiveOutputStream putArchiveEntry
public abstract void putArchiveEntry(ArchiveEntry entry) throws IOException;
From source file:org.apache.tomcat.maven.plugin.tomcat7.run.AbstractExecWarMojo.java
/** * return file can be deleted//from w w w . j a va 2 s . c o m */ protected File addContextXmlToWar(File contextXmlFile, File warFile) throws IOException, ArchiveException { ArchiveOutputStream os = null; OutputStream warOutputStream = null; File tmpWar = File.createTempFile("tomcat", "war-exec"); tmpWar.deleteOnExit(); try { warOutputStream = new FileOutputStream(tmpWar); os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, warOutputStream); os.putArchiveEntry(new JarArchiveEntry("META-INF/context.xml")); IOUtils.copy(new FileInputStream(contextXmlFile), os); os.closeArchiveEntry(); JarFile jarFile = new JarFile(warFile); extractJarToArchive(jarFile, os, null); os.flush(); } finally { IOUtils.closeQuietly(os); IOUtils.closeQuietly(warOutputStream); } return tmpWar; }
From source file:org.apache.tomcat.maven.plugin.tomcat7.run.AbstractExecWarMojo.java
/** * Copy the contents of a jar file to another archive * * @param file The input jar file//from w w w. java 2 s . c o m * @param os The output archive * @throws IOException */ protected void extractJarToArchive(JarFile file, ArchiveOutputStream os, String[] excludes) throws IOException { Enumeration<? extends JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry j = entries.nextElement(); if (excludes != null && excludes.length > 0) { for (String exclude : excludes) { if (SelectorUtils.match(exclude, j.getName())) { continue; } } } if (StringUtils.equalsIgnoreCase(j.getName(), "META-INF/MANIFEST.MF")) { continue; } os.putArchiveEntry(new JarArchiveEntry(j.getName())); IOUtils.copy(file.getInputStream(j), os); os.closeArchiveEntry(); } if (file != null) { file.close(); } }
From source file:org.apache.tomcat.maven.plugin.tomcat7.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."); }/* www. java 2s . co 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(Tomcat7Runner.ARCHIVE_GENERATION_TIMESTAMP_KEY, Long.toString(System.currentTimeMillis())); properties.put(Tomcat7Runner.ENABLE_NAMING_KEY, Boolean.toString(enableNaming)); properties.put(Tomcat7Runner.ACCESS_LOG_VALVE_FORMAT_KEY, accessLogValveFormat); properties.put(Tomcat7Runner.HTTP_PROTOCOL_KEY, connectorHttpProtocol); properties.put(Tomcat7Runner.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(Tomcat7Runner.USE_SERVER_XML_KEY, Boolean.TRUE.toString()); } else { properties.put(Tomcat7Runner.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(Tomcat7RunnerCli.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("tomcat7-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.apache.tomcat.maven.plugin.tomcat8.run.AbstractExecWarMojo.java
public void execute() throws MojoExecutionException, MojoFailureException { if (this.skip) { getLog().info("skip execution"); return;//from w w w . j a v a 2s .c om } //project.addAttachedArtifact( ); 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 //* 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); if (httpPort != null) { properties.put(Tomcat8Runner.HTTP_PORT_KEY, httpPort); } os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.JAR, execWarJarOutputStream); if ("war".equals(project.getPackaging())) { os.putArchiveEntry(new JarArchiveEntry(StringUtils.removeStart(path, "/") + ".war")); IOUtils.copy(new FileInputStream(projectArtifact.getFile()), os); os.closeArchiveEntry(); properties.put(Tomcat8Runner.WARS_KEY, StringUtils.removeStart(path, "/") + ".war|" + path); } else if (warRunDependencies != null && !warRunDependencies.isEmpty()) { for (WarRunDependency warRunDependency : warRunDependencies) { if (warRunDependency.dependency != null) { Dependency dependency = warRunDependency.dependency; 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"); } Artifact artifact = artifactFactory.createArtifactWithClassifier(dependency.getGroupId(), // dependency.getArtifactId(), // version, // dependency.getType(), // dependency.getClassifier()); artifactResolver.resolve(artifact, this.remoteRepos, this.local); File warFileToBundle = new File(resolvePluginWorkDir(), artifact.getFile().getName()); FileUtils.copyFile(artifact.getFile(), warFileToBundle); if (warRunDependency.contextXml != null) { warFileToBundle = addContextXmlToWar(warRunDependency.contextXml, warFileToBundle); } final String warFileName = artifact.getFile().getName(); os.putArchiveEntry(new JarArchiveEntry(warFileName)); IOUtils.copy(new FileInputStream(warFileToBundle), os); os.closeArchiveEntry(); String propertyWarValue = properties.getProperty(Tomcat8Runner.WARS_KEY); String contextPath = StringUtils.isEmpty(warRunDependency.contextPath) ? "/" : warRunDependency.contextPath; if (propertyWarValue != null) { properties.put(Tomcat8Runner.WARS_KEY, propertyWarValue + ";" + warFileName + "|" + contextPath); } else { properties.put(Tomcat8Runner.WARS_KEY, warFileName + "|" + contextPath); } } } } 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, this.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.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."); }//w ww . j av a 2 s . co 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.artifactory.util.ArchiveUtils.java
/** * Archives the contents of the given directory into the given archive using the apache commons compress tools * * @param sourceDirectory Directory to archive * @param destinationArchive Archive file to create * @param recurse True if should recurse file scan of source directory. False if not * @param archiveType Archive type to create * @throws java.io.IOException Any exceptions that might occur while handling the given files and used streams * @throws IllegalArgumentException Thrown when given invalid destinations *//*from w ww .ja va2 s . c o m*/ public static void archive(File sourceDirectory, File destinationArchive, boolean recurse, ArchiveType archiveType) throws IOException { if ((sourceDirectory == null) || (destinationArchive == null)) { throw new IllegalArgumentException("Supplied destinations cannot be null."); } if (!sourceDirectory.isDirectory()) { throw new IllegalArgumentException("Supplied source directory must be an existing directory."); } String sourcePath = sourceDirectory.getAbsolutePath(); String archivePath = destinationArchive.getAbsolutePath(); log.debug("Beginning to archive '{}' into '{}'", sourcePath, archivePath); FileOutputStream destinationOutputStream = new FileOutputStream(destinationArchive); ArchiveOutputStream archiveOutputStream = createArchiveOutputStream( new BufferedOutputStream(destinationOutputStream), archiveType); try { @SuppressWarnings({ "unchecked" }) Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, recurse); childrenFiles.remove(destinationArchive); ArchiveEntry archiveEntry; FileInputStream fileInputStream; for (File childFile : childrenFiles) { String childPath = childFile.getAbsolutePath(); String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length()); /** * Need to convert separators to unix format since zipping on windows machines creates windows specific * FS file paths */ relativePath = FilenameUtils.separatorsToUnix(relativePath); archiveEntry = createArchiveEntry(childFile, relativePath, archiveType); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } log.debug("Archive '{}' into '{}'", childPath, archivePath); } } finally { IOUtils.closeQuietly(archiveOutputStream); } log.debug("Completed archiving of '{}' into '{}'", sourcePath, archivePath); }
From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializer.java
/** * Serializes the identified stream from the package state to the supplied archive output stream. * * @param state the package state object containing the identified stream * @param streamId the stream identifier for the content being serialized * @param aos the archive output stream to serialize the stream to *//*from www . j a v a2s. c o m*/ void serializeToArchive(PackageState state, StreamId streamId, ArchiveOutputStream aos) { // when writing to an archive file: // 1. read the stream to be serialized, to get its properties // 2. create the archive entry using the properties and write it to the archive stream // 3. write the stream to be serialized to the archive stream // 4. close the entry final FileTime now = FileTime.fromMillis(Calendar.getInstance().getTimeInMillis()); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 4); CRC32CalculatingOutputStream crc = new CRC32CalculatingOutputStream(baos); StreamResult result = new StreamResult(crc); serializeToResult(state, streamId, result); ArchiveEntry arxEntry = arxStreamFactory.newArchiveEntry(streamId.name(), baos.size(), now, now, 0644, crc.resetCrc()); try { aos.putArchiveEntry(arxEntry); baos.writeTo(aos); aos.closeArchiveEntry(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } }
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;// w w w . ja v a 2s .co m 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.dbflute.helper.io.compress.DfZipArchiver.java
protected void addDir(ArchiveOutputStream archive, File topDir, File targetDir) throws IOException { final String name = buildEntryName(topDir, targetDir, true); archive.putArchiveEntry(new ZipArchiveEntry(name)); archive.closeArchiveEntry();/*from w ww. j a v a 2 s . c o m*/ }
From source file:org.dbflute.helper.io.compress.DfZipArchiver.java
protected void addFile(ArchiveOutputStream archive, File topDir, File targetFile) throws IOException { final String name = buildEntryName(topDir, targetFile, false); archive.putArchiveEntry(new ZipArchiveEntry(name)); FileInputStream ins = null;//w w w .j av a 2s . c om try { ins = new FileInputStream(targetFile); IOUtils.copy(ins, archive); } finally { if (ins != null) { try { ins.close(); } catch (IOException ignored) { } } } archive.closeArchiveEntry(); }