No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Vous devez être membre et vous identifier pour publier un article.
Les visiteurs peuvent toutefois commenter chaque article par une réponse.

[maven] Utiliser les infos de maven dans une classe Java

Astuces de l’Infobrol (Java)Article publié le 20/12/2008 12:00:23


Dans mes applications Java, j’utilise différents jars qui contiennent de nombreux codes dont j’ai souvent besoin. Maven est vraiment très pratique pour gérer les dépendances, et pour me générer automatiquement un jar exécutable.

Seulement, que ce soit pour une application exécutable ou un simple jar dont dépendent d’autres applications, il est bon de pouvoir afficher un "A propos" qui nous retourne certaines informations, comme le numéro de version.

Puisque toutes ces informations se trouvent dans un pom.xml (Project Object Model), il serait redondant (et peu souple au niveau de la maintenance) de retrouver ces informations dans un fichier properties ou pire encore, codées en dur.

Ma première tentative fut d’utiliser le fichier que maven génère dans le jar :
  1. package be.gaudry.model.config;
  2.  
  3. import java.util.Properties;
  4.  
  5.  
  6. /**
  7.  * @date 20 déc. 2008
  8.  * @author Steph GAUDRY
  9.  *
  10.  */
  11. public class POMHelper {
  12. private static Properties props;
  13. static{
  14. props = new Properties();
  15. try {
  16. props.load(POMHelper.class.getClassLoader().getResourceAsStream("META-INF/maven/be.gaudry.broldev/broldev.core.model/pom.properties"));
  17. } catch(Exception e){
  18.  
  19. }
  20. }
  21. /**
  22. *
  23. * @return the version or null
  24. */
  25. public static String getVersion(){
  26. return props.getProperty("version");
  27. }
  28. /**
  29. *
  30. * @return the groupId or null
  31. */
  32. public static String getGroupId(){
  33. return props.getProperty("groupId");
  34. }
  35. /**
  36. *
  37. * @return the artifactId or null
  38. */
  39. public static String getArtifactId(){
  40. return props.getProperty("artifactId");
  41. }
  42. }


Nous pouvons ensuite lier avec un fichier de langue le message correct en fonction que la méthode retourne null, ou un String.

Mais cela ne me convenait pas, alors j’ai cherché du côté de maven s’il pouvait faire le travail pour moi, et il suffit d’ajouter une ligne dans le pom pour mettre ces informations à disposition lors de la commande maven "install" ou "assembly" :
  1. ‹!--
  2. Adds a Class-Path entry to the manifest to do an executable jar
  3. --›
  4. ‹plugin›
  5. ‹groupId›org.apache.maven.plugins‹/groupId›
  6. ‹artifactId›maven-jar-plugin‹/artifactId›
  7. ‹configuration›
  8. ‹archive›
  9. ‹manifest›
  10. ‹addClasspath›true‹/addClasspath›
  11. ‹mainClass›
  12. be.gaudry.about.AboutBrolDevModel
  13. ‹/mainClass›
  14. ‹!--
  15. Maven already provides a hook to have the version number written to a jars MANIFEST.MF
  16. This instructs Maven to add the following headers to the MANIFEST.MF of your jar:
  17.  
  18. Implementation-Title: display-version
  19. Implementation-Version: 1.0-SNAPSHOT
  20. Implementation-Vendor-Id: test
  21.  
  22. --›
  23. ‹addDefaultImplementationEntries›true‹/addDefaultImplementationEntries›
  24. ‹/manifest›
  25. ‹/archive›
  26. ‹/configuration›
  27. ‹/plugin›


Il suffit ensuite d’invoquer la méthode suivante :
  1. POMHelper.class.getPackage().getImplementationVersion();

Vous pouvez remplacer POMHelper par le nom de votre classe.

Avatar :: Steph Un article de Steph

Source : indéterminée


Sélection, tri et recherche d'articles
FILTRER :
TRIER :1er critère : 2e critère :
CHERCHER : Dans les titres Dans le contenu


[Afficher les liens en fonction des critères du formulaire ci-dessus]

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 13/09/2004, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/ast-rf-447.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.