java.util.jar.Manifest.getMainAttributes()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(294)

本文整理了Java中java.util.jar.Manifest.getMainAttributes()方法的一些代码示例,展示了Manifest.getMainAttributes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Manifest.getMainAttributes()方法的具体详情如下:
包路径:java.util.jar.Manifest
类名称:Manifest
方法名:getMainAttributes

Manifest.getMainAttributes介绍

[英]Returns the main Attributes of the JarFile.
[中]返回文件的主要属性。

代码示例

代码示例来源:origin: jenkinsci/jenkins

  1. private String getVersionOf(Manifest manifest) {
  2. String v = manifest.getMainAttributes().getValue("Plugin-Version");
  3. if(v!=null) return v;
  4. // plugins generated before maven-hpi-plugin 1.3 should still have this attribute
  5. v = manifest.getMainAttributes().getValue("Implementation-Version");
  6. if(v!=null) return v;
  7. return "???";
  8. }

代码示例来源:origin: jenkinsci/jenkins

  1. protected String identifyPluginShortName(File t) {
  2. try {
  3. JarFile j = new JarFile(t);
  4. try {
  5. String name = j.getManifest().getMainAttributes().getValue("Short-Name");
  6. if (name!=null) return name;
  7. } finally {
  8. j.close();
  9. }
  10. } catch (IOException e) {
  11. LOGGER.log(WARNING, "Failed to identify the short name from "+t,e);
  12. }
  13. return FilenameUtils.getBaseName(t.getName()); // fall back to the base name of what's uploaded
  14. }

代码示例来源:origin: gocd/gocd

  1. private static String defaultMainClassName(JarFile jarFile) throws IOException {
  2. return jarFile.getManifest().getMainAttributes().getValue("GoCD-Main-Class");
  3. }
  4. }

代码示例来源:origin: skylot/jadx

  1. public static String getVersion() {
  2. try {
  3. ClassLoader classLoader = Jadx.class.getClassLoader();
  4. if (classLoader != null) {
  5. Enumeration<URL> resources = classLoader.getResources("META-INF/MANIFEST.MF");
  6. while (resources.hasMoreElements()) {
  7. Manifest manifest = new Manifest(resources.nextElement().openStream());
  8. String ver = manifest.getMainAttributes().getValue("jadx-version");
  9. if (ver != null) {
  10. return ver;
  11. }
  12. }
  13. }
  14. } catch (Exception e) {
  15. LOG.error("Can't get manifest file", e);
  16. }
  17. return "dev";
  18. }
  19. }

代码示例来源:origin: jenkinsci/jenkins

  1. private static void parseClassPath(Manifest manifest, File archive, List<File> paths, String attributeName, String separator) throws IOException {
  2. String classPath = manifest.getMainAttributes().getValue(attributeName);
  3. if(classPath==null) return; // attribute not found
  4. for (String s : classPath.split(separator)) {
  5. File file = resolve(archive, s);
  6. if(file.getName().contains("*")) {
  7. // handle wildcard
  8. FileSet fs = new FileSet();
  9. File dir = file.getParentFile();
  10. fs.setDir(dir);
  11. fs.setIncludes(file.getName());
  12. for( String included : fs.getDirectoryScanner(new Project()).getIncludedFiles() ) {
  13. paths.add(new File(dir,included));
  14. }
  15. } else {
  16. if(!file.exists())
  17. throw new IOException("No such file: "+file);
  18. paths.add(file);
  19. }
  20. }
  21. }

代码示例来源:origin: stackoverflow.com

  1. Manifest manifest = new Manifest();
  2. manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
  3. JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);
  4. add(new File("inputDirectory"), target);
  5. target.close();

