how to make checkstyle ignore xml files in my application - static-analysis

i've configured my checkstyle plugin with customized checkstyle.xml and it works fine with the java classes but having a lot of warnings with the xml files can anyone suggest how to disable the check style from any xml file in my application.
thanks in advance.

I can think of two ways to do that:
You could suppress findings from non-Java files. This works very well inside and outside of your IDE.
If you are using Eclipse, you can also configure Eclipse not to feed the non-Java files to Checkstyle. In order to do that, right-click your project and click Checkstyle. Uncheck use simple configuration. Specify a file set using \.java$ as regexp. You will see the list of files in the bottom half of the dialog window change to only Java files.
In earlier versions of Checkstyle, I think I remember that there also was a file name filter in the configuration XML, but I can't seem to find it in the docs now, so maybe that feature is no more.

There is a basedir property at the start of the Checker module in the check style config file. Uncomment it if it is commented.
Set it's value to the folder you want to apply your checkstyle rules to.
E.g. src folder of any eclipse project only contains java files.
<!-- If you set the basedir property below, then all reported file names
will be relative to the specified directory. See http://checkstyle.sourceforge.net/5.x/config.html#Checker -->
<property name="basedir" value="/MyEclipeProject/src"/>

Related

How to make configuration read from an xml file available at configuration phase of gradle build?

I have a ton of ant projects which I want to migrate to gradle. All projects have in common a config.xml file. This file exists for reasons independent of the build, but I want to keep its information in a single place, so I evaluate it during the ant build as well.
I created a custom plugin with a PluginExtension and a ParserTask. The PluginExtension allows the user to provide the config.xml path as a propertie. The ParserTask locates the file automatically or using the provided path and parses the file if found. It then fills the remaining PluginExtension properties with the respective configuration data from config.xml.
This is working perfectly but has a major drawback: since parsing is done in a task, the respective properties of the PluginExtension are not filled with the information from config.xml during configuration phase of my build. This leads to all kind of smelly workarounds in my build file because I can't set the archives basename at configuration phase or configure the manifest in that phase, so I need to update this information during the execution phase.
How can I parse the config.xml during my build such that the properties are already filled with the correct values during configuration phase?
Moving the configuration information to a properties file and regenerating the config.xml during build time is NOT an option, because I need to maintain the ant build for a while as well. Additionally I need to config.xml file to be part of the version control.
Edit: despite having found an answer myself, I'm still interested in other responses, especially if they address the downside that I have left.
I now moved the parsing into the PluginExtension and called the parser from the PluginExtension Constructor. One downside is left. I can no longer configure where to search for the config.xml using that extension. Instead I have to rely entirely on file search. This is OK for my current use case.

SonarQube excluding files, directories, and generated code?

The code base I am working with has a lot of generated code. In addition, there are also some deprecated files that I would want to exclude from SonarQube analysis. I've read up the documentation and looked at some answers on here about that, but it does not help in my case.
I have a multi-module maven project. So I have multiple projects in my workspace that are all part of a large application. Say I want to exclude this file:
/home/username/workspace/com.mst.rtra.importing.message/bin/com/mst/rtra/importing/message/idl/parse/idlparser.java
I don't really know how to write this in the exclusions settings on SonarQube because of how long the filepath is. Also, what if I want to exclude another file, but from a different module, say :
/home/username/workspace/com.mst.rtra.interpreter.create/
I am confused about I should write this in the exclusions box in project settings. Should I write the absolute file path due to the multi-module nature of this project? Or is there some other convention used?
In addition, if I want to exclude generated files from analysis, I would need to put file:/generated-sources/ as I saw in another answer. However, after analysis, I can still view the analysis results of those files when I open up the project in SonarQube dashboard.
We use ant rather than maven, and an older version of the Sonar ant task at that. But what works for us is setting a sonar.exclusions property in our build.xml, which accepts wildcards for filenames. For example:
<property name="sonar.exclusions" value="**/com/ex/wsdl/asvc/*.java,**/com/ex/wsdl/bsvc/*.java"/>
That skips analyzing all the code generated from a wsdl file for two services. You ought to be able to do something similar for maven.

Configure Sonar and maven to analyse multilingual project

I wonder if it is possible to specify different languages in the sonar-project.properties file to analyse, for example, both java script and xml, which are in the same folder?
Thanks for your help!
ADDITION
for example, I have the following project structure:
client
pom.xml
|
|-----UI
|-----pom.xml
|
|-----core
|-----pom.xml
In UI folder I have different types of files (js, html, xml) and maven java project in core folder. Can I aim sonar to UI folder so that it will check .js, .html and .xml files in this folder and subfolders recursivly? I tried to specify several languages (separated by comma) for one module, but that had no effect. Thanks!
Yes, this is possible. You can check out the following "web+Javascrip" sample project: https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/multi-language/multi-language-source-files-in-same-directory
Please not that there are some requirements on Sonar and Sonar Runner versions (described in the README file).

Adding entries to a properties file before maven compile phase

We are using maven filtering for properties file, in which we inject the build number and time from out Jenkins server.
It would be cool if we could add there properties to our app.local.properties file dynamically, thus not forcing each project maintainer to add them (one time operation, but annoying never the less).
I would like to
Add params to a properties file, before the filter plugin kicks in.
If the destination properties file does not exist -> create it and then add.
I need to edit the properties file in the target directory (the src/main/resources dir should remain untouched).
Nice to have:
Add configuration settings only if they aren't already defined in the file (to avoid duplicating entries added manually)
Is there some maven plugin / hook that I can tap into to do this?
you could build a custom maven build extension and retrieve those properties dynamically from a cetntral service and inject them into the POM in memory, right after the build process starts. Indeed that is how we solved this very problem in our company setup.
I've just posted a full recipe to this Stackoverflow answer
I see a conflict here:
Add params to a properties file, before the filter plugin kicks in.
So you need to edit the properties in src/main/resources, as filter plugin works on these, but at the same time:
I need to edit the properties file in the target directory (the src/main/resources dir should remain untouched).
What I understand is, you'd like to do this filtering somehow "on the fly". I don't think Maven mechanisms could do it; I'd do it completely with the Antrun plugin (excluding the property file from Maven handling) which I'd invoke in the process-resources phase. There's a propertyFile task for editing and saving property files; after saving your changes, you could process the file once more with the regexp task, for example, to apply your filters.

converting an eclipse formatter file to a checkstyle file

I have an eclipse formatting file (which contains some configurations) and i want to make the same thing (these configurations) for a checkstyle file to use after with maven build.
Unfortunately, there is no way to convert a formatter file to a checkstyle ruleset at the moment. You have to do it the other way around as explained here.
Otherwise you have to adjust every setting in your formatter manually for checkstyle.
The Eclipse-Checkstyle plugin provides an export feature to export your preferences.

Resources