2010-08-03 53 views

回答

4

THX到麦克道尔的上述建议,改进的init()变为:

@PostConstruct 
public void init() { 
    try { 
     InputStream is = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/META-INF/MANIFEST.MF"); 
     manifest = new Manifest(); 
     manifest.read(is); 
    } catch (IOException ioe) { 
     logger.error("Unable to read the Manifest file from classpath.", ioe); 
    } 
} 
+0

当这样做时:InputStream为null – kwisatz 2015-11-24 11:41:23

0

以下似乎工作:

创建ManagedBean:

@ManagedBean(name = "cic") 
@ApplicationScoped 
public class ConfigurationInformationController { 
    private static final Logger logger = LoggerFactory.getLogger(ConfigurationInformationController.class.getName()); 
    private Manifest manifest = null; 
    public Manifest getManifest() { 
     return manifest; 
    } 
    @PostConstruct 
    public void init() { 
     File manifestFile = null; 
     try { 
      String home = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/"); 
      manifestFile = new File(home, "META-INF/MANIFEST.MF"); 
      manifest = new Manifest(); 
      manifest.read(new FileInputStream(manifestFile)); 
     } catch (IOException ioe) { 
      logger.error("Unable to read the Manifest file from '"+manifestFile.getAbsolutePath()+"'",ioe); 
     } 
    } 
} 

在Facelets的页面,从那里调用属性...

<h:outputLabel for="Build-BuildTimestamp" value="Build Timestamp:"/> 
    <h:outputText id="Build-BuildTimestamp" value="#{cic.manifest.mainAttributes.getValue('Build-BuildTimestamp')}"/> 
+0

幸运的是它的工作原理。虽然,通过File对象的往返感觉有点脆弱。有更好的建议吗? – Jan 2010-08-03 13:01:07

+3

您不能指望MANIFEST.MF文件被解压缩。它应该可以通过类路径作为资源进行访问。 – 2010-08-03 13:35:21

+1

使用'ExternalContext.getResource *'而不是'File'。 http://download.oracle.com/javaee/5/api/javax/faces/context/ExternalContext.html @ThorbjørnRavn Andersen - 如果你使用'ClassLoader.getResource(...'你会得到哪个清单首先?可能会在WEB-INF/lib和应用程序服务器库中的每个JAR文件中出现。 – McDowell 2010-08-03 15:10:45