代码示例来源:origin: apache/hive

  1. /**
  2. * Loads the manifest attributes from the jar.
  3. *
  4. * @throws java.net.MalformedURLException
  5. * @throws IOException
  6. */
  7. private static synchronized void loadManifestAttributes() throws IOException {
  8. if (manifestAttributes != null) {
  9. return;
  10. }
  11. Class<?> clazz = HiveDriver.class;
  12. String classContainer = clazz.getProtectionDomain().getCodeSource()
  13. .getLocation().toString();
  14. URL manifestUrl = new URL("jar:" + classContainer
  15. + "!/META-INF/MANIFEST.MF");
  16. Manifest manifest = new Manifest(manifestUrl.openStream());
  17. manifestAttributes = manifest.getMainAttributes();
  18. }

代码示例来源:origin: aws/aws-sdk-java

  1. private static String kotlinVersionByJar() {
  2. StringBuilder kotlinVersion = new StringBuilder("");
  3. JarInputStream kotlinJar = null;
  4. try {
  5. Class<?> kotlinUnit = Class.forName("kotlin.Unit");
  6. kotlinVersion.append("kotlin");
  7. kotlinJar = new JarInputStream(kotlinUnit.getProtectionDomain().getCodeSource().getLocation().openStream());
  8. String version = kotlinJar.getManifest().getMainAttributes().getValue("Implementation-Version");
  9. concat(kotlinVersion, version, "/");
  10. } catch (ClassNotFoundException e) {
  11. //Ignore
  12. } catch (Exception e) {
  13. if (log.isTraceEnabled()) {
  14. log.trace("Exception attempting to get Kotlin version.", e);
  15. }
  16. } finally {
  17. closeQuietly(kotlinJar, log);
  18. }
  19. return kotlinVersion.toString();
  20. }

代码示例来源:origin: scouter-project/scouter

  1. public static void print() throws IOException {
  2. InputStream manifestStream = Thread.currentThread().getContextClassLoader()
  3. .getResourceAsStream("META-INF/MANIFEST.MF");
  4. try {
  5. Manifest manifest = new Manifest(manifestStream);
  6. Attributes attributes = manifest.getMainAttributes();
  7. String impVersion = attributes.getValue("Implementation-Version");
  8. System.out.println(attributes);
  9. } catch (IOException ex) {
  10. ex.printStackTrace();
  11. }
  12. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * {@inheritDoc}
  3. */
  4. public File toJar(File file) throws IOException {
  5. Manifest manifest = new Manifest();
  6. manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, MANIFEST_VERSION);
  7. return toJar(file, manifest);
  8. }

代码示例来源:origin: pxb1988/dex2jar

  1. Manifest input = jar.getManifest();
  2. Manifest output = new Manifest();
  3. Attributes main = output.getMainAttributes();
  4. if (input != null) {
  5. main.putAll(input.getMainAttributes());

代码示例来源:origin: robovm/robovm

  1. /**
  2. * Gets all attributes of the manifest file referenced by this {@code
  3. * JarURLConnection}. If this instance refers to a JAR-file rather than a
  4. * JAR-file entry, {@code null} will be returned.
  5. *
  6. * @return the attributes of the manifest file or {@code null}.
  7. * @throws IOException
  8. * if an I/O exception occurs while retrieving the {@code
  9. * JarFile}.
  10. */
  11. public Attributes getMainAttributes() throws java.io.IOException {
  12. Manifest m = getJarFile().getManifest();
  13. return (m == null) ? null : m.getMainAttributes();
  14. }
  15. }

代码示例来源:origin: stackoverflow.com

  1. Class clazz = MyClass.class;
  2. String className = clazz.getSimpleName() + ".class";
  3. String classPath = clazz.getResource(className).toString();
  4. if (!classPath.startsWith("jar")) {
  5. // Class not from JAR
  6. return;
  7. }
  8. String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +
  9. "/META-INF/MANIFEST.MF";
  10. Manifest manifest = new Manifest(new URL(manifestPath).openStream());
  11. Attributes attr = manifest.getMainAttributes();
  12. String value = attr.getValue("Manifest-Version");

