SonarQube CRLF detection using checkstyle rules - sonarqube

After migrating maven-checkstyle-plugin to SonarQube, I face some oddity how line feeds and tab chars are transfered.
Back in my Maven build I had the following substantial rules:
<module name="Checker">
<property name="fileExtensions" value="java, xml"/>
<!-- forbid dos/windows lf -->
<module name="RegexpMultiline">
<property name="format" value="\r\n" />
<property name="message" value="Do not use Windows line endings."/>
</module>
<!-- forbid tab character -->
<module name="FileTabCharacter">
<property name="eachLine" value="true" />
</module>
</module>
While checking the SonarQube report I don't see these violations, where they should be. What am I doing wrong here?
PS: SonarQube got this snippet as configured QG/QP.

Your analysis is now based on the rules that are active in your SonarQube Quality Profile. You should re-create your rules in SonarQube.

Related

Where do I put module elements in Checkstyle config file?

I've a simple problem, when I run maven checkstyle, and then go to check what are the problem, I have that:
Line is longer than 80 characters (found 98).
Do someone know how to set it more than 80 characters ? I found few things on the internet, but they don't tell where I need to put that:
<module name="LineLength">
<property name="max" value="120"/>
</module>
Do someone have a clue ?
Cordially
In general, you need to put these module configs into a config file such as the following.
Note: Where LineLength goes depends on the Checkstyle version. Checkstyle 8.24 and higher: directly under Checker; Checkstyle < 8.24: under TreeWalker.
Example for Checkstyle 8.23 and earlier:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<!-- FileSetChecks go here ("parent = Checker") -->
<module name="TreeWalker">
<!-- TreeWalker checks go here ("parent = TreeWalker") -->
<module name="LineLength">
<property name="max" value="120" />
<property name="ignorePattern" value="^\s*\*\s*\S+$" />
</module>
</module>
</module>
As mentioned in another answer, you can subsequently tell Maven where your config file is, or you can specify it inline (not recommended). In either case, you will need to obtain a copy of the config file you are using now, and modify it.
In plain old Maven Checkstyle 3.0.0, the config file would be this one. If you specify a Checkstyle version in your Maven config, modify the version number in the URL to match it.
you can configure a custom checkstyle configuration by configuring the maven-checkstyle-plugin (which you obviouisly already use)
you can use custom-checker-config or inline-checker-config

Infinispan/JDBC as Backend for Hibernate Search on Wildfly/JBoss

