can't be indexed twice - testSourceDirectory and sourceDirectory are same - maven

I have created performance test as a maven submodule to my main module. All the test classes are written under src/main/java and not src/test/java
I am able to package the project as jar and run it to performance test my project.
I wanted to execute mvn test. For mvn test to work I should have <testSourceDirectory> value set. As in this case I have my code in src/main/java I set this to :
<testSourceDirectory>src/main/java</testSourceDirectory>
Now mvn test works.
But the Problem is sonar build fails with error complaining: can't be indexed twice. Which is obvious as for my pom testSourceDirectory and sourceDirectory are same.
[ERROR] Failed to execute goal
org.codehaus.mojo:sonar-maven-plugin:2.5:sonar (default-cli)
on project Blah: File [relative=XYZ.java, abs=/Path/XYZ.java] can't be indexed twice.
Please check that inclusion/exclusion patterns produce
disjoint sets for main and test files ->
How to fix this issue?

I was facing the same problem. Finally, solved it with help of below documentation:-
https://github.com/SonarOpenCommunity/sonar-cxx/wiki/FAQ
Q: ERROR: Caused by: File [...] can't be indexed twice.
A: In case of below error you have to verify your exclusion/inclusion
properties. Please check that inclusion/exclusion patterns produce
disjoint sets for source and test files
ERROR: Caused by: File [...] can't be indexed twice. Please check that
inclusion/exclusion patterns produce disjoint sets for main and test
files An example could look like this:
sonar.sources=.
sonar.tests=.
sonar.test.inclusions=**/*Test*/**
sonar.exclusions=**/*Test*/**

This is not a standard Maven usage but you can easily fix SonarQube analysis using exclusions.
sonar.exclusions=src/main/java/**
or
sonar.test.exclusions=src/main/java/**
depending on whether you want your source files to be considered as tests or main files.
But the proper Maven way would be to put your tests in src/test/java and ackage your tests:
https://maven.apache.org/guides/mini/guide-attached-tests.html

If project does not follow default Maven directory structure then in project's pom you can explicitly specify where is located the part of source code and the part of tests:
<properties>
<sonar.sources>src/main/foo</sonar.sources>
<sonar.tests>src/test/bar</sonar.tests>
</properties>

I was seeing this can't be indexed twice error when running sonarqube Gradle task on an Android project. The issue related to files 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 the error I changed:
property "sonar.coverage.exclusions", "**/assets/**, ..."
to:
property "sonar.exclusions", "**/assets/**, ..."
in order to ignore the /assets/ directory completely.

Related

Can Sonar exclude files outside maven project? Also, can it exclude files other than Java?

I have a use-case where I have few image files (for my README page) outside of the maven project. I want Sonar analysis to skip performing analysis on that file. I tried keeping the file in the main project pom as below:
<sonar.exclusions>**/*.png</sonar.exclusions>
This still gives me the error:
[sonar4bitbucket] Failed to parse diff: string matching regex `\z' expected but `B' found
Any suggestions to skip .png images being allowed outside of maven project with Sonar analysis ?
you can configure exclusions in the UI:
At the global level, the navigation path is Administration > General
Settings > Analysis Scope. At the project level, the navigation path
is Project Settings > General Settings > Analysis Scope
Source: Narrowing the focus
If you want to stay with maven properties, also make sure you do not override those in the build using mvn … -Dsonar.exclusions=….
Also keep in mind that those property-basedd exclusions are not being stored in the Sonar DB. Therefore you might see an old coverage check jumping in which you might want to exclude as well:
<sonar.coverage.exclusions>
**/*.gif,
**/*.png
</sonar.coverage.exclusions>

How to configure a sonar-project.properties file for code coverage?

Current my scanner is running through and only scanning the parent and skipping the rest of my nested files. If I run sonarlint (using the cli and specifying some test and source files) , it tries to analyze 37k files instead of the few I need. I have been able to skip ~3k files by adding the <sonar.skip>true</sonar.skip> property to a pom file. However, I still can't configure the project to run across certain sub-folders and print out some kind of code coverage test. (Is JaCoCo needed for the latest version(6.3,0)? Or can code-coverage be handled through some configuration?).
If Sonar seems to be analyzing too many files, it is probably because you had not set the sonar.sources=src/main/java in your sonar-project.properties file, so it defaults to the basedir and includes everything.
SonarQube can't do code-coverage itself, it just reports on coverage-reports from a tool like JaCoCo. It is funny they don't clarify these things in https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner --but with enough digging, you can find good info on that site.

Tycho missing plugin when building an update site