代码示例来源:origin: gocd/gocd

  1. public static String getCruiseVersion(String jar) {
  2. String version = null;
  3. try {
  4. JarFile jarFile = new JarFile(jar);
  5. Manifest manifest = jarFile.getManifest();
  6. if (manifest != null) {
  7. Attributes attributes = manifest.getMainAttributes();
  8. version = attributes.getValue("Go-Version");
  9. }
  10. } catch (IOException e) {
  11. }
  12. return version;
  13. }

代码示例来源:origin: redisson/redisson

  1. Attributes attributes = manifest.getMainAttributes();
  2. if (attributes == null) {
  3. return;
  4. String s = attributes.getValue(Attributes.Name.CLASS_PATH);
  5. if (s == null) {
  6. return;
  7. file = new File(base, t);
  8. file = file.getCanonicalFile();
  9. if (!file.exists()) {
  10. file = new File(t);
  11. file = file.getCanonicalFile();
  12. if (!file.exists()) {
  13. URL url = new URL(t);
  14. file = new File(url.getFile());
  15. file = file.getCanonicalFile();
  16. if (!file.exists()) {

代码示例来源:origin: apache/geode

  1. Path locationPath = new File(location).getCanonicalFile().toPath();
  2. Files.createDirectories(locationPath);
  3. Manifest manifest = new Manifest();
  4. Attributes global = manifest.getMainAttributes();
  5. global.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
  6. global.put(new Attributes.Name("Class-Path"), String.join(" ", manifestEntries));

代码示例来源:origin: chewiebug/GCViewer

  1. /**
  2. * Returns Manifest-Attributes for MANIFEST.MF, if running for a .jar file
  3. *
  4. * @return Manifest Attributes (may be empty but never null)
  5. * @throws IOException If something went wrong finding the MANIFEST file
  6. * @see <a href="http://stackoverflow.com/a/1273432">stackoverflow article</a>
  7. */
  8. private static Attributes getAttributes() throws IOException {
  9. Class clazz = BuildInfoReader.class;
  10. String className = clazz.getSimpleName() + ".class";
  11. String classPath = clazz.getResource(className).toString();
  12. if (!classPath.startsWith("jar")) {
  13. // Class not from JAR
  14. return new Attributes(0);
  15. }
  16. String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + FILE_NAME;
  17. Manifest manifest = new Manifest(new URL(manifestPath).openStream());
  18. return manifest.getMainAttributes();
  19. }

代码示例来源:origin: jenkinsci/jenkins

  1. static String computeShortName(Manifest manifest, String fileName) {
  2. // use the name captured in the manifest, as often plugins
  3. // depend on the specific short name in its URLs.
  4. String n = manifest.getMainAttributes().getValue("Short-Name");
  5. if(n!=null) return n;
  6. // maven seems to put this automatically, so good fallback to check.
  7. n = manifest.getMainAttributes().getValue("Extension-Name");
  8. if(n!=null) return n;
  9. // otherwise infer from the file name, since older plugins don't have
  10. // this entry.
  11. return getBaseName(fileName);
  12. }

代码示例来源:origin: scouter-project/scouter

  1. public static void print() throws IOException {
  2. InputStream manifestStream = Thread.currentThread().getContextClassLoader()
  3. .getResourceAsStream("META-INF/MANIFEST.MF");
  4. try {
  5. Manifest manifest = new Manifest(manifestStream);
  6. Attributes attributes = manifest.getMainAttributes();
  7. String impVersion = attributes.getValue("Implementation-Version");
  8. System.out.println(attributes);
  9. } catch (IOException ex) {
  10. ex.printStackTrace();
  11. }
  12. }

代码示例来源:origin: gocd/gocd

  1. private static String getManifestKey(JarFile jarFile, String key) {
  2. try {
  3. Manifest manifest = jarFile.getManifest();
  4. if (manifest != null) {
  5. Attributes attributes = manifest.getMainAttributes();
  6. return attributes.getValue(key);
  7. }
  8. } catch (IOException e) {
  9. LOG.error("Exception while trying to read key {} from manifest of {}", key, jarFile.getName(), e);
  10. }
  11. return null;
  12. }

相关文章