/** * @param jarFile Jar file to be created * @param sourceFolder Jar file will be created out of the contents of this folder. This * corresponds to the root folder of the jar file once it is created. */ public static void archiveWar(File jarFile, String sourceFolder) throws MojoExecutionException { try { ZipArchiver archiver = new ZipArchiver(); archiver.enableLogging( new ConsoleLogger(org.codehaus.plexus.logging.Logger.LEVEL_INFO, "console")); archiver.setDestFile(jarFile); archiver.addDirectory(new File(sourceFolder), "", new String[] {"**/*"}, null); archiver.createArchive(); } catch (Exception e) { throw new MojoExecutionException("Error while creating WAR file", e); } }
/** Overridden from Zip class to deal with manifests and index lists. */ protected void zipFile( InputStream is, ZipArchiveOutputStream zOut, String vPath, long lastModified, File fromArchive, int mode, String symlinkDestination) throws IOException, ArchiverException { if (MANIFEST_NAME.equalsIgnoreCase(vPath)) { if (!doubleFilePass || skipWriting) { filesetManifest(fromArchive, is); } } else if (INDEX_NAME.equalsIgnoreCase(vPath) && index) { getLogger() .warn( "Warning: selected " + archiveType + " files include a META-INF/INDEX.LIST which will" + " be replaced by a newly generated one."); } else { if (index && (!vPath.contains("/"))) { rootEntries.addElement(vPath); } super.zipFile(is, zOut, vPath, lastModified, fromArchive, mode, symlinkDestination); } }
private void writeManifest(ZipArchiveOutputStream zOut, Manifest manifest) throws IOException, ArchiverException { for (Enumeration e = manifest.getWarnings(); e.hasMoreElements(); ) { getLogger().warn("Manifest warning: " + e.nextElement()); } zipDir(null, zOut, "META-INF/", DEFAULT_DIR_MODE); // time to write the manifest ByteArrayOutputStream baos = new ByteArrayOutputStream(); manifest.write(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); super.zipFile( bais, zOut, MANIFEST_NAME, System.currentTimeMillis(), null, DEFAULT_FILE_MODE, null); super.initZipOutputStream(zOut); }
/** * reset to default values. * * @see ZipArchiver#reset */ public void reset() { super.reset(); configuredManifest = null; filesetManifestConfig = null; mergeManifestsMain = false; manifestFile = null; index = false; }
/** * Make sure we don't think we already have a MANIFEST next time this task gets executed. * * @see ZipArchiver#cleanUp */ protected void cleanUp() throws IOException { super.cleanUp(); // we want to save this info if we are going to make another pass if (!doubleFilePass || !skipWriting) { manifest = null; configuredManifest = savedConfiguredManifest; filesetManifest = null; originalManifest = null; } rootEntries.removeAllElements(); }
@Override public void execute() throws MojoExecutionException, MojoFailureException { super.execute(); compiler.setOutputDirectory(outputDirectory); try { String outputDirectoryName = OutputNamesHelper.getHarValidationOutput(project.getArtifact()); File outputBase = new File(outputDirectory, outputDirectoryName); if (!outputBase.exists()) outputBase.mkdirs(); if (!skipValidation) { validateTargets(outputBase); } else { getLog().warn("Validation skipped"); } File metadata = createHarMetadata(outputBase); ZipArchiver archiver = new ZipArchiver(); archiver.addFile(metadata, HarMetadata.METADATA_FILE_NAME); for (String compileRoot : project.getCompileSourceRoots()) { archiver.addDirectory(new File(compileRoot)); } for (File resourceDir : ProjectHelper.getResourceDirectories(project)) { archiver.addDirectory(resourceDir); } File destFile = new File( outputDirectory, project.getBuild().getFinalName() + "." + HaxeFileExtensions.HAR); archiver.setDestFile(destFile); archiver.createArchive(); project.getArtifact().setFile(destFile); } catch (IOException e) { throw new MojoFailureException("Error occurred during `har` package creation", e); } catch (Exception e) { throw new MojoFailureException("Har validation failed", e); } }
@Override public void execute() throws MojoExecutionException, MojoFailureException { File outputFile = null, outputAlignedFile = null, destFile = null, destAlignedFile = null; String techId; if (baseDir.getPath().endsWith("source") || baseDir.getPath().endsWith("unit") || baseDir.getPath().endsWith("functional") || baseDir.getPath().endsWith("performance")) { try { if (baseDir.getPath().endsWith("source")) { rootPomFile = new File( baseDir.getParentFile().getPath() + File.separator + PluginConstants.POM_XML); } else { String rootDir = baseDir.getParentFile().getParentFile().getParentFile().getPath(); SplitedSourceDirName = rootDir.substring(rootDir.lastIndexOf(File.separator) + 1); rootPomFile = new File( baseDir.getParentFile().getParentFile().getParentFile().getPath() + File.separator + SplitedSourceDirName + File.separator + PluginConstants.POM_XML); } try { if (rootPomFile.exists()) { rootPomProcessor = new PomProcessor(rootPomFile); if (StringUtils.isNotEmpty( rootPomProcessor.getProperty(Constants.POM_PROP_KEY_SPLIT_PHRESCO_DIR))) { dotPhrescoDirName = rootPomProcessor.getProperty(Constants.POM_PROP_KEY_SPLIT_PHRESCO_DIR); testDirName = rootPomProcessor.getProperty(Constants.POM_PROP_KEY_SPLIT_TEST_DIR); } } } catch (PhrescoPomException e) { e.printStackTrace(); } buildInfoList = new ArrayList<BuildInfo>(); // initialization if (baseDir.getPath().endsWith("source") && dotPhrescoDirName == null) { buildDir = new File(baseDir.getParentFile().getPath() + buildDirectory); packageInfoFile = new File( baseDir.getParentFile().getPath() + File.separator + ".phresco" + File.separator + "phresco-package-info.xml"); } else if (baseDir.getPath().endsWith("source") && dotPhrescoDirName != null) { // code for new archetype changes starts dotPhrescoDir = baseDir.getParentFile().getParentFile().getPath() + File.separator + dotPhrescoDirName; File projectDir = new File(baseDir.getParentFile().getParentFile().getPath()); File[] filesInDir = projectDir.listFiles(); fileProcessor(filesInDir); // code for new archetype changes ends buildDir = new File(finalBuildDir + File.separator + buildDirectory); packageInfoFile = new File( dotPhrescoDir + File.separator + ".phresco" + File.separator + "phresco-package-info.xml"); } else if (dotPhrescoDirName != null) { dotPhrescoDir = baseDir.getParentFile().getParentFile().getParentFile().getPath() + File.separator + dotPhrescoDirName; buildDir = new File(baseDir.getPath() + buildDirectory); } else { buildDir = new File(baseDir.getPath() + buildDirectory); } if (!buildDir.exists()) { buildDir.mkdir(); } buildInfoFile = new File(buildDir.getPath() + "/build.info"); if ((baseDir.getPath().endsWith("unit") || baseDir.getPath().endsWith("functional") || baseDir.getPath().endsWith("performance")) && dotPhrescoDir != null) { packageInfoFile = new File( dotPhrescoDir + File.separator + ".phresco" + File.separator + "phresco-package-info.xml"); } else if (baseDir.getPath().endsWith("unit") || baseDir.getPath().endsWith("functional") || baseDir.getPath().endsWith("performance")) { packageInfoFile = new File( baseDir.getParentFile().getParentFile() + File.separator + ".phresco" + File.separator + "phresco-package-info.xml"); buildDir = baseDir.getParentFile().getParentFile(); buildInfoFile = new File(baseDir.getPath() + buildDirectory + "/build.info"); } MojoProcessor processor = new MojoProcessor(packageInfoFile); Configuration configuration = processor.getConfiguration("package"); Map<String, String> configs = MojoUtil.getAllValues(configuration); techId = configs.get("techId"); if (StringUtils.isNotEmpty(techId)) { outputFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + '.' + APKLIB); } else { outputFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + '.' + APK); outputAlignedFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + "-aligned." + APK); } nextBuildNo = generateNextBuildNo(); currentDate = Calendar.getInstance().getTime(); } catch (IOException e) { throw new MojoFailureException("APK could not initialize " + e.getLocalizedMessage()); } catch (PhrescoException e) { throw new MojoFailureException("APK could not initialize " + e.getLocalizedMessage()); } if (outputFile.exists()) { try { if (StringUtils.isNotEmpty(techId)) { getLog().info("APKLib created.. Copying to Build directory....."); } else { getLog().info("APK created.. Copying to Build directory....."); } String buildNameLocal = project.getBuild().getFinalName() + '_' + getTimeStampForBuildName(currentDate); if (baseDir.getPath().endsWith("unit") || baseDir.getPath().endsWith("functional") || baseDir.getPath().endsWith("performance")) { buildDir = new File(baseDir.getPath() + buildDirectory); } if (buildName != null) { if (StringUtils.isNotEmpty(techId)) { destFile = new File(buildDir, buildName + '.' + APKLIB); } else { destFile = new File(buildDir, buildName + '.' + APK); // Creating the file in build folder for copying the aligned APK - Created by Hari - // 20-May-2013 destAlignedFile = new File(buildDir, buildName + "-aligned." + APK); } FileUtils.copyFile(outputFile, destFile); getLog().info("copied to..." + destFile.getName()); /* If outputAlignedFile exists in target folder, * Then we are copying it to destinationFile in build folder * Added By - Hari - May, 20 , 2013 */ if (outputAlignedFile != null && outputAlignedFile.exists()) { FileUtils.copyFile(outputAlignedFile, destAlignedFile); } apkFileName = destFile.getName(); getLog().info("Creating deliverables....."); ZipArchiver zipArchiver = new ZipArchiver(); File tmpFile = new File(buildDir, buildName); if (!tmpFile.exists()) { tmpFile.mkdirs(); } FileUtils.copyFileToDirectory(destFile, tmpFile); /*To Copy the aligned apk into zip file in build folder, *It is for downloading the aligned apk from build Tab * Added by - Hari -May, 20 ,2013 */ if (destAlignedFile != null && destAlignedFile.exists()) { FileUtils.copyFileToDirectory(destAlignedFile, tmpFile); } if (!packageInfoFile.exists()) { PluginUtils.createBuildResources(packageInfoFile, baseDir, tmpFile); } File inputFile = new File(apkFileName); zipArchiver.addDirectory(tmpFile); File deliverableZip = new File(buildDir, buildName + ".zip"); zipArchiver.setDestFile(deliverableZip); zipArchiver.createArchive(); deliverable = deliverableZip.getPath(); getLog().info("Deliverables available at " + deliverableZip.getName()); if (tmpFile.exists()) { FileUtil.delete(tmpFile); } writeBuildInfo(true); } else { if (StringUtils.isNotEmpty(techId)) { destFile = new File(buildDir, buildNameLocal + '.' + APKLIB); } else { destFile = new File(buildDir, buildNameLocal + '.' + APK); // Creating the file in build folder for copying the aligned APK - Created by Hari - // 20-May-2013 destAlignedFile = new File(buildDir, buildNameLocal + "-aligned." + APK); } FileUtils.copyFile(outputFile, destFile); getLog().info("copied to..." + destFile.getName()); /* If outputAlignedFile exists in target folder, * Then we are copying it to destinationFile in build folder * Added By - Hari - May, 20 , 2013 */ if (outputAlignedFile != null && outputAlignedFile.exists()) { FileUtils.copyFile(outputAlignedFile, destAlignedFile); } apkFileName = destFile.getName(); getLog().info("Creating deliverables....."); ZipArchiver zipArchiver = new ZipArchiver(); File tmpFile = new File(buildDir, buildNameLocal); if (!tmpFile.exists()) { tmpFile.mkdirs(); } FileUtils.copyFileToDirectory(destFile, tmpFile); /*To Copy the aligned apk into zip file in build folder, *It is for downloading the aligned apk from build Tab * Added by - Hari -May, 20 ,2013 */ if (destAlignedFile != null && destAlignedFile.exists()) { FileUtils.copyFileToDirectory(destAlignedFile, tmpFile); } if (!packageInfoFile.exists()) { PluginUtils.createBuildResources(packageInfoFile, baseDir, tmpFile); } File inputFile = new File(apkFileName); zipArchiver.addDirectory(tmpFile); File deliverableZip = new File(buildDir, buildNameLocal + ".zip"); zipArchiver.setDestFile(deliverableZip); zipArchiver.createArchive(); deliverable = deliverableZip.getPath(); getLog().info("Deliverables available at " + deliverableZip.getName()); if (tmpFile.exists()) { FileUtil.delete(tmpFile); } writeBuildInfo(true); } } catch (IOException e) { throw new MojoExecutionException("Error in writing output..."); } } } else { getLog().info("It is a component "); } }
@Override public void execute() throws MojoExecutionException, MojoFailureException { File outputFile = null, destFile = null, projectHome = null; String techId; try { getLog().info("Base Dir === " + baseDir.getAbsolutePath()); buildInfoList = new ArrayList<BuildInfo>(); // initialization // srcDir = new File(baseDir.getPath() + File.separator + sourceDirectory); buildDir = new File(baseDir.getPath() + buildDirectory); if (!buildDir.exists()) { buildDir.mkdir(); getLog().info("Build directory created..." + buildDir.getPath()); } buildInfoFile = new File(buildDir.getPath() + "/build.info"); if (baseDir.getPath().endsWith("unit") || baseDir.getPath().endsWith("functional") || baseDir.getPath().endsWith("performance")) { projectHome = new File( baseDir.getParentFile().getParentFile() + File.separator + ".phresco" + File.separator + "phresco-package-info.xml"); } else { projectHome = new File( baseDir.getPath() + File.separator + ".phresco" + File.separator + "phresco-package-info.xml"); } MojoProcessor processor = new MojoProcessor(projectHome); Configuration configuration = processor.getConfiguration("package"); Map<String, String> configs = MojoUtil.getAllValues(configuration); if (baseDir.getPath().endsWith("unit") || baseDir.getPath().endsWith("functional") || baseDir.getPath().endsWith("performance")) { buildDir = baseDir.getParentFile().getParentFile(); buildInfoFile = new File(baseDir.getPath() + buildDirectory + "/build.info"); } techId = configs.get("techId"); if (StringUtils.isNotEmpty(techId)) { outputFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + '.' + APKLIB); } else { outputFile = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + '.' + APK); } nextBuildNo = generateNextBuildNo(); currentDate = Calendar.getInstance().getTime(); } catch (IOException e) { throw new MojoFailureException("APK could not initialize " + e.getLocalizedMessage()); } catch (PhrescoException e) { throw new MojoFailureException("APK could not initialize " + e.getLocalizedMessage()); } if (outputFile.exists()) { try { getLog().info("APK created.. Copying to Build directory....."); String buildName = project.getBuild().getFinalName() + '_' + getTimeStampForBuildName(currentDate); if (baseDir.getPath().endsWith("unit") || baseDir.getPath().endsWith("functional") || baseDir.getPath().endsWith("performance")) { buildDir = new File(baseDir.getPath() + buildDirectory); } if (StringUtils.isNotEmpty(techId)) { destFile = new File(buildDir, buildName + '.' + APKLIB); } else { destFile = new File(buildDir, buildName + '.' + APK); } FileUtils.copyFile(outputFile, destFile); getLog().info("copied to..." + destFile.getName()); apkFileName = destFile.getName(); getLog().info("Creating deliverables....."); File packageInfoFile = new File( baseDir.getPath() + File.separator + ".phresco" + File.separator + PluginConstants.PHRESCO_PACKAGE_FILE); ZipArchiver zipArchiver = new ZipArchiver(); File tmpFile = new File(buildDir, buildName); if (!tmpFile.exists()) { tmpFile.mkdirs(); } FileUtils.copyFileToDirectory(destFile, tmpFile); if (packageInfoFile.exists()) { PluginUtils.createBuildResources(packageInfoFile, baseDir, tmpFile); } File inputFile = new File(apkFileName); zipArchiver.addDirectory(tmpFile); File deliverableZip = new File(buildDir, buildName + ".zip"); zipArchiver.setDestFile(deliverableZip); zipArchiver.createArchive(); deliverable = deliverableZip.getPath(); getLog().info("Deliverables available at " + deliverableZip.getName()); writeBuildInfo(true); if (!tmpFile.exists()) { FileUtil.delete(tmpFile); } } catch (IOException e) { throw new MojoExecutionException("Error in writing output..."); } } }
/** * Create the index list to speed up classloading. This is a JDK 1.3+ specific feature and is * enabled by default. See <a * href="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#JAR%20Index">the JAR index * specification</a> for more details. * * @param zOut the zip stream representing the jar being built. * @throws IOException thrown if there is an error while creating the index and adding it to the * zip stream. * @throws org.codehaus.plexus.archiver.ArchiverException . */ private void createIndexList(ZipArchiveOutputStream zOut) throws IOException, ArchiverException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // encoding must be UTF8 as specified in the specs. PrintWriter writer = new PrintWriter(new OutputStreamWriter(baos, "UTF8")); // version-info blankline writer.println("JarIndex-Version: 1.0"); writer.println(); // header newline writer.println(getDestFile().getName()); // filter out META-INF if it doesn't contain anything other than the index and manifest. // this is what sun.misc.JarIndex does, guess we ought to be consistent. Set<String> filteredDirs = new HashSet<String>(addedDirs.keySet()); // our added dirs always have a trailing slash if (filteredDirs.contains(META_INF_NAME + '/')) { boolean add = false; for (String entry : entries.keySet()) { if (entry.startsWith(META_INF_NAME + '/') && !entry.equals(INDEX_NAME) && !entry.equals(MANIFEST_NAME)) { add = true; break; } } if (!add) { filteredDirs.remove(META_INF_NAME + '/'); } } writeIndexLikeList(new ArrayList<String>(filteredDirs), rootEntries, writer); writer.println(); if (indexJars != null) { java.util.jar.Manifest mf = createManifest(); String classpath = mf.getMainAttributes().getValue(ManifestConstants.ATTRIBUTE_CLASSPATH); String[] cpEntries = null; if (classpath != null) { StringTokenizer tok = new StringTokenizer(classpath, " "); cpEntries = new String[tok.countTokens()]; int c = 0; while (tok.hasMoreTokens()) { cpEntries[c++] = tok.nextToken(); } } for (String indexJar : indexJars) { String name = findJarName(indexJar, cpEntries); if (name != null) { ArrayList<String> dirs = new ArrayList<String>(); ArrayList<String> files = new ArrayList<String>(); grabFilesAndDirs(indexJar, dirs, files); if (dirs.size() + files.size() > 0) { writer.println(name); writeIndexLikeList(dirs, files, writer); writer.println(); } } } } writer.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); super.zipFile( bais, zOut, INDEX_NAME, System.currentTimeMillis(), null, DEFAULT_FILE_MODE, null); }