I am trying to configure a JDBC-backed Infinispan cache to act as the backend for my Java EE app making use of Hibernate Search. I am deploying on JBoss EAP 7.0 or Wildfly 10. I have a module, cache container, and persistence.xml configuration that does not give me any errors on startup. In addition, I am able to create JPA objects and have them indexed via Hibernate Search as expected. I am then able to query those objects successfully. However, at no time are the SQL tables created in the database that I have configured as my JDBC data source for the cache container. So, obviously, the search indices only exist in memory and are not persisted across app server restarts. Here is what I have done thus far:
Downloaded the Infinispan 8.1.x release that corresponds to the Infinispan release embedded within JBoss EAP. This was done because the hibernate-search modules from Infinispan are not included in the embedded module
I've configured the appropriate modules for the Infinispan hibernate-search module within JBoss EAP
Modified my standalone-full-ha.xml JBoss EAP configuration file to include a JDBC-backed cache-container and cache definitions
Modified my persistence.xml file to make use of an Infinispan cache manager and directory provider
Here is the definition for my cache-container as found in standalone-full-ha.xml:
<cache-container name="hibernateSearch" default-cache="LuceneIndexesData" module="org.infinispan.cachestore.jdbc" jndi-name="java:jboss/infinispan/hibernateSearch">
<transport lock-timeout="60000"/>
<replicated-cache name="LuceneIndexesMetadata" statistics-enabled="true" mode="SYNC">
<binary-keyed-jdbc-store data-source="InfinispanCacheDS" passivation="false" purge="false" shared="true">
<binary-keyed-table>
<id-column name="ID_COLUMN" type="VARCHAR(255)"/>
<data-column name="DATUM" type="BYTEA"/>
</binary-keyed-table>
</binary-keyed-jdbc-store>
</replicated-cache>
<replicated-cache name="LuceneIndexesData" statistics-enabled="true" mode="SYNC">
<binary-keyed-jdbc-store data-source="InfinispanCacheDS" passivation="false" purge="false" shared="true">
<binary-keyed-table>
<id-column name="ID_COLUMN" type="VARCHAR(255)"/>
<data-column name="DATUM" type="BYTEA"/>
</binary-keyed-table>
</binary-keyed-jdbc-store>
</replicated-cache>
<replicated-cache name="LuceneIndexesLocking" statistics-enabled="true" mode="SYNC"/>
</cache-container>
Here is my JDBC data source from standalone-full-ha.xml:
<datasource jndi-name="java:jboss/datasources/InfinispanCacheDS" pool-name="InfinispanCacheDS" enabled="true" use-java-context="true" statistics-enabled="true">
<connection-url>jdbc:postgresql://localhost:5432/db_infinispan_cache</connection-url>
<driver>postgresql-jdbc4</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>20</max-pool-size>
<prefill>true</prefill>
<flush-strategy>IdleConnections</flush-strategy>
</pool>
<security>
<user-name>infinispan_cache</user-name>
<password>mypassword</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLValidConnectionChecker"/>
<validate-on-match>true</validate-on-match>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.postgres.PostgreSQLExceptionSorter"/>
</validation>
<statement>
<track-statements>true</track-statements>
</statement>
</datasource>
Here is my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="MyPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jdbc/datasources/MyDataSourceDS</jta-data-source>
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:/MyDataSourceEntityManagerFactory" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.connection.release_mode" value="auto" />
<!-- Transactions -->
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="hibernate.transaction.flush_before_completion"
value="true" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup" />
<property name="hibernate.max_fetch_depth" value="5" />
<!-- Caching support - Infinispan -->
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<property name="hibernate.cache.infinispan.cachemanager"
value="java:jboss/infinispan/container/hibernate" />
<property name="hibernate.cache.use_second_level_cache"
value="true" />
<!-- Hibernate Search properties - Generic -->
<property name="hibernate.search.reader.strategy" value="shared" />
<property name="hibernate.search.worker.execution" value="sync" />
<property name="hibernate.search.jmx_enabled" value="true" />
<!-- Hibernate Search properties - Infinispan -->
<property name="hibernate.search.infinispan.cachemanager_jndiname"
value="java:jboss/infinispan/hibernateSearch" />
<property name="hibernate.search.default.directory_provider"
value="infinispan" />
<property name="hibernate.search.infinispan.chunk_size"
value="300000000" />
</properties>
</persistence-unit>
</persistence>
When JBoss starts up, I do not see any errors. I also don't see any reference to JDBC, however. I also do not see any errors when persisting JPA objects, so it seems they are being indexed appropriately. It's just that my Hibernate Search index isn't being saved in the database as I would expect.
Can anyone shed some light on what I'm missing here?
As you noticed, the Infinispan extensions used by Hibernate Search for this purpose are not included within the Infinispan module which is part of WildFly / JBoss EAP, so you correctly downloaded the Infinispan modules from the Infinispan project.
What you are missing is that WildFly is able to isolate modules very effectively, so the first thing you have to realise is that you really don't have to match the version of Infinispan as included in WildFly.
Since you will be using the module set from infinispan.org, you should NOT configure these caches in your JBoss EAP configuration file, as the caches defined there are controlled by the clustering subsystem and will affect the cache definitions created by the Infinispan modules included in WildFly (the Infinispan modules at slot "main").
You should include an Infinispan configuration file in your Hibernate Search based application, and have it start a new CacheManager using the right module.
Alternatively you can create another application to start the CacheManager in any way you like - as long as you depend on the right Infinispan modules (avoid the "main" slot) - then register it to JNDI and have Hibernate Search look for that name.
N.B. the Hibernate Search module has a dependency to the optional Infinispan module, so it will attempt to load the right Infinispan module if it's present:
https://github.com/wildfly/wildfly/blob/84d88b8/feature-pack/src/main/resources/modules/system/layers/base/org/hibernate/search/engine/main/module.xml#L53
Be aware also that thanks to the module system, you can override / upgrade the Hibernate Search version.
In terms of versions your restrictions are :
pick an Infinispan module version compatible with your Hibernate Search version of choice
pick an Hibernate Search version compatible with the Hibernate ORM version of choice
(That's right, you can override / upgrade Hibernate ORM as well).
Assuming you are using the default version of Hibernate ORM and Hibernate Search as included in WildFly 10, you could download the Infinispan modules at version 8.2.6.Final (the latest stable release) as it also contains a module
<module name="org.infinispan.hibernate-search.directory-provider" slot="for-hibernatesearch-5.5" >
Or if you're using JBoss EAP, you might prefer to download the JBoss Data Grid distribution, which will contain the same features as the Infinispan modules.

How to attach the Sonar JUnitListener when not using maven?

I have a Sonar analysis configured to use sonar-ant-task.
JUnit tests are executed with the Eclipse test framework (as a JUnit Plug-in test, using the PDE) and it uses the JaCoCo agent for the on-the-fly instrumentation.
The results are then stored in the directory ./junit/results/ in JUnit XML format.
Here is the configuration for the Sonar (using Ant):
<property name="sonar.projectKey" value="my-project" />
<property name="sonar.projectName" value="My Project" />
<property name="sonar.working.directory" value="./.sonar" />
<property name="sonar.sources" value="$./src" />
<property name="sonar.java.binaries" value="./bin" />
<property name="sonar.java.libraries" value="./lib" />
<property name="sonar.tests" value="./tests" />
<property name="sonar.dynamic" value="reuseReports" />
<property name="sonar.junit.reportsPath" value="./junit/result" />
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.jacoco.reportPath" value="./jacoco/jacoco.exec" />
In the directory junit/result there is a file with name AllTests.xml that has this content (sensitive part in the properties section was ommitted):
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>
<testsuite errors="0" failures="0" hostname="vmtnetci002" id="0" name="AllTests" package="com.example.company.project.tests" tests="2" time="100.000" timestamp="2015-07-09T17:31:51">
<properties>
<!-- ... -->
</properties>
<testcase classname="com.example.company.project.tests.MyTest" name="test1" time="50.000" />
<testcase classname="com.example.company.project.tests.MyTest" name="test2" time="50.000" />
<system-out><![CDATA[]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
</testsuites>
Code coverage is reported correctly to Sonar but during the Sonar analysis it shows this message in the log:
...
INFO - Sensor SurefireSensor
INFO - parsing ./junit/results
INFO - Sensor SurefireSensor (done) | time=23ms
...
INFO - No information about coverage per test.
And then I don't have information about JUnit tests result in Sonar, so I think it might be related with the message above.
How can I attach the sonar JUnitListener when running the tests?

Sonar Ant Task running in compatibility mode

I have the following ant project settings for sonar:
<!-- list of Sonar project related properties -->
<property name="sonar.projectName" value="bla bla" />
<property name="sonar.projectKey" value="com.my.project" />
<property name="sonar.projectVersion" value="7.3" />
<property name="sonar.language" value="java" />
<property name="sonar.modules" value="admin-api" />
<!-- all of the sub-modules -->
<property name="admin-api.sonar.projectName" value="admin-api" />
<property name="admin-api.sonar.projectBaseDir" location="server/admin/admin-api"/>
<property name="admin-api.sonar.sources" value="src" />
<property name="admin-api.sonar.binaries" value="build" />
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant">
<!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
<libraries>
<path>
<fileset dir="server/third-party-jars" includes="*.jar" />
</path>
</libraries>
</sonar:sonar>
I am getting 1 warning :
[sonar:sonar] *****************************************************************************************************************************************
[sonar:sonar] /!\ Sonar Ant Task running in compatibility mode: please refer to the documentation to udpate your scripts to comply with the standards.
[sonar:sonar] *****************************************************************************************************************************************
And also The following error:
The following mandatory information is missing:
- task attribute 'key'
- task attribute 'version'
I understand that there were changes migrating to the new sonar ant task 2.0 but couldn't find any place where the changes were specified.
Also, I understand that all of the modules settings now have to be specified in the parent build.xml only and not in the sub-modules build.xml files. So why do I get the errors ? I followed the example in https://github.com/SonarSource/sonar-examples/tree/master/projects/multi-module/ant . Am I missing something ?
Thanks
You're in the compatibility mode because you have defined "libraries" inside the "sonar:sonar" tag. Instead, you should define "sonar.libraries" outside of this tag, by joining all the JAR file paths with a comma.
Then the multi-module mechanism will work as expected.
I am facing the same issue. In my case, the build.xml looks like belows:
<target depends="build" name="sonar-runner">
<!-- Define the SonarQube properties -->
<property name="sonar.host.url" value="http://sonar-server:9000/sonar" />
<property name="sonar.jdbc.url" value="jdbc:jtds:sqlserver://comp-sql-db-server/sonar;SelectMethod=Cursor" />
<property name="sonar.jdbc.username" value="XXXXXX" />
<property name="sonar.jdbc.password" value="*******" />
<property name="sonar.projectKey" value="asdfsaf:2550" />
<property name="sonar.projectName" value="Proj 1" />
<property name="sonar.projectVersion" value="4.0.1" />
<property name="sonar.language" value="java" />
<property name="sonar.profile" value="Sonar Way" />
<property name="sonar.sources" value="src" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<!-- run sonar for this project -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<!-- Update the following line, or put the "sonar-ant-task-*.jar" file
in your "$HOME/.ant/lib" folder -->
<classpath path="${lib-dist}/sonar/sonar-ant-task-2.1.jar" />
</taskdef>
<!-- Execute Sonar -->
<sonar:sonar />
</target>
This means that I have setup ant for single project but don't know why its requiring for Key and Version even its defined in properties even I didn't provide any thing inside the sonar:sonar tag.
Thanks

Mandatory properties missing when migrating to new 2.0 ant task sonar

I have a multi module project (with many sub-modules) defined in Sonar and it was working great until i've upgraded sonar to the newer version.
My previous settings were:
a single parent build.xml that contain all of the general project properties like jdbc connection etc. Also it contained the <sonar:sonar /> task to run Sonar. This parent project does not contain any source / binaries, just the instruction to build the sub-modules.
many sub-modules build.xml files that contained only the sub-module specific properties like sonar.projectKey, sonar.sources and sonar.binaries
Now in the new Sonar ant task I need to define everything in the parent buid.xml. I have defined it in the following way (I only show one sub-module here, I assume it should work the same if I add more modules later) :
<target name="sonar">
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath path="build-utils/lib/sonar-ant-task-2.0.jar" />
</taskdef>
<!-- list of Sonar database related properties -->
<property name="sonar.jdbc.url" value="jdbc:oracle:thin:#localhost/DB11g" />
<property name="sonar.jdbc.driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<!-- for security reasons, pass these parameters from outside -->
<property name="sonar.jdbc.username" value="" />
<property name="sonar.jdbc.password" value="" />
<property name="sonar.host.url" value="http://localhost:9000" />
<!-- list of Sonar project related properties -->
<property name="sonar.projectName" value="My Project" />
<property name="sonar.projectKey" value="com.my.project" />
<property name="sonar.projectVersion" value="7.3" />
<property name="sonar.language" value="java" />
<property name="sonar.libraries" value="server/third-party-jars"/>
<property name="sonar.modules" value="admin-api" />
<!-- all of the sub-modules -->
<property name="admin-api.sonar.projectName" value="admin-api" />
<property name="admin-api.sonar.projectBaseDir" location="server/admin/admin-api"/>
<property name="admin-api.sonar.sources" value="src" />
<property name="admin-api.sonar.binaries" value="build" />
<sonar:sonar xmlns:sonar="antlib:org.sonar.ant" />
</target>
When I run the target I get the following error:
You must define the following mandatory properties for 'com.orca.rightv': sonar.sources
Another question, is there any way to keep the specific definition of the sub-modules in each module's build.xml like I did in the previous version ? That would save me a lot of work.
Thanks
There's a bug in the current version of the Ant Task, which is related to the version of the Sonar Runner API embedded. This will be fixed in the next version: http://jira.codehaus.org/browse/SONARPLUGINS-2818
Meanwhile, you can define "sonar.sources" at the top, its value will be inherited in to modules (and obviously you can still override it in modules).
As for the definition of properties at module level, you can define those properties in a "sonar-project.properties" file that you put at the root of the module, like:
sonar.projectName=My Project
# following needed only to override the values defined in top build.xml
sonar.sources=src
sonar.binaries=build
, and just leave "admin-api.sonar.projectBaseDir" property (like you did) in the top build.xml file.

Resources