tharindu_DG
tharindu_DG

Reputation: 9261

Dynamically populate version from version.sbt

I'm trying to take the version from version.sbt and and populate it to logback.xml's log appender's applicationVersion field.

version.sbt

version in ThisBuild := "0.4.63"

logback.xml

<configuration debug="true" scan="true" scanPeriod="60 seconds">

    <appender name="ADP-MESSAGING" class="com.agoda.adp.messaging.logging.appenders.LogbackAppender">
        <applicationName>MyApp</applicationName>
        <applicationAssemblyName>myapp</applicationAssemblyName>

        <applicationVersion>0.4.61</applicationVersion>

        <!-- <applicationVersion>${application.version}</applicationVersion> -->

        <apiKey>s234W@#$WFW$@$@</apiKey>
        <getCallerData>false</getCallerData>
    </appender>

    ....

    <root level="WARN">
        <appender-ref ref="ADP-MESSAGING" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

I tried by adding ${application.version}, ${version} but no success.

How can I do this? Please share your thoughts.

Thanks

Upvotes: 3

Views: 198

Answers (2)

Gryum
Gryum

Reputation: 1

You can add logback PropertyDefiner implementation:

package org.mypackage

import ch.qos.logback.core.PropertyDefinerBase

class VersionPropertyDefiner extends PropertyDefinerBase {
  override def getPropertyValue: String = BuildInfo.version
}

You will get autogenerated (managed) scala code BuildInfo.version if you use BuildInfoPlugin in your project build settings.

Then you can define and use variable in your logback.xml configuration:

<configuration debug="true" scan="true" scanPeriod="60 seconds">
    <define name="appVersion" class="org.mypackage.VersionPropertyDefiner"/>

    <appender name="ADP-MESSAGING" class="com.agoda.adp.messaging.logging.appenders.LogbackAppender">
        <applicationName>MyApp</applicationName>
        <applicationAssemblyName>myapp</applicationAssemblyName>

        <applicationVersion>${appVersion}</applicationVersion>

        <apiKey>s234W@#$WFW$@$@</apiKey>
        <getCallerData>false</getCallerData>
    </appender>

    ....

    <root level="WARN">
        <appender-ref ref="ADP-MESSAGING" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Upvotes: 0

jkinkead
jkinkead

Reputation: 4411

The values interpolated in a logback.xml file are simply Java system properties. All you have to do is add a value to your Java commandline defining the value you want:

// NOTE: This will only work when running through sbt. You'll have to 
// append the same value to your startup scripts when running elsewhere.
javaOptions += "-Dapplication.version=" + version.value

With this flag, you should be able to interpolate the version in your XML file:

<applicationVersion>${application.version}</applicationVersion>

Upvotes: 2

Related Questions