Hi Diptesh Kumar,
I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this!
Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.
Update:
To resolve the error The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program
:
We need to explicitly set the environment variable for
JAVA_HOME
to a specific folder-level path, but only for that particular task.
To Summarize, you can build a pipeline that switches between Java versions using environment variables and run both Maven and Gradle builds.
Sample Pipeline:
trigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
variables:
JAVA_HOME_17: '/path/to/java17'
JAVA_HOME_21: '/path/to/java21'
jobs:
- job: MavenBuild
displayName: 'Maven Build'
steps:
- task: UseJavaVersion@1
inputs:
versionSpec: '17'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
jdkHome: $(JAVA_HOME_17)
- script: |
mvn clean install
displayName: 'Run Maven Build'
- job: GradleBuild
displayName: 'Gradle Build'
steps:
- task: UseJavaVersion@1
inputs:
versionSpec: '21'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
jdkHome: $(JAVA_HOME_21)
- script: |
./gradlew build
displayName: 'Run Gradle Build'
You can use the GitHub repository https://github.com/spring-projects/spring-petclinic.git which contains both Maven and Gradle build configurations to achieve your requirement.
If you want to validate the Maven installation and run the build tasks using Java 17 and Java 21 versions, use mavenPath: '$(MAVEN_HOME)'
instead of mentioning mavenPath: '$(MAVEN_HOME)'/bin
.
This is to avoid bin/mvn
getting appended redundantly which causes the path to be incorrect like /apache-maven-version/bin/bin/mvn
and prevents path-related errors during build process:
variables:
MAVEN_HOME: '/AzDOAgents/apache-maven-3.8.8' # Correct Maven installation path
stages:
- stage: ValidateEnvironment
displayName: 'Validate Environment Setup'
jobs:
- job: ValidateMaven
displayName: 'Validate Maven Installation'
steps:
# Step 1: Validate Maven Installation
- script: |
echo "### Validating Maven Installation ###"
echo "MAVEN_HOME is set to: $MAVEN_HOME"
export PATH=$MAVEN_HOME/bin:$PATH
which mvn
mvn -version
displayName: 'Validate MAVEN_HOME Path'
# Step 2: Run Maven Task
- task: Maven@4
inputs:
mavenPomFile: 'pom.xml'
publishJUnitResults: false
javaHomeOption: 'JDKVersion'
jdkVersionOption: '11'
mavenVersionOption: 'Path'
mavenPath: '$(MAVEN_HOME)'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
displayName: 'Run Maven'
Hope this helps.
Please do not forget to click "Accept the answer” and Yes wherever the information provided helps you, this can be beneficial to other community members.
If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.