Is there any way for a class in war to access META-INF from the ear?

This need may seem a little confusing, and if so, I am open to suggestions regarding best implementation practices. My problem is as follows. I have a WAR web application that is contained in an EAR. Everything was stunned. From inside my webapp, I am trying to show artifact ids and ear and war version numbers.

War is a fairly simple case. I can use simple maven filtering to enter the required artifactId / versionId file into the war as it is created. However, the ear is harder.

I know that there is META-INF / maven /// pom.properties that I can look at in the ear, which contains this information, but I do not see access to it.

I tried (from inside the jsp page) the following without success (all with and without lead /); all calls return null:

getClass().getClassLoader().getResource( "/META-INF/maven/<group>/<artifact>/pom.properties"); getClass().getClassLoader().getResourceAsStream( "/META-INF/maven/<group>/<artifact>/pom.properties"); 

Is this possible with a class loader? Or does it depend on the configuration of the class loader? Is there a better way to get this information?

I am currently running tests on JBoss, but the final deployment will be in WebSphere. However, ideally, I would like the solution to be server-independent.

Thanks!

Eric

+4
source share
2 answers

I don’t think this is possible because EAR, EJB and WARs have different class loaders that are not related to each other according to the universal, predictable and Java EE-wide scheme.

I recently ran into a similar problem, and the only acceptable solution I found was to specify env-entry in application.xml and read it using @Resource injection:

 <application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6"> <!-- usual stuff goes here --> <env-entry> <description>application-wide property</description> <env-entry-name>java:app/env/AppWideProperty</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>stackoverflow.com</env-entry-value> </env-entry> </application> 

and then in any class:

 public class CalcResultCachePool { @Resource(lookup="java:app/env/AppWideProperty") String appWideProperty; // // } 
+5
source

Have you tried using ContextClassLoader related artifacts? In web applications, ContextClassLoader is the preferred way to retrieve files from the deployed path to the application classes.

 Thread.currentThread().getContextClassLoader().getResourceAsStream("/META-INF/maven/<group>/<artifact>/pom.properties"); 

If you pack this code in both the WAR file and the JAR file in the EAR, this may return the correct information to you depending on the class loader.

Disclaimer: this may not work on all application servers the same way, as some application servers use a unified class loader for web applications.

+1
source

Source: https://habr.com/ru/post/1403744/


All Articles