Sonar javascript static code analyze - javascript riport missing. Maven project - javascript

Help me, please. I don't know why missing only javascript static code analyze riports.
So, I have a maven project with multiple modules and the javascript codes in the frontend project.
SonarJS plugin installed
i'm running: mvn clean install sonar:sonar
i have configure sonar from pom.xml instead of sonar-project.properties
sonar version: 6.7 community edition
In the sonar overview it has java static code analyze riport both of modules, but nothin about javascript codes.
Here is my pom.xml settings in the parent project.
<?xml version="1.0" encoding="UTF-8"?>
<project ... >
...
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>backend</module>
<module>frontend</module>
</modules>
<properties>
...
<!-- sonar -->
<sonar.version>3.4.0.905</sonar.version>
<sonar.host.url>http://my.sonar.domain.url</sonar.host.url>
<sonar.login>asdsd43f8g7fs498u9s8df7s97zf9er97zf7</sonar.login>
<sonar.javascript.file.suffixes>.js,.jsx,.vue</sonar.javascript.file.suffixes>
<sonar.sources>src/main/</sonar.sources>
<sonar.test>src/test/</sonar.test>
<sonar.jacoco.itReportPath>${project.testresult.directory}/coverage/jacoco/jacoco-it.exec</sonar.jacoco.itReportPath>
<sonar.jacoco.reportPath>${project.testresult.directory}/coverage/jacoco/jacoco.exec</sonar.jacoco.reportPath>
<sonar.java.codeCoveragePlugin>jacoco</sonar.java.codeCoveragePlugin>
<!-- For Java -->
<sonar.junit.reportsPath>reports/java/surefire-reports</sonar.junit.reportsPath>
<!-- For JavaScript -->
<sonar.javascript.lcov.reportPath>reports/js/lcov.dat</sonar.javascript.lcov.reportPath>
...
</properties>
<dependencies> ... </dependencies>
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.version}</version>
<executions>
<execution>
<id>sonar-run</id>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
....
</plugins>
</pluginManagement>
</build>
</project>
UPDATE:
I have run sonar analyze with -X debug. The logs below:
[DEBUG] 15:08:27.144 'src/main/resources/static/js/template/template.js' excluded by org.sonar.plugins.javascript.JavaScriptExclusionsFileFilter
[DEBUG] 15:08:27.145 'src/main/resources/static/js/navigation/navigationEvents.js' excluded by org.sonar.plugins.javascript.JavaScriptExclusionsFileFilter
[DEBUG] 15:08:27.145 'src/main/resources/static/js/navigation/navigation.js' excluded by org.sonar.plugins.javascript.JavaScriptExclusionsFileFilter
[DEBUG] 15:08:27.145 'src/main/resources/static/js/navigation/moduleLoader.js' excluded by org.sonar.plugins.javascript.JavaScriptExclusionsFileFilter
But doesn't have any exculde settings.

