List of usage examples for java.util.jar JarFile getManifest
public Manifest getManifest() throws IOException
From source file:org.apache.struts2.osgi.BaseOsgiHost.java
/** * Gets the version used to export the packages. it tries to get it from MANIFEST.MF, or the file name *//* w w w . j a v a 2 s . c o m*/ protected String getVersion(URL url) { if ("jar".equals(url.getProtocol())) { try { JarFile jarFile = new JarFile(new File(URLUtil.normalizeToFileProtocol(url).toURI())); Manifest manifest = jarFile.getManifest(); if (manifest != null) { String version = manifest.getMainAttributes().getValue("Bundle-Version"); if (StringUtils.isNotBlank(version)) { return getVersionFromString(version); } } else { //try to get the version from the file name return getVersionFromString(jarFile.getName()); } } catch (Exception e) { if (LOG.isErrorEnabled()) LOG.error("Unable to extract version from [#0], defaulting to '1.0.0'", url.toExternalForm()); } } return "1.0.0"; }
From source file:net.cliseau.composer.javatarget.PointcutParseException.java
/** * Update the manifest of a given JAR file to include CliSeAu's dependencies in the classpath list. * * This method modifies the "Class-Path" entry of the given JAR file's * manifest to include the paths of all runtime dependencies that are caused * by the instrumentation with the CliSeAu unit. * * @param targetJARFile The JAR file whose manifest to update. * @exception IOException Thrown when reading or writing the JAR file fails. * @todo Check whether this update is possible also with the JarFile API alone. *///ww w.jav a 2 s .c o m private void updateTargetManifest(final File targetJARFile) throws IOException, InvalidConfigurationException { // Step 1: Obtain the existing class path list from the target JAR file JarFile targetJAR = new JarFile(targetJARFile); Manifest targetManifest = targetJAR.getManifest(); LinkedList<String> classPathEntries; if (targetManifest != null) { String targetClassPath = targetManifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); if (targetClassPath == null) { targetClassPath = ""; } classPathEntries = new LinkedList<String>( Arrays.asList(targetClassPath.split(manifestClassPathSeparator))); } else { classPathEntries = new LinkedList<String>(); } // close the object again (this shall ensure that the command in // Step 4 can safely work on the file again) targetJAR.close(); // Step 2: Add all newly introduced runtime dependencies of CliSeAu classPathEntries.addAll(getInlinedDependencies()); // Step 3: Create a new manifest file with *only* the updated class path directive File manifestUpdate = File.createTempFile("MANIFEST", ".MF"); PrintWriter muWriter = new PrintWriter(manifestUpdate); muWriter.print("Class-path:"); muWriter.print(StringUtils.join(classPathEntries, manifestClassPathSeparator)); muWriter.println(); muWriter.close(); // Step 4: Run "jar" to update the JAR file with the new manifest; this // does not replace the JAR file's manifest with the new one, but // *update* *only* those entries in the JAR file's manifest which are // present in the new manifest. That is, only the class path settings are // updated and everything else remains intact. CommandRunner.exec(new String[] { aspectjConfig.getJarExecutable(), "umf", // update manifest manifestUpdate.getPath(), targetJARFile.getPath() }); // Step 5: cleanup manifestUpdate.delete(); }
From source file:org.nuclos.server.customcode.codegenerator.NuclosJavaCompilerComponent.java
public synchronized boolean validate() throws NuclosCompileException { if (forceCompile) { try {/*from ww w .ja v a 2 s . co m*/ compile(); } finally { forceCompile = false; } } else if (JARFILE.exists()) { try { JarFile jar = new JarFile(JARFILE); if (!jar.getManifest().equals(getManifest())) { compile(); } else { return false; } } catch (IOException e) { LOG.debug("validate: " + e); compile(); } } else { compile(); } return true; }
From source file:com.orange.mmp.dao.flf.ModuleDaoFlfImpl.java
public Module[] find(Module module) throws MMPDaoException { if (module == null) { throw new MMPDaoException("missing or bad data access object"); }/*from w ww.j av a 2 s .c o m*/ FilenameFilter jarFilter = new SuffixFileFilter(".jar"); File widgetsFiles[] = new File(this.path).listFiles(jarFilter); if (widgetsFiles == null) return new Module[0]; ArrayList<Module> list = new ArrayList<Module>(); JarFile jarFile = null; try { for (File found : widgetsFiles) { jarFile = new JarFile(found); Manifest manifest = jarFile.getManifest(); boolean match = (module.getId() == null && module.getLocation() == null && module.getVersion() == null && module.getName() == null && module.getCategory() == null); if (!match) { boolean goOnchecking = true; if (module.getLocation() != null && goOnchecking) { match = found.toURI().toString().equals(module.getLocation().toString()); goOnchecking = false; } if (module.getId() != null) { match = (manifest.getMainAttributes().getValue(MODULE_ID_HEADER) != null && manifest.getMainAttributes().getValue(MODULE_ID_HEADER).equals(module.getId())); goOnchecking = false; } if (module.getVersion() != null && goOnchecking) { match = (manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER) != null && manifest .getMainAttributes().getValue(MODULE_VERSION_HEADER).equals(module.getVersion())); goOnchecking = match; } if (module.getName() != null && goOnchecking) { match = (manifest.getMainAttributes().getValue(MODULE_NAME_HEADER) != null && manifest .getMainAttributes().getValue(MODULE_NAME_HEADER).equals(module.getName())); goOnchecking = match; } if (module.getCategory() != null && goOnchecking) { match = (manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER) != null && manifest .getMainAttributes().getValue(MODULE_CATEGORY_HEADER).equals(module.getCategory())); goOnchecking = match; } } if (match) { Module foundModule = new Module(); foundModule.setId(manifest.getMainAttributes().getValue(MODULE_ID_HEADER)); foundModule.setName(manifest.getMainAttributes().getValue(MODULE_NAME_HEADER)); foundModule .setVersion(new Version(manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER))); foundModule.setCategory(manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER)); foundModule.setLastModified(found.lastModified()); foundModule.setLocation(found.toURI()); list.add(foundModule); } jarFile.close(); } } catch (IOException ioe) { throw new MMPDaoException("failed to load module"); } finally { try { if (jarFile != null) jarFile.close(); } catch (IOException ioe) { //Nop just log } } Module[] modulesList = new Module[list.size()]; return list.toArray(modulesList); }
From source file:org.gradle.util.TestFile.java
public Manifest getManifest() { assertIsFile();//from ww w. j a v a 2 s .c o m try { JarFile jarFile = new JarFile(this); try { return jarFile.getManifest(); } finally { jarFile.close(); } } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java
@Test public void testDefaultPackaging() throws MojoExecutionException, IOException { WebJarPackager packager = new WebJarPackager(); packager.project = mock(MavenProject.class); packager.projectHelper = mock(MavenProjectHelper.class); when(packager.project.getArtifactId()).thenReturn("test"); when(packager.project.getVersion()).thenReturn("1.1"); when(packager.project.getBasedir()).thenReturn(fake); packager.buildDirectory = new File("target/junk"); copy();// w ww.java2s.c om packager.packageWebJar = true; packager.deployWebJarToWisdom = true; packager.execute(); final File wj = new File(packager.buildDirectory, "test-1.1-webjar.jar"); assertThat(wj).isFile(); JarFile jar = new JarFile(wj); assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/less/style.less")).isNotNull(); assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/missing")).isNull(); assertThat(jar.getEntry(WebJarPackager.ROOT + "test/1.1/coffee/script.coffee")).isNotNull(); Attributes attributes = jar.getManifest().getMainAttributes(); assertThat(attributes.getValue("Webjar-Name")).isEqualTo("test"); assertThat(attributes.getValue("Webjar-Version")).isEqualTo("1.1"); }
From source file:org.springframework.boot.loader.tools.Repackager.java
private Manifest buildManifest(JarFile source) throws IOException { Manifest manifest = source.getManifest(); if (manifest == null) { manifest = new Manifest(); manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); }/* w w w . jav a 2 s . co m*/ manifest = new Manifest(manifest); String startClass = this.mainClass; if (startClass == null) { startClass = manifest.getMainAttributes().getValue(MAIN_CLASS_ATTRIBUTE); } if (startClass == null) { startClass = findMainMethodWithTimeoutWarning(source); } String launcherClassName = this.layout.getLauncherClassName(); if (launcherClassName != null) { manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName); if (startClass == null) { throw new IllegalStateException("Unable to find main class"); } manifest.getMainAttributes().putValue(START_CLASS_ATTRIBUTE, startClass); } else if (startClass != null) { manifest.getMainAttributes().putValue(MAIN_CLASS_ATTRIBUTE, startClass); } String bootVersion = getClass().getPackage().getImplementationVersion(); manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion); manifest.getMainAttributes().putValue(BOOT_CLASSES_ATTRIBUTE, (this.layout instanceof RepackagingLayout) ? ((RepackagingLayout) this.layout).getRepackagedClassesLocation() : this.layout.getClassesLocation()); String lib = this.layout.getLibraryDestination("", LibraryScope.COMPILE); if (StringUtils.hasLength(lib)) { manifest.getMainAttributes().putValue(BOOT_LIB_ATTRIBUTE, lib); } return manifest; }
From source file:org.hyperic.hq.plugin.tomcat.TomcatServerDetector.java
private boolean isCorrectVersion(String versionJar) { boolean correctVersion = false; try {// www.j av a 2 s .c om JarFile jarFile = new JarFile(versionJar); log.debug("[isInstallTypeVersion] versionJar='" + jarFile.getName() + "'"); Attributes attributes = jarFile.getManifest().getMainAttributes(); jarFile.close(); String tomcatVersion = attributes.getValue("Specification-Version"); String expectedVersion = getTypeProperty("tomcatVersion"); if (expectedVersion == null) { expectedVersion = getTypeInfo().getVersion(); } log.debug("[isInstallTypeVersion] tomcatVersion='" + tomcatVersion + "' (" + expectedVersion + ")"); correctVersion = tomcatVersion.equals(expectedVersion); } catch (IOException e) { log.debug("Error getting Tomcat version (" + e + ")", e); } return correctVersion; }
From source file:com.orange.mmp.dao.flf.ModuleDaoFlfImpl.java
@SuppressWarnings("unchecked") public Module createOrUdpdate(Module module) throws MMPDaoException { if (module == null || module.getLocation() == null) { throw new MMPDaoException("missing or bad data access object"); }/*from w w w . ja va2 s . c o m*/ JarFile jarFile = null; try { this.lock.lock(); jarFile = new JarFile(new File(module.getLocation())); Manifest manifest = jarFile.getManifest(); if (manifest == null) { throw new MMPDaoException("invalid module archive, MANIFEST file not found"); } String symbolicName = manifest.getMainAttributes().getValue(MODULE_ID_HEADER); if (manifest.getMainAttributes().getValue(MODULE_ID_HEADER) != null) { if (module instanceof Widget) { String[] widgetId = symbolicName.split(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN); module.setId(widgetId[0]); } else { module.setId(symbolicName); } } else throw new MMPDaoException("invalid module archive, missing " + MODULE_ID_HEADER + " header"); if (manifest.getMainAttributes().getValue(MODULE_NAME_HEADER) != null) { module.setName(manifest.getMainAttributes().getValue(MODULE_NAME_HEADER)); } else throw new MMPDaoException("invalid module archive, missing " + MODULE_NAME_HEADER + " header"); if (manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER) != null) { module.setVersion(new Version(manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER))); } else throw new MMPDaoException("invalid module archive, missing " + MODULE_VERSION_HEADER + " header"); if (manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER) != null) { module.setCategory(manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER)); } else module.setCategory(Constants.MODULE_CATEGORY_LIBRARY); File moduleFile = new File(module.getLocation()); File dstFile; if (module instanceof Widget) { String[] nameAndBranch = symbolicName.split(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN); if (nameAndBranch.length > 1) dstFile = new File(this.path, nameAndBranch[0].concat(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN) .concat(nameAndBranch[1]).concat(".jar")); else { String defaultBranchId = null; Branch defaultBranch = new Branch(); defaultBranch.setDefault(true); Branch defaultBranchesResult[] = (Branch[]) DaoManagerFactory.getInstance().getDaoManager() .getDao("branch").find(defaultBranch); if (defaultBranchesResult.length > 0) { defaultBranchId = defaultBranchesResult[0].getId(); } defaultBranch = defaultBranchesResult[0]; dstFile = new File(this.path, nameAndBranch[0].concat(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN) .concat(defaultBranchId).concat(".jar")); } } else { dstFile = new File(this.path, symbolicName.concat(".jar")); } FileUtils.copyFile(moduleFile, dstFile); module.setLocation(dstFile.toURI()); module.setLastModified(dstFile.lastModified()); jarFile.close(); FileUtils.touch(new File(this.path)); return module; } catch (IOException ioe) { throw new MMPDaoException("failed to add module : " + ioe.getMessage()); } finally { try { if (jarFile != null) jarFile.close(); } catch (IOException ioe) { //Nop just log } this.lock.unlock(); } }
From source file:org.wisdom.maven.mojos.WebJarPackagerTest.java
@Test public void testNameVersionAndClassifierCustomization() throws MojoExecutionException, IOException { WebJarPackager packager = new WebJarPackager(); packager.project = mock(MavenProject.class); packager.projectHelper = mock(MavenProjectHelper.class); when(packager.project.getArtifactId()).thenReturn("test"); when(packager.project.getVersion()).thenReturn("1.0"); when(packager.project.getBasedir()).thenReturn(fake); packager.buildDirectory = new File("target/junk"); copy();//from w w w . j a v a2 s . co m packager.webjar = new WebJar(); packager.webjar.setName("library"); packager.webjar.setVersion("2.0"); packager.webjar.setClassifier("wb"); packager.execute(); final File wj = new File(packager.buildDirectory, "library-2.0-wb.jar"); assertThat(wj).isFile(); JarFile jar = new JarFile(wj); assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/missing")).isNull(); assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/coffee/script.coffee")).isNotNull(); assertThat(jar.getEntry(WebJarPackager.ROOT + "library/2.0/less/style.less")).isNotNull(); Attributes attributes = jar.getManifest().getMainAttributes(); assertThat(attributes.getValue("Webjar-Name")).isEqualTo("library"); assertThat(attributes.getValue("Webjar-Version")).isEqualTo("2.0"); }