1

I'm trying to mavenize a project. The code base is beginning to bloat and it needs to be split into multiple modules. However, we already have a somewhat proprietary deployment process in place and the directory structure cannot be compromised in favor of the Maven way.

The simplified structure is as follows:

  /workspace/basesrc/
                    |_ superpom.xml
                    |_ com/company/parentpkg/
                                          |_ ModuleA/
                                                    |_ pom.xml
                                                    |_SubAModuleA/
                                                                 |_ SubAModuleAClass.java
                                                                 |_ pom.xml
                                                    |_SubAModuleB/
                                                                 |_ SubAModuleBClass.java
                                                                 |_ pom.xml
                                          |_ ModuleB/
                                                    |_ ModuleBClass.java
                                                    |_ pom.xml

I was able to build the project using the following setup:

Approach A

(superpom.xml):

<build>
  <sourceDirectory>.</sourceDirectory>
</build>

This is inherited by the submodules, in effect building only its current directory.

However, Sonar seems to have trouble resolving package names if this is the case as stated in this thread: mvn sonar:sonar throws exception while doing Java AST scan.

Approach B: Specify the root source directory and use maven compiler inclusions, as explained here: http://maven.apache.org/guides/mini/guide-using-one-source-directory.html

(superpom.xml)

<build>
  <sourceDirectory>/workspace/basesrc/</sourceDirectory>
</build>

(SubAModuleA pom.xml)

<build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <includes><include>com/company/parentpkg/ModuleA/SubAModuleA/**/*.java</include></includes>
        </configuration>
      </plugin>
    </plugins>
  </build>

Again, this works for the case of maven build. But Sonar seems to include the whole /workspace/basesrc/ folder despite running mvn sonar:sonar from a submodule directory.

SO my question is,

If I use Approach A, is there a way to configure/override the root directory property in which Sonar Maven Plugin is searching for Java source files? Can it be different from the ${project.build.sourceDirectory} maven property?

Using Approach B, How can Sonar be configured to analyze only what is being built by maven-compiler-plugin?

Community
  • 1
  • 1
  • I think it's important to tell us what package the SubAModuleAClass has. Is it com.company.parentpkg.ModuleA.SubAModuleA ? If so, I dont see the reason for having any submodule hierarchy there. Just go for the superpom and do the analysis there. – Peter Butkovic Jul 16 '13 at 05:53
  • Hi Peter,Yes it is com.company.parentpkg.ModuleA.SubAModuleA. However, the contents of this package is meant to be a self contained module. And besides, that's the point of implementing a multimodule project right? To be able to separate a source base into self contained reusable components? – Jarell Mallari Jul 16 '13 at 05:59
  • well, in the maven each of your submodules should contain full package hierarchy, rather than just an end class – Peter Butkovic Jul 16 '13 at 06:08

2 Answers2

1

For those who are interested, just to reiterate <sonar.includes> must be placed under <properties> instead of <configuration> under <plugin>. And sonar must be version 3.5 up.

0

well, I think following should help you to setup your project: http://maven.apache.org/guides/mini/guide-using-one-source-directory.html

See the section:

Producing Multiple Unique JARs from a Single Source Directory

Please understand that it's not the way things should be setup in maven, as you're breaking the convention, that makes maven so strong. On the other hand I understand that in some projects there is no option for changing the source structure to the way maven convention says it.

Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81
  • Hi Peter, this could be an option, but this doesn't make any difference in the sonar analysis vs the Approach B I mentioned above. It still analyzes the whole code base as a single project. What would be ideal is to be able to selectively analyze a submodule without taking in all the source codes from other modules. – Jarell Mallari Jul 16 '13 at 06:29
  • are you sure, it's the same? I believe the difference to your setup is that they use: ../src/main/java in the submodule, did you do that? For your case: ../../../../ (if I counted the packages correctly :)) – Peter Butkovic Jul 16 '13 at 06:32
  • I came across this a while ago, http://stackoverflow.com/questions/15065400/maven-sonar-exclude-folder-from-analysis. So I added, [insert maven-compiler-plugin inclusion filter here]. However , judging from the length of the analysis it seems that Sonar is still looking at the other submodules? Or am I just mistaken in the usage? – Jarell Mallari Jul 16 '13 at 06:34
  • What I meant was, it is still results in the same single source folder sonar analysis, rather than having separate modules be analyzed selectively. Did I get you right? – Jarell Mallari Jul 16 '13 at 06:35
  • 1
    Got it. should be as a project property and sonar version must be 3.5 up. – Jarell Mallari Jul 16 '13 at 11:22