Skip to content
Advertisement

Get Maven Version in an AWS Lambda

I am developing an AWS Lambdas using java. It would be very useful if, in the lambda, there was a way to log the maven version of the lambda in the log file so we can track what versions of lambdas are running in different environments. Normally this could be done pretty easily using the code below:

    String version = null;
    try {
        Properties p = new Properties();
        InputStream is = getClass().getResourceAsStream("/META-INF/maven/com.my.example/my-lambda/pom.properties");
        if (is != null) {
            p.load(is);
            version = p.getProperty("version", "");
        }
    } catch (Exception e) {
        // ignore
    }
    log.info(version);
    Package aPackage = getClass().getPackage();
    if(aPackage != null){
        version = aPackage.getImplementationVersion();
        if(version == null){
            version = aPackage.getSpecificationVersion();
        }
    }
    log.info(version);

Unfortunately it always comes up null for the version. I verified that the jar file has the specified path and the pom.properties is present in the jar in the correct path with the version information contained within. Any help would be greatly appreciated!

EDIT: The answer has to be specific to running the java jar as an aws lambda. Getting it out of a jar is normally pretty trivial using the code above, but when the jar is run as an aws lambda it doesn’t seem to be able to access the various the maven files to get the version information.

NOTE on the answer: AWS sam build takes the built jars and deploys only certain files to be available to the lambda. You can see this when you run your lambda in an IDE and the aws-toolkit creates the .aws-sam/build folder. Unlike in typical jars, only the lambda class file and any resources defined in the pom.xml with the resources tag are copied here. I used the suggestion to use the git-commit-id-plugin to create a git.properties file with version information, which is created in the root of the project. To make it so aws knows to copy this file also, you need to modify the pom.xml to include the git.properties file as shown below:

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>./</directory>
                <includes>
                    <include>**/git.properties</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>

Advertisement

Answer

If you use Git, I do the following. In my pom.xml I have (in the build/plugins section):

        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <version>4.0.4</version>

            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <useNativeGit>true</useNativeGit>
                <dateFormat>yyyy-MM-dd'T'HH:mm:ssXXX</dateFormat>
                <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <failOnNoGitDirectory>false</failOnNoGitDirectory>
            </configuration>
        </plugin>

In my Java code I have:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class VersionInfo {
    public static String getVersionString() {
        try {
            Properties properties = getGitProperties();

            boolean isDirty = false;

            String gitDirty = properties.getProperty( "git.dirty" );
            if( gitDirty != null )
                isDirty = Boolean.parseBoolean(gitDirty);

            return "built "" + properties.getProperty("git.build.time") +
                    "" in branch "" + properties.getProperty("git.branch") +
                    "" with short commit id "" + properties.getProperty("git.commit.id.describe-short") + """ +
                    ", isDirty is " + isDirty +
                    " remote url is "" + properties.getProperty("git.remote.origin.url") + """;
        }
        catch( IOException ioe ) {
            return( "can't locate git.properties on the class path");
        }
    }


    private static Properties getGitProperties() throws IOException {

        Properties properties = new Properties();

        try (InputStream inputStream = VersionInfo.class.getResourceAsStream("/git.properties")) {
            if (inputStream == null)
                throw new IOException("Can't locate properties file to generate version info");

            properties.load(inputStream);

            return properties;
        }
    }
}

And in my Lambda I have:

public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {
    context.getLogger().log( "starting " + this.getClass().getName() + " version " + VersionInfo.getVersionString() );
}

Obviously you can get whatever information from the git-properties file. It looks something like:

Generated by Git-Commit-Id-Plugin
#Mon Mar 22 10:15:05 MDT 2021
git.branch=develop
git.build.host=bluerock
git.build.time=2021-03-22T10:16:05-06:00
git.build.user.email=
git.build.user.name=
git.build.version=1.1.0-SNAPSHOT
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=7745a4c46484b26c3285362e6f7526f551a9a60f
git.commit.id.abbrev=7745a4c
git.commit.id.describe=7745a4c
git.commit.id.describe-short=7745a4c
git.commit.message.full=some uber fixes
git.commit.message.short=some uber fixes
git.commit.time=2020-03-23T15:16:42-06:00
git.commit.user.email=user@example.com
git.commit.user.name=User Name
git.dirty=false
git.remote.origin.url=https://oauth2@git.example.com/some-project.git
git.tags=
git.total.commit.count=14
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement