ARTIFACTORY: Artifactory maven build task using MAVEN_OPTS as a parameter

AuthorFullName__c
Deepti Sharma
articleNumber
000005579
FirstPublishedDate
2023-02-14T14:18:31Z
lastModifiedDate
2025-07-22

ARTIFACTORY: Artifactory maven build task using MAVEN_OPTS as a parameter

Sometimes we encounter an OutOfMemoryError during the build process. This error occurs when the Java Virtual Machine (JVM) does not have enough memory available to allocate to the Java program that is being run.
The -DXms and -DXmx options that we specify  the options input for the ArtifactoryMaven task are used to set the minimum and maximum heap sizes for the JVM, respectively. The -DMaxPermSize option is used to set the maximum size of the permanent generation, which is a part of the heap that is used to store class and method objects.

In the below use case, we have specified a minimum heap size of 10 MB and a maximum heap size of 10 MB, as well as a maximum permanent generation size of 500 MB. These values may not be sufficient to allow the Java program to run without encountering an OutOfMemoryError, depending on the requirements of the program and the resources available on the system. You can increase or decrease them as per specific requirements.
 

options: -DXms=10m -DXmx=10m -DMaxPermSize=500m
How to overcome this heap memory error


To troubleshoot this issue, we can try increasing the heap sizes and permanent generation size to see if that helps to resolve the error.
In addition, you may want to consider optimizing the Java program to reduce its memory usage, such as by reducing the number of objects that are being created and stored in the heap, or by using data structures that are more memory-efficient.

We can overcome the heap memory error after defining the MAVEN_OPTS as a global variable that has the scope of all tasks.
Here we defined the variable in our environment as MAVEN_OPTS  in a sample pipeline shared below:-

Sample Pipeline snippet:

# Maven
# Build your Java project and run tests with Apache Maven.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/java
variables:
- name: MAVEN_OPTS
  value: '-Xms10m -Xmx10m -client -XX:MaxPermSize=500m  -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Xverify:none'
trigger:
- main
pool:
  vmImage: ubuntu-latest
steps:
- task: CmdLine@2
  inputs:
    script: 'java -XX:+PrintFlagsFinal -version | grep -iE ''HeapSize|PermSize|ThreadStackSize'''
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: 'env | sort'
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      write-host "before: $($env:MAVEN_OPTS)"
- task: JFrogMaven@1
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'install'
    options: -DXms=100m -DXmx=10m -DMaxPermSize=10m
    artifactoryResolverService: 'art2'
    targetResolveReleaseRepo: 'manu-libs-release'
    targetResolveSnapshotRepo: 'manu-libs-snapshot'
    artifactoryDeployService: 'art2'
    targetDeployReleaseRepo: 'manu-libs-release'
    targetDeploySnapshotRepo: 'manu-libs-snapshot'
    collectBuildInfo: true
    buildName: '$(Build.DefinitionName)'
    buildNumber: '$(Build.BuildNumber)'
    includeEnvVars: true
- task: CmdLine@2
  inputs:
    script: 'env | sort'
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'write-host "$($env:MAVEN_OPTS)"'
- task: CmdLine@2
  inputs:
    script: 'java -XX:+PrintFlagsFinal -version | grep -iE ''heapsize|permsize|threadstacksize'''
Defining MAVEN_OPTS inside maven artifactory task


If you want to define the MAVEN_OPTS as an internal variable inside the ARTIFACTORY_MAVEN task , we already have a feature request in github for this https://github.com/jfrog/jfrog-azure-devops-extension/issues/373 and it is pending a fix.