How to properly setup sonarcloud on java Microservice

I have a problem setting up sonarcloud on my microservices.
Which service should i include the below as instructed, considering the structure of my project

<properties>
  <sonar.organization>blinxcode</sonar.organization>
  <sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>

Project structure image.

Thanks in advance.

Hey there.

Would you consider this a monorepo? If so – you would probably want to consider configuring a monorepo which would mean setting up analysis for each project in the repo.

1 Like

Yes. I will look into the resource.

I have read the resource and it’s quite helpful in understanding the setup the more… but my question was not answered there. As regard the pom.xml, where should i place the properties ? on each of the projects or what?

I need to know where to place this code.

In the pom.xml for each project.

1 Like

Should the run in the GitHub Actions .yml file come last after i finished adding other projects in the monorepo or for each of the projects added.

run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=B*****-***

Below is my current error in github action
Invalid workflow file: .github/workflows/build.yml#L40


The workflow is not valid. .github/workflows/build.yml (Line: 40, Col: 9): Unexpected value 'run' .github/workflows/build.yml (Line: 34, Col: 9): Required property is missing: uses

The line 40 it is complaining of is the run: ... above

Since you aren’t using the official GitHub action, you can’t use with: projectBaseDir .... and need to pass sonar.projectBaseDir to your mvn command.

1 Like

Are you saying i should remove the line or add sonar.projectBaseDir as below?

with:
 sonar.projectBaseDir: user-identity-service/

Running the above still complain of the workflow not valid.

Confirm the below configuration is fine. I need to get this sorted today and see how it works before introducing our private project.

Current error

Error: The goal you specified requires a project to execute but there is no POM in this directory (/home/runner/work/Blinx-Microservice/Blinx-Microservice). Please verify you invoked Maven from the correct directory.

Below is my implementation.

name: My Test Monorepo Project
on:
  push:
      branches:
      - main
 
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  cloudGatwayService:
    name: CloudGatwayService
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis

      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Cache SonarCloud packages
        uses: actions/cache@v1
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      - name: Build and analyze
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BlinxCode_Blinx-Microservice
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}



  serviceRegistry:
    name: ServiceRegistry
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis

      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Cache SonarCloud packages
        uses: actions/cache@v1
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      - name: Build and analyze
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BlinxCode_Blinx-Microservice
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}



  userIdentityService:
    name: UserIdentityService
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis

      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Cache SonarCloud packages
        uses: actions/cache@v1
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      - name: Build and analyze
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=BlinxCode_Blinx-Microservice

        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
 

Here is the stacktrace

Error:  The goal you specified requires a project to execute but there is no POM in this directory (/home/runner/work/Blinx-Microservice/Blinx-Microservice). Please verify you invoked Maven from the correct directory. -> [Help 1]
org.apache.maven.lifecycle.MissingProjectException: The goal you specified requires a project to execute but there is no POM in this directory (/home/runner/work/Blinx-Microservice/Blinx-Microservice). Please verify you invoked Maven from the correct directory.
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:85)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:294)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

Well… is there a pom.xml in /home/runner/work/Blinx-Microservice/Blinx-Microservice? If not, where is it for this project?

As noted previously: add it to the mvn command. mvn .... -Dsonar.projectBaseDir=directory

Look at my above structure. There is no pom.xml outside each of the services. I only have pom.xml in the services.

How can i configure workflow to point to each of the service’s pom.xml

Please confirm the below directory i respect to my attached project structure screenshot, because it is still failing.

          run: mvn -X switch -B verify sonar:sonar  -Dsonar.projectKey=BlinxCode_Blinx-Microservice -Dsonar.organization=BlinxCode -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN -Dsonar.projectBaseDir=Blinx-Microservice/cloud-gateway/

@Colin Please and Please i need to get over this to show our IT department how Sonarcloud can help us before going for the Enterprise. The last question is pretty straight forward, If possible, the product is public, you can check it out.

Hello @Bashorun_Olajide ,

Looking at your project and the error message from your latest GitHub Actions run, it seems that you need to remove Blinx-Microservice from the projectBaseDir configuration:

          run: mvn -X switch -B verify sonar:sonar  -Dsonar.projectKey=BlinxCode_Blinx-Microservice -Dsonar.organization=BlinxCode -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN -Dsonar.projectBaseDir=cloud-gateway/

Let me know if that works. Also, if you are going with the monorepo setup, you will need three different project keys in your configuration. If you have imported your repository as a monorepo on SonarCloud (docs) then you will be able to add multiple projects within that repository.

1 Like

Thank you for your response @Martin_Bednorz,

I have corrected that, it didn’t work as well. Check my github workflow or the latest error

if you are going with the monorepo setup, you will need three different project keys in your configuration.

How’s the above done?

I have a presentation tomorrow with team company and i hope to get this sorted so that they ca approve Sonarcloud for their code analysis.

The mvn command seems to be run in the root folder, where there is no pom.xml in your case. You could try changing the working directory for the various GitHub Actions jobs to the relevant subfolders as described here.

[...]
  jobs:
    cloudGateway:
      name: CloudGateway
      runs-on: ubuntu-latest
      defaults:
        run:
          working-directory: cloud-gateway
      steps:
[...]

Then you could also remove the sonar.projectBaseDir property from the mvn command.

When analyzing a new project, you can select to setup a monorepo on bottom right of the “Analyze Projects” page.


I’ve forked your repo and created a sample configuration here that works (I’ve had to skip tests for some of the projects). Feel free to check it out and let me know if you have any questions.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.