6

I want to access information like implementation version stored in the jar file. This seems to work quite well, in any technique but always based on the classloader.

If i want to test in my maven environment, of course i cannot use the surefire plugin, because this is prior to packaging in jar. Thus I must use failsafe plugin.

But then neither of the technique works, most probably, because of some dark magic concerning classloaders.

The simplest way to get implementation version is just

this.getClass().getPackage().getImplementationVersion()

which reads from META-INF/MANIFEST.MF in the jar which looks sth like

Name: eu/simuline/octave/
Extension-name: eu.simuline.octave
Specification-Version: 0.7
Implementation-Version: 0.7-SNAPSHOT

Maybe extension is not needed, what is needed is the section name which is derived from the package name in an obvious way (trailing slash seems vital;-) ). But as said, this works only in productive context, not within tests with failsafe plugin. Then the java code returns 0.7-SNAPSHOT, else it just returns null, which means according to the api docs, that the version is unknown... well.

What can I do to include meta info in maven tests???

user2609605
  • 419
  • 2
  • 14
  • You need to access the generated `jar` file in the `target` directory... only way... if you really need to test ... what exactly? – khmarbaise Nov 16 '20 at 11:56
  • you are right. I must access that. I think it is the classloader which shall access that. I think Package.getImplementationVersion() does nothing but relying on the lcassloader. As all pieces of information are in the MANIFEST.MF in the jar. So no need to access myself, i think, right? – user2609605 Nov 16 '20 at 12:05
  • @hkmarbaise: i want to check that the pieces of meta info i pass to the user via manifest are really as wanted and really present. As an example i want to pass certain versions and also other information tailored to the user. – user2609605 Nov 16 '20 at 12:12
  • The question which coming up is: What are you really testing? How have configured to get the meta information into the MANIFEST.MF file? via Maven? – khmarbaise Nov 16 '20 at 13:02
  • @khmarbaise, yes but this does not contribute to the solution, does it? It is only important what the jar looks like. – user2609605 Nov 19 '20 at 01:01
  • @khmarbaise: access the jar, yes, if i understood right, failsafe plugin accesses the jar, e.g. the class files in jar. but also hm... i would have bet also the manifest and all other material... but now i doubt. I just dont know. By the way: yes: maven. That can be seen from the fact that I talk about failsafe plugin. – user2609605 Nov 19 '20 at 01:05
  • So the only thing you need is the location of the jar file which is `target/xxx.jar` which you can open within your Test code (integration test) which should be named like `*IT.java` to be run via failsafe-plugin... then you can check whatever you like... – khmarbaise Nov 19 '20 at 07:20

1 Answers1

2

I did some research on the failsafe plugin. For example running with

mvn -X verify

unveils

INFO] --- maven-failsafe-plugin:3.0.0-M5:verify (run-tests) @ javaoctave ---
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-failsafe-plugin:3.0.0-M5:verify from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-failsafe-plugin:3.0.0-M5, parent: jdk.internal.loader.ClassLoaders$AppClassLoader@277050dc]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-failsafe-plugin:3.0.0-M5:verify' with basic configurator -->
[DEBUG]   (s) basedir = /home/ernst/OpenSource/OctaveInJavaGit/octaveInJava/javaoctave
[DEBUG]   (s) reportsDirectory = /home/ernst/OpenSource/OctaveInJavaGit/octaveInJava/javaoctave/target/failsafe-reports
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@1416a80a
[DEBUG]   (s) skip = false
[DEBUG]   (f) summaryFile = /home/ernst/OpenSource/OctaveInJavaGit/octaveInJava/javaoctave/target/failsafe-reports/failsafe-summary.xml
[DEBUG]   (s) testClassesDirectory = /home/ernst/OpenSource/OctaveInJavaGit/octaveInJava/javaoctave/target/test-classes
[DEBUG]   (s) testFailureIgnore = false
[DEBUG] -- end configuration --
[DEBUG] Failsafe report directory: /home/ernst/OpenSource/OctaveInJavaGit/octaveInJava/javaoctave/target/failsafe-reports

Note the testClassesDirectory. I thought that the plugin accesses the jar created in the preceeding lifecycle phase package
but obviously this is not true and there seems to be no way to do so. Thus all the stuff does not work.

I filed a feature/enhancement request to fix that.

No idea whether they like my idea.

user2609605
  • 419
  • 2
  • 14