Controling java imports with the help of sonarqube

What I am trying to do is in sonarqube add rules that say for instance:
Files in project A are not allowed to import files from project B, or vice versa.
I read in your documentation that there was a rule called ‘Architectural constraint’ that could do exactly this but I can not find it among my templates where the documentation claims it to be.

I have version 7.1, build 11001.
I have searched internet and started looking for plugins that can do this instead, tried installing sonargraph but wasn’t able to make my own rules.
Is the documentation old or is there a replacement for ‘Architectural constraints’ because that is exactly what I need. “Forbid access to **.client.** from **.server.** classes” and vice versa.

Thanks for any help.
Write you later / Mårten

Hi Marten,

First, sorry about the missing diacritic in your name, I have a plain English keyboard layout.

To answer your question, I remember the rule you’re talking about, but I don’t know what happened to it. (Would you mind pointing out where you found the reference to it so I can get that cleaned up, please?) There is Track uses of disallowed classes. It’s a template rule, so while it would be tedious, I think you can accomplish your goal with it with a carefully constructed set of rules and an equally carefully constructed set of “Multiple Criteria” on which to ignore issues.

 
Ann

Hello Ann,

Hehe no worries, the problems with having Swedish letters in my name :slight_smile:

The rule is referenced here: https://docs.sonarqube.org/latest/user-guide/rules/.
Sad that it was removed because it really looks like what I was after.

It sounds like what you are describing is something I can use.
At the moment we have some issues with our sonar so can’t test it but as soon as it is up and running I will try to add two rules, one for each package I want to exclude and try to set up some criteria to exclude the rule from their the respective package.
If it works I will mark your comment as the solution.

Thanks for the help!

Write you later / Mårten

1 Like

Hi again,

Thanks for the documentation reference. From the rule key in the screenshots, I was able to track down that this rule was deprecated and eventually removed in favor of a small suite of replacements:

  • Track uses of disallowed methods
  • Track uses of disallowed dependencies
  • Track uses of disallowed classes
  • Track uses of disallowed constructors

I’ve created this ticket to get the documentation fixed:

SONAR-12190 - Update Rules page to remove reference to no-longer-available rule

 
Thx,
Ann

1 Like

Hello again,

It seems to work so thanks for the tips!
I did notice two things I was wondering about and thought I might just mention them.

  1. I am unable to use *.client.*, as class to exclude, I have to use e.g root.*.client.*. Would it be possible to allow regexp from start?
    (It only forces me to do some extra rules but thought I could ask.)
  2. It doesn’t seem to react if I for instance only fetches constants from another class.
    Lets say I exclude root.*.client.* and in my code I use
    root.sublevel.client.constants.Fruits.APPLE
    I do not get an error.
    But if I where to do:
    new root.sublevel.client.constants.Fruits().APPLE
    I do get an error.
    Is it possible to make it react on the import? (Just curious)

Hi Marten,

Could you create a new thread with these two issues, please? They need attention from other people, and it will be easier to get that with a new thread since this one is ‘solved’.

 
Thx,
Ann

Hello Ann,

Sure!
Will make them right away.

Write you later / Mårten

1 Like