Jenkins执行SonarQube任务报错及项目关联配置咨询
Hey there, let’s work through this SonarQube + Jenkins problem step by step— I’ve dealt with similar headaches before, so I know exactly where to start.
1. First: Fix the "Task 'my-task' does not exist" Error
This error almost always happens because you’re using the project display name in Jenkins instead of SonarQube’s unique project key. SonarQube uses keys to identify projects, not the friendly name you set when creating it.
- Log into your SonarQube server, find the project you created
- Head to the project details page (or project settings) and grab the Project Key— it might look like
my-task-key(could be different from the name you typed) - Make sure Jenkins is configured to use this key, not the display name, when referencing the task
2. Linking Your Actual Project to SonarQube & Setting Scan Paths
Here’s a common misconception: you don’t need to manually link your server’s project path in SonarQube’s backend. The scan path is controlled by your build command (like Maven) when you run the analysis.
When you created the project in SonarQube, it should’ve generated a sample analysis command (for Maven, it looks like this):
mvn sonar:sonar \ -Dsonar.projectKey=your-project-key \ -Dsonar.projectName="Your Project Name" \ -Dsonar.host.url=http://your-sonarqube-server:9000 \ -Dsonar.token=your-auth-token
To scan your project at /home/{user}/live/{sitename}/htdocs, just run this command from that directory— SonarQube will automatically scan all code there.
If you need to narrow down the scan to specific folders (like only src or htdocs), add this parameter:
-Dsonar.sources=src,htdocs # Separate multiple paths with commas
3. Configuring SonarQube in Jenkins Build Steps
Since your project uses Maven, let’s cover two straightforward ways to set this up:
Option 1: Use Maven Build Step with SonarQube Params
- In your Jenkins project configuration, go to the Build section and add an Invoke top-level Maven targets step
- In the Goals field, enter:
clean verify sonar:sonar - In the Properties section, add these key-value pairs (replace with your actual values):
sonar.projectKey=your-project-key-from-sonarqube sonar.projectName=Your Project’s Display Name sonar.host.url=http://localhost:9000 # Since SonarQube is on the same server, localhost works sonar.token=your-sonarqube-auth-token # Generate this in SonarQube’s user settings sonar.projectBaseDir=/home/{user}/live/{sitename}/htdocs # Tells Maven where your project lives
Option 2: Use the SonarQube Scanner Plugin
If you have the SonarQube Scanner plugin installed in Jenkins:
- Add an Execute SonarQube Scanner build step to your project
- In the Analysis properties box, paste these lines (adjust values as needed):
sonar.projectKey=your-project-key sonar.projectName=Your Project Name sonar.host.url=http://localhost:9000 sonar.token=your-auth-token sonar.projectBaseDir=/home/{user}/live/{sitename}/htdocs sonar.sources=. # Scans all files in the project base dir; replace with specific paths if needed
Quick Checks to Avoid Headaches Later
- Make sure the Jenkins user (usually
jenkins) has permission to access/home/{user}/live/{sitename}/htdocs— usechownorchmodto adjust permissions if needed - The SonarQube token you use should have project analysis permissions (check this in SonarQube’s project settings)
- If you’re still stuck, run this command from your project directory on the server to list all valid SonarQube projects:
This will show you all projects SonarQube recognizes— confirm your project key is in the list.mvn sonar:list -Dsonar.host.url=http://localhost:9000 -Dsonar.token=your-token
内容的提问来源于stack exchange,提问作者ThurstonLevi

