Excluding specific set of files from lint issue report - go

I am using gometalinterv2 in my Go project for linting. After the lint report is generated, the report file is linked to sonarqube for analysis and presentation.
I want to exclude some files like *_test.go from linting. I know there is a --exclude flag for gometalinterv2 to exclude folders. But since _test.go files are in the same folder/package as the source code, this won't work.
So is there any way to achieve this (either at linting stage or in sonar properties file)?

Add config file .gometalinter.json to the root of your project and specify rules for excluding:
{
"exclude": [
".*_test.go",
"/any/folder/"
]
}

I found another way after I marked #bayrinet's answer. The files (not just folders) to be excluded can also be passed to the command using the exclude flag like below -
>gometalinter.v2 ./... --exclude=somefolder --exclude=.*_test.go

Related

How to exclude generated code from jacoco coverge report?

I have a maven project and the below step is mentioned right after executing surefire tests(for JUnit) and failsafe (for Integration tests). However, I am not able to exclude the files from generated-sources folder. However, if I use a single exclusionPattern:'/tomcat/', it is excluding tomcat folder from the report
I have tried below option:
**```
post {
always {
junit allowEmptyResults: true, testResults: '**/target/failsafe-reports/*.xml'
step( [ $class: 'JacocoPublisher', exclusionPattern: '**/target/generated-sources/**,**/tomcat/**'] )
}
}
```**
but it is only excluding the tomcat folders and not the generated-sources. Still seeing files from this folder in coverage report.
As a workaround, I am targeting the java packages inside target directory to improve coverage. It's not a very efficient way to do it as I had to add entries for multiple packages inside exclusionPattern. But, it works well for my requirement.

Gradle: Include dynamically generated file in build?

During my gradle build, I generate a temporary buildinfo.properties file containing things like Git commit info, build time, etc. I would like to include this file in my output *.jar / *.war files as a resource. However, I do not want to put this file in my project src/ folder (this would require fiddling with .gitignore and in general it just seems unnecessary to me). Ideally, the developer shouldn't even see this file at all, it should just be contained in the output archive.
How would you include a dynamically generated text file in a Gradle build?
Add that file in a jar task (Kotin DSL):
tasks {
val jar by getting(Jar::class) {
from("build/buildinfo.properties")
}
}
It will add build/buildinfo.properties file (assuming you generate it there with another taks) to the root of your JAR.
For dynamically generated file, standard gradle way to process resources is the gradle task called processResources. You can do something like this:
processResources {
dependsOn taskThatGeneratesYourBuildinfo
from("build/buildinfo.properties") {
into("desired/path/in/jar")
}
}

How do I prevent Gradle from building a non-project directory?

In the Gradle samples (included with version 2.2.1) there is a java/multiproject project.
The settings.gradle file defines the following projects:
include "shared", "api", "services:webservice", "services:shared"
Note that services is not itself a project, merely a directory which contains the webservice and shared projects.
When I run the command gradle build from the root directory, I notice that after gradle successfully builds it creates inside the /services directory a /build directory containing /lib and a /tmp directories.
Inside of /services/build/lib is a jar: services-1.0.jar which contains very little; specifically just a META-INF/MANIFEST.MF file containing:
Manifest-Version: 1.0
provider: gradle
So what is causing Gradle to build a jar for this non-project? And how can I prevent this behavior in my similarly structured multiproject project?
/services isn't a project, I don't want to create anything inside /build folder at all. Yes I could just delete it, but I would like to avoid the unnecessary work of building this jar/running any tasks on this non-project in the first place.
To be honest I've no reasonable idea why gradle builds this folder. I guess that because it's a kind of a transient folder. However it can be excluded by adding the following piece of code to main build.gradle script:
project(':services').jar { onlyIf { false } }
Desired effect (services.jar elimination) can be also obtained with the following settings.gradle content:
include "shared", "api", "services/webservice", "services/shared"
File instead of project paths are included.
My guess would be that this is a combination of the next 2 gradle rules:
When you're including subprojects in the build.settings file using the include keyword according to Gradle Documentation here:
the inclusion of the path 'services:hotels:api' will result in
creating 3 projects: 'services', 'services:hotels' and
'services:hotels:api'.
In simple words, this means that the inclusion of services::webservice will also build the services project
The bulid.gradle file in your root that applies the 'java' plugin. According to Gradle Documentation here every configuration defined in the root.gradle takes effect for all sub projects. This means that it will also hold as the default configuration for the services project. As the 'java' plugin was applied a jar will be created, but as there is no src/main folder under the services directory nothing will be compiled and the jar will include only a META-INF/MANIFEST.MF file.

How to use Sonar Runner on selected files?

I want to run Sonar Runner only on some selected files only. I'm using SonarRunner Ant.
My project directory structure is :
MyProject
|
|-----src
|-----java
|-----A
|-----B
| |---<files>.java
|
|-----C
| |---<files>.java
|
|-----hello.java
Now I want to run Sonar Runner only on hello.java file.
sonar.sources=../../../MyProject/src // takes the source directory
sonar.sources=../../../MyProject/src/java/A/hello.java didn't work
sonar.exclusions=**/**/*.java // excludes all java files
// now I want to include only hello.java file
// didn't find any parameter for inclusion, but tried the following
sonar.inclusions=hello.java // didn't work
sonar.inclusions=java/A/hello.java // didn't work
Referred this article for analysis parameters.
One solution which crossed my mind is : exclusion of all the files but the required ones.
But here the structure is just a small part. In real I have more than 250 java files, and want to generate report for, say, 10 files only. Then, by this approach, excluding 240+ files doesn't look a good idea.
Is there anyway to generate sonar report on selected files, other than the mentioned approach?
If you're looking for specific files, you might try the same syntax as is listed to explicitly exclude files (Narrowing the Focus - at the bottom)
#Absolute Path
To define an absolute path, start the pattern with "file:"
#Exclude all the *.cs files included in /path_to_my_project/myProject/src/generated and its subdirectories
sonar.exclusions=file:/path_to_my_project/myProject/src/generated/**/*.cs
#Exclude all the java classes contained in a src/generated/java directory and its subdirectories
sonar.exclusions=file:**/src/generated/java/**/*.java

