Reputation: 574
i'm trying to configure a property of a Spring bean to show the version of the Maven artifact. I need the property to be configured when the bean is loaded (not at runtime).
applicationContext.xml:
<bean id="myBean" class="com.domain.ClassName">
<property name="version" value="${???????}" />
</bean>
com.domain.ClassName:
private String version;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
Is there a way to do this? More generally, is there a simple way to access properties from the POM xml on the Spring xml?
Thank you
Upvotes: 4
Views: 1970
Reputation: 155
Here an example of how to introduce build version/ branch and revision through maven resource plugin in pom.xml (normaly you want have different values in different profiles e.g. local, prod, test... etc.):
<properties>
<!-- in my case injected by jenkins build job -->
<build.version>dev</build.version>
<build.branch>master</build.branch>
<build.revision>0.0.32</build.revision>
</properties>
Your version.properties property-file is e.g this:
build_version=${build.version}
build_branch=${build.branch}
build_revision=${build.revision}
Resource filtering (placeholders are replaced by pom-property values here while maven is processing your resources)
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>conf/version.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
Bean and property placeholder config in context.xml:
<context:property-placeholder location="classpath:conf/version.properties"/>
<bean id="buildVersion" class="de.your.package.cfg.BuildVersion">
<property name="buildBranch" value="${build_branch}"/>
<property name="buildVersion" value="${build_version}"/>
<property name="buildRevision" value="${build_revision}"/>
</bean>
Your bean looks like this then:
@Component
public class BuildVersion {
private String buildBranch;
private String buildVersion;
private String buildRevision;
public String getBuildRevision() {
return buildRevision;
}
public void setBuildRevision(String buildRevision) {
this.buildRevision = buildRevision;
}
public String getBuildVersion() {
return buildVersion;
}
public void setBuildVersion(String buildVersion) {
this.buildVersion = buildVersion;
}
public String getBuildBranch() {
return buildBranch;
}
public void setBuildBranch(String buildBranch) {
this.buildBranch = buildBranch;
}
}
So your replacement flow is from maven build profile -> properties file -> spring bean code (instead of maven profile your input can be from command-line parameters like -Dbuild.version="dev" or something else...).
Alternativelly to xml-configuration you can user @Value and annotations to inject the values from spring context into the bean:
@Component
public class BuildVersion {
@Value("${build_branch:default_value}")
private String buildBranch;
...
public String getBuildBranch() {
return buildBranch;
}
...
}
You will need an xml context configuration or a configuration bean with PropertySource configured:
@Configuration
@PropertySource({"classpath:conf/version.properties"})
public class ApplicationContext(){
...
}
Upvotes: 2
Reputation: 10833
You should have a look at the maven resource plugin. See : http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
Basically the idea is that upon building your application, you will filter some of your project files to replace some text with what you want (in your case you want to place the version declared in your pom.xml
into your spring.xml
)
The key here is to understand that it is maven that, at build time, will do some replacement in your spring configuration file that will be used at runtime.
Upvotes: 2
Reputation: 5318
Since both Spring and Maven could read properties file I would suggest that you extract the data you want to share to this common file and use it from both configurations independently.
Upvotes: 0