When I build my project by tycho-maven-plugin, I encounter the following message:
[ERROR] Cannot resolve project dependencies:
[ERROR] Software being installed: foo-updatesite raw:0.2.0.'SNAPSHOT'/format(n[.n=0;[.n=0;[-S]]]):0.2.0-SNAPSHOT
[ERROR] Missing requirement for filter properties ~= $0: foo-feature.feature.group 0.2.0.qualifier requires 'foo-plugin 0.0.0' but it could not be found
[ERROR] Cannot satisfy dependency: foo-updatesite raw:0.2.0.'SNAPSHOT'/format(n[.n=0;[.n=0;[-S]]]):0.2.0-SNAPSHOT depends on: foo-feature.feature.group [0.2.0,0.2.1)
This error occurs when building an update site. Plugin and feature are build normally. I build the update site from PDE without issues.
The project structure is common: there is a parent project with pom packaging and three modules: plugin, feature and updatesite. Of course, I invoke mvn clean package from the parent project.
What can be the reason for this error? How can I debug it?
UPD: I tried many things: removed "grandparent" pom, removed SNAPSHOT/qualifier, played with configuration. However, I found the solution: I switched to tycho 0.19 instead of 0.21. So I suppose there is a bug in tycho 0.21.
The error message
Missing requirement for filter properties ~= $0
indicates that you have defined an environment filter when you include the plugin foo-plugin in the feature.xml. However this does not explain the dependency resolution error you are seeing. The only way I can reproduce the error message is by adding an Eclipse-PlatformFilter header in the manifest of the plugin using a value which doesn't match environments configuration in the POM.
I had the same error message:
Missing requirement for filter properties ~= $0: foo-feature.feature.group 0.2.0.qualifier requires 'foo-plugin 0.0.0' but it could not be found
For some reason this error (also) means, that foo-plugin is present... twice.
So in your case, open the "Content" tab of the "Target Editor" and make sure that foo-plugin is not present in two distinct versions. I common way this could happen is if two features contain the same bundle.

tycho-p2-repository-plugin fails with "No content specified for p2 repository"

I am trying to build a p2 repository using Tycho and the tycho-p2-repository-plugin in an eclipse-repository packaging type. When I run the build, it is giving me the following exception:
Failed to execute goal org.eclipse.tycho:tycho-p2-repository-plugin:0.13.0:assemble-repository (default-assemble-repository) on project com.mycompany: No content specified for p2 repository
Can someone point me to the cause of this problem?
You need either of the following files in the root of the eclipse-repository project:
A category.xml files with at least one feature referenced in it
A *.product file
These files define what should be included in the p2 repository, so if there is none of these, the p2 repository would be empty. This is probably not what you wanted, so Tycho fails the build. (Admittedly, the error message could be more helpful...)
I had this problem too, unfortunately it was one of many problems and I do not remember which thing I did to fix it.
Make sure you are using 0.14.1 of tycho.
My working version is a very empty pom.xml, much like Packaging Types
I have not needed to configure the eclipse-repository via tycho-p2-repository-plugin.
Do you have a valid target definition defined in your reactor build somewhere?
Is your *.product file valid? The product file needs to be in the same directory as your eclipse-repository pom, I think it gets found via black magic.
Also make sure your product feature is separate from your eclipse-repository.
under PDE builds you could have the *.product file and the with the feature that is the root for your product.

Generate SQL-scheme and copy it to include to WAR-file: problem with plugins' dependencies

Before running integration tests I need to setup database. This include generating SQL-scheme (based on JPA-annotations), save it to file and copy it with other resources to directory which later will be used for creating war file (which will be deployed to Jetty).
So, I use following hibernate3-maven-plugin configuration for generating SQL-scheme: http://pastebin.ubuntu.com/606229/
To copy resulting hsql-scheme.sql from src/env/test/WEB-INF/classes to target/ where it will be packaged to WAR-file I use following:
http://pastebin.ubuntu.com/606230/
But when I run mvn verify -P test (yes, all these code inside separate profile) I've got: http://pastebin.ubuntu.com/606231/
As you can see hibernate3:hbm2ddl does not generate any SQL-scheme and resulting file is empty (in other case we will see it on console also). And this is a problem.
The root of problem (as I understand) is because hibernate3:hbm2ddl invokes the execution of the lifecycle phase process-resources prior to executing itself.
Thanks in advance!
This question helped me to fix schema' creation:
call hibernate3:hbm2ddl at prepare-package phase instead of generate-resources
put result of hibernate3:hbm2ddl directly to target/${build.finalName}/WEB-INF/classes
So, now I still have hibernate3:hbm2ddl invokes the execution of the lifecycle phase process-resources prior to executing itself warning, but file target/${build.finalName}/WEB-INF/classes/hsql-scheme.sql not empty as before.

Resources