Read maven project version from pom.properties in site.war

1,304 views
Skip to first unread message

Alexandru Ionescu

unread,
Jun 24, 2015, 9:29:36 AM6/24/15
to hippo-c...@googlegroups.com
Hi,

We are trying to use the Spring @Value annotation to read the deployed version from the artifact. We want to use this for a monitoring application.

We tried the following:
    @Value("#{build['version']}")
   
private String version;

and in our override xml we tried each of the following:
<util:properties id="build" location="classpath:/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties" />
<util:properties id="build" location="classpath:META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties" />
<util:properties id="build" location="/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties" />

We also have the correct xmlns:util and the coresponding xsi:schemaLocation added.

On startup when deploying site.war we get the following:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'build': Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties] cannot be opened because it does not exist

We are using Hippo CMS 7.9.8 Enteprise edition and the development machine is on Windows.

Any ideea how to get this to work?

Regards,
Alex

Woonsan Ko

unread,
Jun 24, 2015, 9:38:32 AM6/24/15
to hippo-c...@googlegroups.com
On 6/24/15 9:29 AM, Alexandru Ionescu wrote:
> and in our override xml we tried each of the following:
> |
> <util:propertiesid="build"location="classpath:/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties"/>
> <util:propertiesid="build"location="classpath:META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties"/>
> <util:propertiesid="build"location="/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties"/>
> |
>
> We also have the correct xmlns:util and the
> coresponding xsi:schemaLocation added.
>
> On startup when deploying site.war we get the following:
>
> |
> org.springframework.beans.factory.BeanCreationException:Errorcreating
> bean withname 'build':Invocationof init method failed;nested exception
> isjava.io.FileNotFoundException:classpath resource
> [META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties]cannot be
> opened because it does notexist
> |

Do you have a jar artifact (groupId: de.einsundeins.hippo, artifactId:
mam-hippo-site) file in site/WEB-INF/lib/ directory?
If not, where can you find the
META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties file
inside your site webapp?

Regards,

Woonsan

>


--
w....@onehippo.com www.onehippo.com
Boston - 745 Atlantic Ave, 8th Floor, Boston MA 02111
Amsterdam - Oosteinde 11, 1017 WT Amsterdam
US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466

Alexandru Ionescu

unread,
Jun 24, 2015, 9:47:22 AM6/24/15
to hippo-c...@googlegroups.com
Hi Woonsan,

On the dev machine the file is in:
mam-hippo\target\tomcat7x\webapps\site\META-INF\maven\de.einsundeins.hippo\mam-hippo-site 

and on our qa / live systems it's in:
/home/tomcat/server/slot1/webapps/site/META-INF/maven/de.einsundeins.hippo/mam-hippo-site

Regards,
Alex

Olivier Bourgeois

unread,
Jun 24, 2015, 9:59:39 AM6/24/15
to hippo-c...@googlegroups.com

Hi Alex,

I’ve used the ServletContext.getResourceAsStream in the past to display a build number in a webapp. I am not sure if Spring can read from the classpath inside a web container.

You can also try with the ClassLoader, but IIRC building the path can be tricky, it’s probably easier to use the ServletContext. So something like:

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/META-INF\maven\de.einsundeins.hippo\mam-hippo-site");

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.
Visit this group at http://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.

Alexandru Ionescu

unread,
Jun 24, 2015, 11:51:50 AM6/24/15
to hippo-c...@googlegroups.com
Hi Olivier,

This doesn't seem to work for me.
I tried the following:

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("META-INF/maven/de.einsundeins.hippo/mam-hippo-site");
        log
.info(inputStream.toString());

or variations on it:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
       
// InputStream input = classLoader.getResourceAsStream("META-INF/maven/de.einsundeins.hippo/mam-hippo-site");
       
System.out.println(classLoader.getResource("META-INF/MANIFEST.MF").toString());
       
InputStream input = classLoader.getResourceAsStream("META-INF/maven/de.einsundeins.hippo/mam-hippo-site");
        log
.info(input.toString());

The classLoader I get is the following:
WebappClassLoader
  context: /site
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@1c4d98da

Any ideea what I'm missing?

Regards,
Alex

Woonsan Ko

unread,
Jun 24, 2015, 11:53:26 AM6/24/15
to hippo-c...@googlegroups.com
Hi Alex,

Yeah, that's what I was suspicious about. :-)
That path (webapps/site/META-INF/...) is not in your classpath. That's
the reason why you couldn't read those properties from classpath resources.

Your util:properties bean definition is actually defining a bean with
PropertiesFactoryBean with the classpath: resource location. [1]
Spring Framework converts the classpath: resource location string to a
Resource object (ClassPathResource [3] in this case due to the prefix)
automatically.

Therefore, if you use a traditional bean definition, then you can
control more (e.g, instantiating a ServletContextResource object
instead) like the following example:

<bean id="build"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<bean class="">
<constructor-arg>
<bean
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject" ref="componentManagerFacade" />
<property name="propertyPath" value="servletContext" />
</bean>
</constructor-arg>
<constructor-arg
value="/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties"
/>
</bean>
</property>
</bean>

Let me know if it doesn't work.

Regards,

Woonsan


[1]
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util-properties
[2]
http://docs.spring.io/autorepo/docs/spring/3.2.12.RELEASE/javadoc-api/org/springframework/core/io/Resource.html
[3]
http://docs.spring.io/autorepo/docs/spring/3.2.12.RELEASE/javadoc-api/org/springframework/core/io/ClassPathResource.html
[4]
http://docs.spring.io/autorepo/docs/spring/3.2.12.RELEASE/javadoc-api/org/springframework/web/context/support/ServletContextResource.html

On 6/24/15 9:47 AM, Alexandru Ionescu wrote:
> Hi Woonsan,
>
>
> On the dev machine the file is in:
> *mam-hippo\target\tomcat7x\webapps\site\META-INF\maven\de.einsundeins.hippo\mam-hippo-site *
> *
> *
> and on our qa / live systems it's in:
> */home/tomcat/server/slot1/webapps/site/META-INF/maven/de.einsundeins.hippo/mam-hippo-site*
> *
> *
> Regards,
> Alex
>
> --
> Hippo Community Group: The place for all discussions and announcements
> about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google
> Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.

Olivier Bourgeois

unread,
Jun 24, 2015, 12:02:00 PM6/24/15
to hippo-c...@googlegroups.com
I think you need an absolute path, so more like "/META-INF/maven/de.einsundeins.hippo/mam-hippo-site".

And give a try also to ServletContext.getResourceAsStream, or what Woonsan suggested. It seems this classloader you get only have access to WEB-INF/classes and not your META-INF.


--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.

Alexandru Ionescu

unread,
Jun 25, 2015, 3:58:59 AM6/25/15
to hippo-c...@googlegroups.com
Hi,

We got it working by retrieving the ServletContext.

Woonsan, we could not get your example working, we get the following exception on startup:

[INFO] [talledLocalContainer] Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unnamed bean
 definition specifies neither
'class' nor 'parent' nor 'factory-bean' - can't generate bean name

Is this because of the bean with an empty class?

<bean id="build"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 
<property name="location">
   
<bean class="">
     
<constructor-arg>
       
<bean
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
         
<property name="targetObject" ref="componentManagerFacade" />
         
<property name="propertyPath" value="servletContext" />
       
</bean>
     
</constructor-arg>
     
<constructor-arg
value="/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties"
/>
   
</bean>
 
</property>
</bean>

Best regards,
Alex

Woonsan Ko

unread,
Jun 25, 2015, 9:09:34 AM6/25/15
to hippo-c...@googlegroups.com
Ah, right. I forgot to fill the FQCN. It should be:

<bean id="build"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<bean
class="org.springframework.web.context.support.ServletContextResource">
<constructor-arg>
<bean
class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject" ref="componentManagerFacade" />
<property name="propertyPath" value="servletContext" />
</bean>
</constructor-arg>
<constructor-arg
value="/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties"
/>
</bean>
</property>
</bean>


On 6/25/15 3:58 AM, Alexandru Ionescu wrote:
> Hi,
>
> We got it working by retrieving the ServletContext.
>
> Woonsan, we could not get your example working, we get the following
> exception on startup:
>
> |
> [INFO][talledLocalContainer]Causedby:org.springframework.beans.factory.parsing.BeanDefinitionParsingException:Configurationproblem:Unnamedbean
> definition specifies neither 'class'nor 'parent'nor
> 'factory-bean'-can't generate bean name
> |
>
> Is this because of the bean with an empty class?
>
> |
> <beanid="build"
> class="org.springframework.beans.factory.config.PropertiesFactoryBean">
> <propertyname="location">
> <beanclass="">
> <constructor-arg>
> <bean
> class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
> <propertyname="targetObject"ref="componentManagerFacade"/>
> <propertyname="propertyPath"value="servletContext"/>
> </bean>
> </constructor-arg>
> <constructor-arg
> value="/META-INF/maven/de.einsundeins.hippo/mam-hippo-site/pom.properties"
> />
> </bean>
> </property>
> </bean>
> |
>
> Best regards,
> Alex
>
> --
> Hippo Community Group: The place for all discussions and announcements
> about Hippo CMS (and HST, repository etc. etc.)
>
> To post to this group, send email to hippo-c...@googlegroups.com
> RSS:
> https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
> ---
> You received this message because you are subscribed to the Google
> Groups "Hippo Community" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hippo-communi...@googlegroups.com
> <mailto:hippo-communi...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages