# SonarQube Scanner execution fails: java can't be indexed twice

**URL:** <https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576>\
**Category:** SonarQube Server / Community Build\
**Created:** [October 22, 2018, 6:31pm UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576 "2018-10-22T18:31:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![manueljordan](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@manueljordan](https://community.sonarsource.com/u/manueljordan)\
**Post date:** [October 22, 2018, 6:31pm UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/1 "2018-10-22T18:31:12Z")

</div>

I am working with:

- sonarqube 7.3
- scanner 3.2.0.1227

I have a Gradle multi module

```auto
sonarqube-03
   sonarqube-03-domain
   sonarqube-03-repository
   sonarqube-03-repository-impl

```

I have just **one** `sonar-project.properties` file located in the root project `sonarqube-03` with the following content:

```auto
# must be unique in a given SonarQube instance
sonar.projectKey=manolito-labs:sonarqube-03
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=sonarqube-03
sonar.projectVersion=1.0

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=src
sonar.tests=src

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

#Manolito
sonar.exclusions=build/**,\
                 bin/**,\
                 gradle/**,\
                 gradlew,\
                 gradlew.bat,\
                 *.gradle
sonar.java.binaries=.

#Multi-Module
sonar.modules=sonarqube-03-domain,\
              sonarqube-03-repository,\
              sonarqube-03-repository-impl

# Properties can obviously be overriden for
# each module - just prefix them with the module ID
sonarqube-03-domain.sonar.projectName=Module Sonarqube 03 Domain
sonarqube-03-repository.sonar.projectName=Module Sonarqube 03 Repository
sonarqube-03-repository-impl.sonar.projectName=Module Sonarqube 03 Repository Impl

```

When I execute on Mac: `$SONARQUBE_SCANNER/bin/sonar-scanner`

I got in the final of the all the output:

```auto
...
INFO: Source paths: src
INFO: Test paths: src
INFO: Source encoding: UTF-8, default locale: en_US
INFO: Load server rules
INFO: Load server rules (done) | time=162ms
INFO: Index files
INFO: Excluded sources:
INFO: build/**
INFO: bin/**
INFO: gradle/**
INFO: gradlew
INFO: gradlew.bat
INFO: *.gradle
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 11.684s
INFO: Final Memory: 11M/167M
INFO: ------------------------------------------------------------------------
ERROR: Error during SonarQube Scanner execution
ERROR: File sonarqube-03-repository/src/main/java/com/manuel/jordan/repository/PersonRepository.java can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files
ERROR:
ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

```

If I execute with the debug mode ( `$SONARQUBE_SCANNER/bin/sonar-scanner -X` )

I got:

```auto
...
13:31:07.362 INFO: Source paths: src
13:31:07.363 INFO: Test paths: src
13:31:07.363 INFO: Source encoding: UTF-8, default locale: en_US
13:31:07.370 INFO: Load server rules
13:31:07.480 DEBUG: GET 200 http://localhost:9000/api/rules/list.protobuf | time=109ms
13:31:07.510 INFO: Load server rules (done) | time=140ms
13:31:07.663 DEBUG: Declared extensions of language Python were converted to sonar.lang.patterns.py : **/*.py
13:31:07.663 DEBUG: Declared extensions of language CSS were converted to sonar.lang.patterns.css : **/*.css,** /*.less,**/*.scss
13:31:07.664 DEBUG: Declared extensions of language Go were converted to sonar.lang.patterns.go : **/*.go
13:31:07.664 DEBUG: Declared extensions of language Kotlin were converted to sonar.lang.patterns.kotlin : **/*.kt
13:31:07.664 DEBUG: Declared extensions of language JavaScript were converted to sonar.lang.patterns.js : **/*.js,** /*.jsx,**/*.vue
13:31:07.664 DEBUG: Declared extensions of language C# were converted to sonar.lang.patterns.cs : **/*.cs
13:31:07.664 DEBUG: Declared extensions of language Java were converted to sonar.lang.patterns.java : **/*.java,** /*.jav
13:31:07.665 DEBUG: Declared extensions of language Flex were converted to sonar.lang.patterns.flex : **/*.as
13:31:07.665 DEBUG: Declared extensions of language XML were converted to sonar.lang.patterns.xml : **/*.xml,** /*.xsd,**/*.xsl
13:31:07.665 DEBUG: Declared extensions of language PHP were converted to sonar.lang.patterns.php : **/*.php,** /*.php3, **/*.php4,** /*.php5, **/*.phtml,** /*.inc
13:31:07.666 DEBUG: Declared extensions of language TypeScript were converted to sonar.lang.patterns.ts : **/*.ts,** /*.tsx
13:31:07.680 INFO: Index files
13:31:07.683 INFO: Excluded sources:
13:31:07.684 INFO: build/**
13:31:07.684 INFO: bin/**
13:31:07.684 INFO: gradle/**
13:31:07.684 INFO: gradlew
13:31:07.684 INFO: gradlew.bat
13:31:07.684 INFO: *.gradle
13:31:07.702 DEBUG: 'src/main/java/com/manuel/jordan/repository/PersonRepository.java' indexed with language 'java'
13:31:07.779 INFO: ------------------------------------------------------------------------
13:31:07.779 INFO: EXECUTION FAILURE
13:31:07.779 INFO: ------------------------------------------------------------------------
13:31:07.779 INFO: Total time: 3.231s
13:31:07.846 INFO: Final Memory: 11M/167M
13:31:07.846 INFO: ------------------------------------------------------------------------
13:31:07.846 ERROR: Error during SonarQube Scanner execution
13:31:07.846 ERROR: File sonarqube-03-repository/src/main/java/com/manuel/jordan/repository/PersonRepository.java can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files

```

For both outputs the problem is:

```auto
ERROR: File 
sonarqube-03-repository
/src/main/java
/com/manuel/jordan/repository/PersonRepository.java 
can't be indexed twice. 
Please check that inclusion/exclusion patterns produce disjoint sets for main and test files

```

Thus, what is missing in my current configuration?

---

<div class="post-metadata">

**Author:** ![janos](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/janos/32/60_2.png) [@janos](https://community.sonarsource.com/u/janos)\
**Post date:** [October 23, 2018, 5:24am UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/2 "2018-10-23T05:24:01Z")

</div>

The error message says ( **_emphasis_** mine):

> Please check that inclusion/exclusion patterns produce _ **disjoint** _ sets for main and test files

And in your configuration you have:

> ```
> sonar.sources=src
> sonar.tests=src
> 
> ```

In short, the sources and test files overlap. You need to make them not have any overlap.

More importantly, it’s unusual to use `sonar-scanner` with a Gradle project. We have a specialized [scanner for Gradle](https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle) which is a lot easier to use (only minimal configuration is needed), and it is more likely to produce the desired results.

---

<div class="post-metadata">

**Author:** ![agabrys](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/agabrys/32/2744_2.png) [@agabrys](https://community.sonarsource.com/u/agabrys)\
**Post date:** [October 23, 2018, 5:58am UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/3 "2018-10-23T05:58:36Z")

</div>

> <https://stackoverflow.com/questions/52633708/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice>

> [@Allow enabling sonar rules for test sources](https://community.sonarsource.com/t/allow-enabling-sonar-rules-for-test-sources/3379):
>
> Please create a configuration option that allows to apply all sonar rules that are evaluated for src/main/java also to be applied to any code in src/test/java I think it is a common coding guideline that test code has to fulfill the same rules as production code.

---

<div class="post-metadata">

**Author:** ![manueljordan](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@manueljordan](https://community.sonarsource.com/u/manueljordan)\
**Post date:** [October 23, 2018, 1:05pm UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/4 "2018-10-23T13:05:22Z")

</div>

I have the same behavior (problem) even with:

```auto
sonar.sources=.
sonar.tests=.

```

I thought the problem could be a wrong configuration pattern. But seems really the point is ignore or avoid `src/test/java`. Something related with:

- [Allow enabling sonar rules for test sources](https://community.sonarsource.com/t/allow-enabling-sonar-rules-for-test-sources/3379)

Thanks for the link about `Gradle` I will do a research.

---

<div class="post-metadata">

**Author:** ![manueljordan](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@manueljordan](https://community.sonarsource.com/u/manueljordan)\
**Post date:** [October 23, 2018, 1:06pm UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/5 "2018-10-23T13:06:43Z")

</div>

Yes, clear for the moment avoid `src/test/java`

---

<div class="post-metadata">

**Author:** ![seeteshh](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@seeteshh](https://community.sonarsource.com/u/seeteshh)\
**Post date:** [March 8, 2019, 1:05pm UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/6 "2019-03-08T13:05:07Z")

</div>

In my case I have only the src folder which doesnt have the java or test subfolder. But I am still getting the same indexing failure

---

<div class="post-metadata">

**Author:** ![lmonahan](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/lmonahan/32/13218_2.png) [@lmonahan](https://community.sonarsource.com/u/lmonahan)\
**Post date:** [September 30, 2020, 7:33pm UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/7 "2020-09-30T19:33:05Z")

</div>

I was seeing this problem in an Android project for assets stored in `app/src/debug/assets`. I tried setting `sonar.sources` and `sonar.tests` properties to use disjointed sets but I wasn’t able to resolve the error.

To fix it I changed:

`property "sonar.coverage.exclusions"`

to:

`property "sonar.exclusions"`

in order to ignore the **/assets/** directory completely. For example:

`property "sonar.exclusions", " **/assets/** , **/test/** , ....`

---

<div class="post-metadata">

**Author:** ![Sara\_Han](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/sara_han/32/50803_2.png) [@Sara\_Han](https://community.sonarsource.com/u/Sara_Han)\
**Post date:** [October 24, 2023, 12:59pm UTC](https://community.sonarsource.com/t/sonarqube-scanner-execution-fails-java-cant-be-indexed-twice/3576/8 "2023-10-24T12:59:19Z")

</div>

I am also having problem with this issue.  
I use Sonar scan for my Android project, and it keeps failing when trying to index .xml files under res directory.  
Because the security team required us to scan all resource files, I cannot simply exclude the res folders.  
(Problem disappears when I do `sonar.excludes=" **/res/**"`)  
I cannot find any duplicate .xml file under main & test folders.  
I have tried sonar.sources=“all my source file dir paths” sonar.tests=“all my test file paths” explicitly, and it does not help.  
I am quite lost in this issue. Any help?
