jcabi / jcabi-manifests

Java library for convenient reading of MANIFEST.MF files available in classpath
https://manifests.jcabi.com
Other
60 stars 22 forks source link

Servlet Environment-- Reading Manifest.mf #11

Closed nandhusriram closed 9 years ago

nandhusriram commented 9 years ago

I am trying to read the variable "Class-Path" added by the maven archiver to the manifest and it looks like Manifests.read("Class-Path") showing me entirely unexpected results , any ideas would be greatly appreciated?

yegor256 commented 9 years ago

@nandhusriram can you show your java code?

nandhusriram commented 9 years ago

My Servlet Listener

public void contextInitialized(ServletContextEvent event)
    {
        try 
        {
            LOG.debug("contextInitialized called {} ",event);
            ServletContext sc = event.getServletContext();
            Manifests.DEFAULT.append((new ServletMfs(sc)));
            LOG.debug("adding servlet  {} ",sc);
            }catch (Exception ex) {}

My Manifest reader

if(Manifests.exists("Class-Path")) versionTextBuilder.append( String.format("Class-Path is  %s %n",Manifests.read("Class-Path")));

Both of the java files are in a jar file that gets deployed with war , I could paste the Manifest if you need one.

yegor256 commented 9 years ago

Do you have this file in your WAR: WEB-INF/classes/META-INF/MANIFEST.MF?

nandhusriram commented 9 years ago

not under classes warname/META-INF/MANIFEST.MF

yegor256 commented 9 years ago

In this case, you should also add this:

Manifests.DEFAULT.append(new ClasspathMfs());
nandhusriram commented 9 years ago

let me try that real quick

nandhusriram commented 9 years ago

still same issue

nandhusriram commented 9 years ago

is there any order for appending the Mfs , I added ServletMfs and then ClassPath

yegor256 commented 9 years ago

Nope, the order doesn't matter. If none of them can find anything, it means that you don't have MANIFEST.MF available in your JAR/WAR. Are you sure you have the file available?

nandhusriram commented 9 years ago

Yes I am looking at the exploded war and its there , its finding other variables and even for the variable I am interested in , its finding that not the correct one, looks to me its finding the variable "class-path" from some other manifest.mf

yegor256 commented 9 years ago

Ah, sure, there could be many duplicates of the same attribute in multiple files. jcabi-manifests gets the first one available.

nandhusriram commented 9 years ago

how could I force to load mine first , if thats even a possibility ?

yegor256 commented 9 years ago

How about this:

for (InputStream stream : new ClasspathMfs().fetch()) {
  Manifest manifest = new Manifest(stream);
  Attributes attrs = manifest.getMainAttributes();
  for (final Object key : attrs.keySet()) {
    String value = attrs.getValue(Attributes.Name.class.cast(key));
    System.out.println("key: " + key + ", value: " + value);
  }
}
yegor256 commented 9 years ago

this snippet will load all available key/value pairs from all available MANIFEST.MF in classpath. I don't know how to find "yours" there though :)

nandhusriram commented 9 years ago

Thanks , I am looking at maven archiver plugin of how to change the name of variable class-path when this is invoked, if I cannot figure that out , I might add a prefix to my values , so I can parse it

configuration>

true
    </configuration>