Unfortunately, this does not work when executing unit tests as the jar has not been created yet.
/Anders 2011/2/2 Lucas Persson <lucas.pers...@oracle.com> > I can not resist ;-) > this.getClass().getPackage().getImplementationVersion(); > > And you need to instruct maven jar plugin to include version info the > MANIFEST.MF > E.g. > <configuration> > <archive> > <manifest> > > <addDefaultImplementationEntries>true</addDefaultImplementationEntries> > </manifest> > </archive> > </configuration> > http://maven.apache.org/shared/maven-archiver/index.html > > Cheers > Lucas > > > On 02/02/2011 09:45 AM, Martin Schayna wrote: > > On 02/01/2011 10:23 PM, Wayne Fay wrote: > > How do I access the maven version from my java code. > > For what purpose, exactly? Are you building a Maven plugin, or simply > building some Java project with Maven? > > > I have done this by reading version number during runtime from MANIFEST.MF > file: > > import java.util.jar.Attributes; > import java.util.jar.Manifest; > > Manifest manifest = > (Manifest)app.getServletContext().getAttribute("MANIFEST"); > if (manifest == null) > try { > manifest = new > Manifest(app.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF")); > } catch (Exception e) { > manifest = new Manifest(); > } > String version = getAttribute(manifest, "Implementation-Version"); > > Just curious if this solution is ok or exists better one? > > Thanks, Martin Schayna. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@maven.apache.org > For additional commands, e-mail: users-h...@maven.apache.org > > > -- > [image: Oracle] <http://www.oracle.com> > Lucas Persson | Principal Member of Technical Staff > Phone: +4684773644 <tel:+4684773644> | | | Mobile: > +46730946656<tel:+46730946656> > Oracle Communications Platform > ORACLE Sweden | Folkungagatan 122 | 116 30 Stockholm > > Oracle Svenska AB, KronborgsgrÀnd 17, S-164 28 KISTA, reg.no. 556254-6746 > [image: Green Oracle] <http://www.oracle.com/commitment> Oracle is > committed to developing practices and products that help protect the > environment >