How to Configure Maven in Jenkins for Build Automation
To configure
Maven in Jenkins, first install the Maven plugin from Jenkins plugin manager. Then, add a Maven installation under Manage Jenkins > Global Tool Configuration by specifying the Maven version or installation path. Finally, use this Maven installation in your Jenkins jobs to build your projects.Syntax
Configuring Maven in Jenkins involves these key steps:
- Install Maven Plugin: Enables Maven support in Jenkins.
- Global Tool Configuration: Define Maven installations with names and paths.
- Job Configuration: Select the Maven installation and specify goals like
clean install.
shell
1. Go to Jenkins Dashboard > Manage Jenkins > Manage Plugins > Available tab 2. Search for 'Maven Integration Plugin' and install it 3. Go to Manage Jenkins > Global Tool Configuration 4. Under 'Maven', click 'Add Maven' 5. Enter a name (e.g., 'Maven 3.8.6') 6. Either check 'Install automatically' or provide the MAVEN_HOME path 7. In your Jenkins job configuration: - Choose 'Invoke top-level Maven targets' - Select the Maven installation name - Enter goals like 'clean install' - Save and build
Example
This example shows how to configure a Jenkins freestyle job to build a Maven project:
shell
1. Install 'Maven Integration Plugin' via Manage Plugins. 2. Configure Maven in Global Tool Configuration: - Name: Maven 3.8.6 - Install automatically: checked 3. Create a new Freestyle project: - Under 'Build' section, add 'Invoke top-level Maven targets' - Select 'Maven 3.8.6' as Maven version - Goals: clean install 4. Save and run the build.
Output
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.123 s
[INFO] Finished at: 2024-06-01T12:00:00Z
[INFO] ------------------------------------------------------------------------
Common Pitfalls
Common mistakes when configuring Maven in Jenkins include:
- Not installing the Maven plugin before configuration.
- Incorrect Maven installation path causing build failures.
- Forgetting to select the configured Maven version in the job.
- Not specifying Maven goals, leading to no build actions.
shell
Wrong way:
- Skipping plugin installation
- Using wrong MAVEN_HOME path
- Leaving Maven goals empty
Right way:
- Install Maven plugin first
- Verify Maven installation path or use automatic install
- Specify goals like 'clean install' in job build stepQuick Reference
| Step | Action | Details |
|---|---|---|
| 1 | Install Maven Plugin | Manage Jenkins > Manage Plugins > Available > Search 'Maven Integration Plugin' > Install |
| 2 | Configure Maven | Manage Jenkins > Global Tool Configuration > Add Maven > Name + Path or Auto-install |
| 3 | Use in Job | Create/Configure job > Build > Invoke top-level Maven targets > Select Maven > Enter goals |
| 4 | Run Build | Save job and click Build Now to run Maven build |
Key Takeaways
Always install the Maven plugin before configuring Maven in Jenkins.
Define Maven installations in Global Tool Configuration with correct paths or auto-install.
Select the configured Maven version and specify goals in your Jenkins job build step.
Verify Maven goals like 'clean install' to ensure proper build execution.
Check logs for build success or errors to troubleshoot configuration issues.