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?

4

2 回答 2

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.

于 2013-07-16T14:19:08.843 回答
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.

于 2013-07-16T06:07:24.233 回答