You can retrieve maven path from the hosted agent capabilities section, then create the variable using the Logging Commands. Then you can use the variable in batch script instead calling maven path.
Create a PowerShell script to set the avariables (Reference below
sample, you can also Use the OAuth token to access the REST
API), then check in the script into VSTS.
Add a PowerShell task in your definition to run the PS script
3.Use the variable in steps which behind the set variable step
$collectionurl = "https://xxx.visualstudio.com"
$user = "username"
$token = "password"
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$baseUrl = "$collectionurl/_apis/distributedtask/pools/2/agents/1?includeCapabilities=true"
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
#Retrieve values
$maven = $response.systemCapabilities.maven
#Set variable
Write-Host "##vso[task.setvariable variable=maven]$maven"
#Then you can use the variable in the next step: $(maven) in TFS, $env:maven in PowerShell, %maven% in batch script.
UPDATE:
Well, you can use PAT without the username with below script (If you don't want to hardcode the token in the script, then you can create a secret variable and set the token as the value of the variable, then use the variable in script):
$PAT = "nvkoa6qrdrtrxweruiodfiamwd3ya2dkt7r6cx3xxxxw5pyxxxxq"
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT)))
$baseUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)_apis/distributedtask/pools/2/agents/1?includeCapabilities=true"
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)})
#Retrieve values
$maven = $response.systemCapabilities.maven
Write-Host "##vso[task.setvariable variable=maven]$maven"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…