Configure Maven artifact version as Spring property

I am trying to set up the Spring bean property to show the version of the Maven artifact. I need the property to be configured when the bean loads (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 any way to do this? More generally, is there an easy way to access properties from a POM XML object in Spring xml?

thank

+4
source share
3 answers

You should look at the maven resource plugin. See: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

, , , - , ( , pom.xml, spring.xml)

, maven spring, .

+2

, / maven pom.xml( , , local, prod, test... ..):

<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>

version.properties, , :

build_version=${build.version} 
build_branch=${build.branch} 
build_revision=${build.revision}

( pom-property , maven )

<resources>
    <resource>
       <directory>src/main/resources</directory>
           <includes>
                <include>conf/version.properties</include>
           </includes>
       <filtering>true</filtering>
   </resource>
</resources>

Bean .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>

bean :

@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;
    }        
}

, maven → → spring bean ( maven , -Dbuild.version = "dev" - ...).

xml- @Value spring bean:

@Component    
public class BuildVersion {

    @Value("${build_branch:default_value}") 
    private String buildBranch;
    ...

    public String getBuildBranch() {
        return buildBranch;
    }
    ... 
}

xml bean PropertySource:

@Configuration
@PropertySource({"classpath:conf/version.properties"})
public class ApplicationContext(){
 ...
}
+1

Spring Maven , , , .

Maven Properties

Spring

0
source

All Articles