SonarQube Exclude a directory

I am trying to exclude a directory from being analyzed by Sonar. I have the following properties defined in my sonar-project.properties file:
sonar.sources=src/java
sonar.exclusions=src/java/test/****/*.java
The directory structure I have is:
src/java/dig
src/java/test/dig
When I run the sonar-runner I get the following info:
INFO - Excluded sources:
INFO - src/java/test/**/*.java
INFO - Excluded tests:
INFO - **/package-info.java
But when I check the result of the analysis all the packages inside the test directory are still there.
I just need to tell Sonar to not analyze the test directory and any packages inside it.
Try something like this:
sonar.exclusions=src/java/test/**
I'm able to exclude multiple directories using the below config (comma separated folder paths):
sonar.exclusions=system/**, test/**, application/third_party/**, application/logs/**
And while running the sonar runner I got the following in the log:
Excluded sources:
system/**
test/**
application/third_party/**
application/logs/**
This will work for your case:
sonar.exclusions=**/src/java/dig/ ** , **/src/java/test/dig/ **
Another configuration option is adding a maven properties sonar.exclusions. Below is a sample pom file with exclusions of static jquery directory and static pdf viewer directory.
<project >
<modelVersion>4.0.0</modelVersion>
<artifactId>my Artifact</artifactId>
<!-- Enviroment variables can be referenced as such: ${env.PATH} -->
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
<junit.version>4.9</junit.version>
<mockito.version>1.9.5</mockito.version>
<jackson.version>1.9.7</jackson.version>
<powermock.version>1.5</powermock.version>
<!--Exclude the files Here-->
<sonar.exclusions>src/main/webapp/static/jquery_ui/*,src/main/webapp/static/pdf-viewer/*,src/main/webapp/static/pdf-viewer/**,src/main/webapp/static/pdf-viewer/**/*</sonar.exclusions>
</properties>
If we want to skip the entire folder following can be used:
sonar.exclusions=folderName/**/*
And if we have only one particular file just give the complete path.
All the folder which needs to be exclude and be appended here.
Easiest way is to go to the server URL after starting the server(localhost:8080) then login as admin,Go to settings>Exclusions> Source File Exclusions- Add your packages here.
Restart the server.
If you're an Azure DevOps user looking for both where and how to exclude files and folders, here ya go:
Edit your pipeline
Make sure you have the "Prepare analysis on SonarQube" task added. You'll need to look elsewhere if you need help configuring this. Suggestion: Use the UI pipeline editor vs the yaml editor if you are missing the manage link. At present, there is no way to convert to UI from yaml. Just recreate the pipeline. If using git, you can delete the yaml from the root of your repo.
Under the 'Advanced' section of the "Prepare analysis on SonarQube" task, you can add exclusions. See advice given by others for specific exclusion formats.
Example:
# Additional properties that will be passed to the scanner,
# Put one key=value per line, example:
# sonar.exclusions=**/*.bin
sonar.exclusions=MyProjectName/MyWebContentFolder/**
Note: If you're not sure on the path, you can go into sonarqube, view your project, look at all or new 'Code Smells' and the path you need is listed above each grouping of issues. You can grab the full path to a file or use wilds like these examples:
MyProjectName/MyCodeFile.cs
MyProjectName/**
If you don't have the 'Run Code Analysis' task added, do that and place it somewhere after the 'Build solution **/*.sln' task.
Save and Queue and then check out your sonarqube server to see if the exclusions worked.
what version of sonar are you using?
There is one option called "sonar.skippedModules=yourmodulename".
This will skip the whole module. So be aware of it.
You can do the same with build.gradle
sonarqube {
properties {
property "sonar.exclusions", "**/src/java/test/**/*.java"
}
}
And if you want to exclude more files/directories then:
sonarqube {
properties {
property "sonar.exclusions", "**/src/java/test/**/*.java, **/src/java/main/**/*.java"
}
}
Add comma separated folder paths sonar.exclusions=**/abc/**,**/def/**
This worked in an angular project
I typed case sensitive and used "" and it worked. Analyze time decreased to 3 minutes from 10.
# Additional properties that will be passed to the scanner,
# Put one key=value per line, example:
sonar.exclusions=**\Scripts\**\*,**\Content\**\*
Just to mention that once you excluded the files from Sonar, do the same for Jacoco plugin:
<configuration>
<excludes>
<exclude>com/acme/model/persistence/entity/TransactionEntity*</exclude>
<exclude>com/acme/model/persistence/ModelConstants.class</exclude>
</excludes>
</configuration>
add this line to your sonar-project.properties file
ex: sonar.exclusions=src/*.java
be careful if you want to exclude a folder and inside the folder there is a file you must first exclude the files or add the files one by one
for example imagine there is a folder like below:
src/app.java
src/controllers/home.java
src/services/test.java
you have to do this:
sonar.exclusions=src/app.java,src/controllers/*.java,src/services/*.java
It worked for me
You can skip library like this
project(":libABC") {
apply plugin: 'org.sonarqube'
sonarqube {
skipProject = true
}
}
This worked for me:
sonar.exclusions=src/**/wwwroot/**/*.js,src/**/wwwroot/**/*.css
It excludes any .js and .css files under any of the sub directories of a folder "wwwroot" appearing as one of the sub directories of the "src" folder (project root).

Resources