1

I have a maven project with several dependencies that are developed internally by my team. I need some information contained on the MANIFEST.mf files of those dependencies at build time of my current project.

Just to be clear. I don't want to get information from my own MANIFEST.mf file (the one from the project I am building). I want to access information contained inside the MANIFEST.mf files of the dependencies of my project at build time.

I built a maven plugin where I can check the dependency tree and retrieve some basic information about the dependencies, however I haven't found a straight forward way of getting to the MANIFEST.mf files.

Do you have any clues on how I can access them (programatically)?

Thanks!

omrsin
  • 568
  • 10
  • 18
  • Have you checked this http://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest? – Nicolas Filotto Jan 12 '17 at 17:22
  • Possible duplicate of [Reading my own Jar's Manifest](http://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest) – lane.maxwell Jan 12 '17 at 17:23
  • If you really using a Maven project you don't need to access the MANIFEST.MF file for dependencies cause they are located in the pom.xml file. But it sounds like you are working in OSGi project? Apart from that I would like to know what of purpose your plugin has? – khmarbaise Jan 12 '17 at 18:54
  • @NicolasFilotto my question does not refer to the MANIFEST.mf file of my project. I edited my question to avoid this misunderstanding. – omrsin Jan 13 '17 at 15:31
  • @lane.maxwell my question does not refer to the MANIFEST.mf file of my project. I edited my question to avoid this misunderstanding. – omrsin Jan 13 '17 at 15:32
  • @khmarbaise The information obtained from my pom file is insufficient. I am not working in an OSGi project. The information I need from the other manifest files is included by other teams and it will help me to have a clear view of specific properties of the used dependencies. – omrsin Jan 13 '17 at 15:35
  • See the informations Robert Scholte gave. So the questions is why the information in the pom file is insufficient? And what exactly you are trying to do? Furthermore you can take a look into the maven-dependency-plugin which has some goals to get the dependency tree but it's based on the pom file...As long as you don't give more information about what you are trying to achieve it's hard to guess what the right information to give...??? – khmarbaise Jan 14 '17 at 14:17
  • @khmarbaise I think I expressed it clearly. I would like to read some annotated information in the manifest file my in-house developed dependencies. This will be used for different things, among them security checks. – omrsin Jan 16 '17 at 08:52

2 Answers2

1

Use the file of the artifact to create a https://docs.oracle.com/javase/8/docs/api/java/util/jar/JarFile.html , which gives you access to the Manifest

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
  • This seems to be pointing in the right direction. However, how do you get the artifact file that you mention programatically. I am using org.apache.maven.artifact.Artifact to interact with the dependencies but I can't figure it out how to procure the jar file. – omrsin Jan 16 '17 at 08:46
  • Every artifact has a reference to a file: http://maven.apache.org/ref/3.3.9/maven-artifact/apidocs/org/apache/maven/artifact/Artifact.html#getFile() – Robert Scholte Jan 16 '17 at 09:02
0

Maybe something like this would work for you

public void readManifests() throws IOException {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    for(URL url: classLoader.getURLs()) {
        Manifest manifest = new JarFile(url.getFile()).getManifest();
    }
}
lane.maxwell
  • 5,002
  • 1
  • 20
  • 30