You wrote in the comment that:
sonar.javascript.exclusions=**/node_modules/**,**/bower_components/**,**/js/**
If you didn't specify it in any pom.xml file, then that value is taken from the server. You can see the configuration by opening: https://sonar.host/admin/settings?category=javascript or Administration → Configuration → JavaSctript. Next you have to find the JavaScript Exclusions section:
Probably you will also see **/js/**. From my point of view you should delete that entry because it has no sense. If you cannot then you have to overwrite the default value by adding:
<sonar.javascript.exclusions>**/node_modules/**,**/bower_components/**</sonar.javascript.exclusions>
or
<sonar.javascript.exclusions></sonar.javascript.exclusions>

In pom of maven module containing JS code, set sonar.sources (and tests if you want) of your JS code

I'd say that first you want to try to execute coverage with sonar.properties file just to figure out - which keys you are still missing, preferably for js exclusively. Also, correct setting may depend on actual Sonar version that you are using.
One thing I see is that you have sonar.lcov.javascript.reportPath twice. Which one is the correct one (to start with use the hardcoded path, to make sure, you are getting the reports)
Secondly,in my project file I also had to specify the plugin to use (something like sonar.js.coveragePlugin = lcov ). Pehaps that you also want to add, since you are using lcov.
Finally, I also have the following flag set:
sonar.dynamicAnalysis=reuseReports
That was needed to reuse the reports that were generated during the build.
On a side note, my configuration was created a 3 years back, for sonar version that was latest back then. There might have been some changes with the new releases of Sonar.

Related

Disable javascript facet in a m2e project

I'd like to disable the Javascript Facet (and remove the Javascript builder) from a Maven-driven Eclipse project.
Is there any way I can configure the maven-eclipse-plugin to do so?
First of all, clean the configuration with mvn eclipse:clean, it will remove all configuration from the .project file.
Then make a new one with mvn eclipse:eclipse, just with the configuration and dependencies on your pom.xml.
EDIT:
And configure the project natures that you need:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<projectnatures>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
<projectnature>org.eclipse.wst.common.modulecore.ModuleCoreNature</projectnature>
</projectnatures>
<additionalProjectnatures>
[... the ones you need or empty...]
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
</configuration>
</plugin>

Serving bundled JavaScript with a pure AppHost implementation of ServiceStack

I would like to use ServiceStack as a pure AppHost implementation without using MVC. I want to use it to drive a Single Page App.
Serving the SPA's single HTML shell page is straightforward enough and I've found plenty of examples on doing that. However, I also need to serve a number of JavaScript files and I'm assessing the best way of doing this. I can simply put script tags in the HTML shell page but then I don't get the benefits of bundling and minification, and I would have to maintain this every time I add a new JavaScript file.
All these problems are solved with bundling solutions such as Cassette or ASP.NET MVC4 Bundles. But how would I use these with ServiceStack AppHost?
The ServiceStack.Bundler project is great but it seems to have dependencies on ASP.NET MVC, e.g. as a base for the HTML Helpers which render the JavaScript tags in the HTML.
I'd like to be able to do this without any dependency on MVC, if possible.
If you haven't taken a look at GruntJS yet, it's worth a look (http://gruntjs.com/). By creating some simple tasks, it can combine & minify your HTML, JS, and CSS and has no dependency on .NET. There are a lot of other really useful tasks available to GruntJS as well (js lint checks, JS unit test running, and tons more). You can easily setup different tasks for your environments as well (ie, don't combine/minify when deploying to dev server).
What it allows you to do is create a purely static HTML, CSS, and JS SPA, and you can manage that in a completely different solution/project than your ServiceStack AppHost.
So in your example, you'd just reference the scripts in your index.html file like you normally would and when you're ready to deploy to staging/production you'd run your grunt task which would bundle/minify your code for you and output the static html, min.css, and min.js files for you to some deployment directory. It's really powerful and flexible.
I used to use Bundler and I recently made the switch to GruntJS and I haven't looked back.
So, I don' think there is anything to need to do within ServiceStack's AppHost to use a 'bundling-and-minification' solution. To simplify the 'Bundling' process...
1 - 'Bundle' files from a folder(s) creating a new file(s)
2 - Reference the 'Bundled' file(s) from a View/Html
How to 'Bundle' files from a folder(s) creating a new file(s)
Cassette
Cassette seems to handle this process with 'some magic' (see web.config modifications for insight) that calls the Configure method of the CassetteBundleConfiguration class. Installing the Nuget package takes care of 'setup' for you.
ServiceStack.Bundler
ServiceStack.Bundler has a few different ways to handle this process...1) Manually with bundler.cmd, 2) A VS extension 3) Post Build Script 4) Short-cut key to an External Tool
Reference the 'Bundled' file(s)
You can do this however you like as long as you know the path of the file(s)
<link href="/Content/some.css" rel="stylesheet" type="text/css" />
<script src='some.js'></script>
Cassette offers some convenient rendering features
#Bundles.Reference('yourbundle')
#Bundles.RenderStylesheets()
#Bundles.RenderScripts()
ServiceStack.Bundler offers some as well (I think this is the code the depends on System.Web.MVC)
#Html.RenderJsBundle()
#Html.RenderCssBundle()
Note: These are just writing out the <link> and <script> HTML tags.
It seems neither Cassette nor Microsoft.AspNet.Web.Optimization (the bundling solution included with MVC4 projects by default) have dependencies on ASP.NET MVC. Therefore either solution can be made to work with an AppHost implementation of ServiceStack.
For Cassette:
It all works just fine if, from NuGet, you install:
ServiceStack.Host.AspNet
ServiceStack.Razor
Cassette.Aspnet
... and then use Cassette from a Razor 'cshtml' file as usual.
One small gotcha which did cause me to scratch my head for a few minutes:
The order in which the HttpHandlers are listed in your web.config is important. The ServiceStack.Host.AspNet package adds an HttpHandler path which uses a wildcard meaning any further HttpHandlers, such as that for Cassette.axd, are never reached.
Simply changing the order in my web.config from:
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
<add path="cassette.axd" verb="*" type="Cassette.Aspnet.CassetteHttpHandler, Cassette.Aspnet" />
</httpHandlers>
to:
<httpHandlers>
<add path="cassette.axd" verb="*" type="Cassette.Aspnet.CassetteHttpHandler, Cassette.Aspnet" />
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
fixed the problem. I don't know if installing Cassette.Aspnet from Nuget first would have prevented this issue from occurring in the first place.
For Microsoft.AspNet.Web.Optimization:
From NuGet, you install:
ServiceStack.Host.AspNet
ServiceStack.Razor
Microsoft.AspNet.Web.Optimization
Having done this, you can use Microsoft.AspNet.Web.Optimization bundling and minification as normal.
I added a BundleConfig.cs file, followng the convention you'd find in a default MVC4 project. I then call BundleConfig.RegisterBundles(BundleTable.Bundles); from the ServiceStack AppHost file.
Thereafter, all #Scripts.Render() statements in Razor files work just fine.

Jetty Maven Plugin: using scan

I have a JavaScript project in Netbeans with the following file structure:
src/main/javascript
src/main/resources (containing my index.html)
src/main/webapp
Doing the mvn build, the target folder contains my "project" folder with my index.html and the javascript files.
When I edit a JavaScript file or my index.html in the src folder, I want jetty to recognize this (I thought this is the idea of the jetty scanner). But this is not the case.
Here my jetty plugin configuration in the POM:
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.4.2.v20110526</version>
<configuration>
<scanTargets>
<scanTarget>${basedir}/src/main/resources/</scanTarget>
<scanTarget>${basedir}/src/main/javascript/</scanTarget>
</scanTargets>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<classesDirectory>${project.build.directory}/project/</classesDirectory>
<webAppConfig>
<contextPath>/</contextPath>
<resourceBases>
<resourceBase>${project.build.directory}/project/</resourceBase>
<resourceBase>${project.build.directory}/project/</resourceBase>
</resourceBases>
</webAppConfig>
</configuration>
</plugin>
Can someone tell me why changes are not "scanned"?

jasmine-maven-plugin load source files and their dependencies

I've got a bunch of javascript files I'd like to test, located in src/main/webapp/js, but when I try to build on the command line, I get a bunch of errors due to the missing library files that each of my source scripts depend upon. I am trying to include them within the "sourceIncludes" tag, but this seems to do nothing. I can, however, place them all, one at a time, in the "preloadSources" tags, but I'd rather not do this.
Also, some of my sources files have functions that interact with the DOM, and including the .html file that contains the elements that these functions need just gives me:
"net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "XML" is not defined"
my pom.xml below:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>src/main/webapp/js</jsSrcDir>
<jsTestSrcDir>src/test/javascript/js</jsTestSrcDir>
<sourceIncludes>
<include>src/main/webapp/js/lib/**/*.js</include>
<include>**/*.js</include>
</sourceIncludes>
<preloadSources>
<preloadSource>src/main/webapp/js/lib/jquery/jquery-1.7.2.js</preloadSource>
</preloadSources>
</configuration>
</plugin>
I hate having to list my dependencies in order. That's why I use a dependency loader like Require.js. I wrote a quick post summarizing my experience with jasmine-maven-plugin, but using require.js to load dependencies. It keeps my pom.xml clean, and I let my JavaScript files load their own dependencies. jasmine+maven+requirejs+code coverage

jasmine-maven-plugin and require-js result in path issues

I am using require-js to model dependencies in my java script project. I also use jasmine to write BDD styled test cases and the jasmine-maven-plugin:1.1.0 to execute these tests in headless mode.
This is my source project structure
project/
| src/main/javascript
| | api/*.js
| | main.js
| src/test/javascript
| | spec/*spec.js
This is my webapp pom jasmine configuration:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>generateManualRunner</goal>
<goal>resources</goal>
<goal>testResources</goal>
<goal>test</goal>
<goal>preparePackage</goal>
</goals>
</execution>
</executions>
<configuration>
<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
<scriptLoaderPath>lib/require-jquery.js</scriptLoaderPath>
<sourceIncludes>
<include>lib/require-jquery.js</include>
<include>lib/underscore.js</include>
<include>lib/backbone.js</include>
<include>lib/d3.js</include>
<include>lib/d3.layout.js</include>
<include>lib/bootstrap.js</include>
<include>main.js</include>
</sourceIncludes>
<preloadSources>
<source>lib/require-jquery.js</source>
<source>lib/underscore.js</source>
<source>lib/backbone.js</source>
<source>lib/d3.js</source>
<source>lib/d3.layout.js</source>
<source>lib/bootstrap.js</source>
<source>main.js</source>
</preloadSources>
<browserVersion>FIREFOX_3_6</browserVersion>
</configuration>
</plugin>
During the maven build all js sources are copied to target/jasmine/src. The runner.html that refers to my main.js is at target/jasmine.
Now the problem is that my require-js modules define their dependencies relative to the main.js, whereas in the maven test run, the runner assumes them to be relative to target/jasmine/runner.html. Since my modules are not (and must not be) aware of the additional jasmine/src folder, IMHO that is an issue.
I receive the following error message when I run mvn clean install:
Failed to execute goal
com.github.searls:jasmine-maven-plugin:1.1.0:test (default) on project
mywebapp: The jasmine-maven-plugin encountered an
exception: java.lang.RuntimeException:
org.openqa.selenium.WebDriverException:
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
java.lang.RuntimeException: java.io.FileNotFoundException:
C:\Jenkins\jobs\job1\workspace\mywebapp\target\jasmine\api\data-util.js
(The system cannot find the path specified)
Anybody out there having some idea to work around or configure that?
I faced the same problem:
java.io.FileNotFoundException
with requireJS and jasmine
but you don't have to use the maven-resources-plugin and hack the js files to a second output directory.
I used 'srcDirectoryName'
you can find all possible params at https://github.com/searls/jasmine-maven-plugin/blob/master/src/main/java/com/github/searls/jasmine/mojo/AbstractJasmineMojo.java
and advanced examples under https://github.com/searls/jasmine-maven-plugin/tree/master/src/test/resources/examples
In the meantime I found a workaround or a fix (not sure) by configuring the maven-resources-plugin
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-js-files</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jasmine</outputDirectory>
<resources>
<resource>
<directory>src/main/javascript</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/test/webapp</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
I put that configuration in a separate profile which I activate during testing.
I see that you've got lib/require-jquery.js, but your file tree doesn't show where "lib" is located. is loaded relative to , but you don't have one. Maybe that's your problem. I wrote a quick post summarizing my experience with the jasmine-maven-plugin and using Require.js. In it I've got my pom.xml. My pom.xml seems quite a bit smaller than yours but the plugin works fine for me. Maybe my post will help. Jasmine+Maven+Requirejs+Coverage

Categories