I am working on "Cucucmber-Jvm (Using Maven Project) + Selenium WebDriver PageObjects + Allure Report", I am unable generate "Allure" report.
Below are the codes, feature file, pom.xml, etc...
Pom.xml : Reference POM url - https://github.com/allure-framework/allure-cucumber-jvm-adaptor
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<name>Allure Cucumber-JVM Adaptor</name>
<artifactId>allure-cucumber-jvm-adaptor</artifactId>
<groupId>ru.yandex.qatools.allure</groupId>
<version>1.4-SNAPSHOT</version>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<url>https://github.com/allure-framework/allure-cucumber-jvm-adaptor</url>
<connection>scm:git#github.com:allure-framework/allure-cucumber-jvm-adaptor.git</connection>
<developerConnection>scm:git:git#github.com:allure-framework/allure-cucumber-jvm-adaptor.git</developerConnection>
</scm>
<issueManagement>
<system>GitHub Issues</system>
<url>https://github.com/allure-framework/allure-cucumber-jvm-adaptor/issues</url>
</issueManagement>
<ciManagement>
<system>TeamCity</system>
<url>http://teamcity.qatools.ru/</url>
</ciManagement>
<developers>
<developer>
<id>clicman</id>
<name>Viktor Sidochenko</name>
<email>viktor.sidochenko#gmail.com</email>
</developer>
</developers>
<mailingLists>
<mailingList>
<name>Allure Mailing List</name>
<post>allure#yandex-team.ru</post>
</mailingList>
</mailingLists>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<compiler.version>1.7</compiler.version>
<allure.version>1.4.5</allure.version>
<aspectj.version>1.8.4</aspectj.version>
</properties>
<dependencies>
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-core</artifactId>
<version>${allure.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-java-aspects</artifactId>
<version>${allure.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.allure</groupId>
<artifactId>allure-commons</artifactId>
<version>${allure.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.0</version>
<type>jar</type>
</dependency>
<!-- <dependency>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>9</version>
</dependency> -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.5</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<!-- parallel execution configuration -->
<parallel>classes</parallel>
<threadCount>8</threadCount>
<includes>
<include>**/*Test.java</include>
</includes>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
<properties>
<property>
<name>listener</name>
<value>ru.yandex.qatools.allure.cucumberjvm.AllureRunListener</value>
</property>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Feature file location: src/test/resources and Name of the feature file: OpenAndclose.feature
Feature: OpenAndClose
Scenario: OpenAndClose Browser
Given user opened firefox browser
Then user entered url
Then user closed firefox browser
And codes at: src/test/java
TestRunner class:
package openANDclose;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(dryRun=false, monochrome = true, features= "src/test/resources/OpenAndclose.feature")
public class OpenAndClose_TestRunner {
}
Step Definitions:
package openANDclose;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
public class OpenAndClose_StepDefinitions {
public static WebDriver driver;
#Given("^user opened firefox browser$")
public void user_opened_firefox_browser() throws Throwable {
driver=new FirefoxDriver();
}
#Then("^user entered url$")
public void user_entered_url() throws Throwable {
OpenAndClose_PageObjects.user_opened_firefox_browser(driver);
}
#Then("^user closed firefox browser$")
public void user_closed_firefox_browser() throws Throwable {
OpenAndClose_PageObjects.user_closed_firefox_browser(driver);
}
}
Page Object Class:
package openANDclose;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class OpenAndClose_PageObjects {
public static WebDriver driver ;
public static WebElement element=null;
public static WebElement user_opened_firefox_browser(WebDriver driver) {
driver.get("http://google.co.in");
return element;
}
public static WebElement user_closed_firefox_browser(WebDriver driver) {
driver.quit();
return element;
}
}
After running "Allure" report from command prompt, getting below error message and error report.
Below report from: "mvn clean test" below error message is displaying.
D:\Cucumber_JVM_WorkSpace\Cucumber_Allure>mvn clean test
[INFO] Scanning for projects...
[INFO] Inspecting build with total of 1 modules...
[INFO] Installing Nexus Staging features:
[INFO] ... total of 1 executions of maven-deploy-plugin replaced with nexus-st
aging-maven-plugin
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Allure Cucumber-JVM Adaptor 1.4-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # allure-cucumber-jvm-ad
aptor ---
[INFO] Deleting D:\Cucumber_JVM_WorkSpace\Cucumber_Allure\target
[INFO]
[INFO] --- maven-enforcer-plugin:1.0:enforce (enforce-maven) # allure-cucumber-j
vm-adaptor ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # allure-cuc
umber-jvm-adaptor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Cucumber_JVM_WorkSpace\Cucumber_Al
lure\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) # allure-cucumber
-jvm-adaptor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # al
lure-cucumber-jvm-adaptor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) # allure-
cucumber-jvm-adaptor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\Cucumber_JVM_WorkSpace\Cucumber_Allure\tar
get\test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_StepDefinitions.java:[3,27] package org.openqa.selenium does not exis
t
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_StepDefinitions.java:[4,35] package org.openqa.selenium.firefox does
not exist
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_StepDefinitions.java:[11,23] cannot find symbol
symbol: class WebDriver
location: class openANDclose.OpenAndClose_StepDefinitions
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[3,27] package org.openqa.selenium does not exist
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[4,27] package org.openqa.selenium does not exist
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[7,23] cannot find symbol
symbol: class WebDriver
location: class openANDclose.OpenAndClose_PageObjects
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[9,23] cannot find symbol
symbol: class WebElement
location: class openANDclose.OpenAndClose_PageObjects
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[11,62] cannot find symbol
symbol: class WebDriver
location: class openANDclose.OpenAndClose_PageObjects
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[11,23] cannot find symbol
symbol: class WebElement
location: class openANDclose.OpenAndClose_PageObjects
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[16,62] cannot find symbol
symbol: class WebDriver
location: class openANDclose.OpenAndClose_PageObjects
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_PageObjects.java:[16,23] cannot find symbol
symbol: class WebElement
location: class openANDclose.OpenAndClose_PageObjects
[ERROR] /D:/Cucumber_JVM_WorkSpace/Cucumber_Allure/src/test/java/openANDclose/Op
enAndClose_StepDefinitions.java:[15,36] cannot find symbol
symbol: class FirefoxDriver
location: class openANDclose.OpenAndClose_StepDefinitions
[INFO] 12 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.287 s
[INFO] Finished at: 2015-05-02T15:01:37+05:30
[INFO] Final Memory: 13M/33M
[INFO] ------------------------------------------------------------------------
Below report from: mvn site
About Allure Cucumber-JVM Adaptor
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Project not comliled successfully and not started. You should add webdriver dependencies to your project`s pom.xml.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.45.0</version>
</dependency>
Also you should not compile the adaptor itself. Use it as dependency to your project. See an example project: allure-cucumber-jvm-example.
Use example project as base and add selenium dependency to it and your code.
Related
I am working in Selenium. It is demo project. I am using maven for dependency management. I have downloaded the Gecko driver and has kept to folder location in local drive. But it will create a dependency to with local machine.
We have have folder in maven, called resource. I want to use that. I want to keep the Firefox driver there and want to load it from here.
Maven Dependency
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>maven-selemium</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Add Following Lines in Your POM File -->
<properties>
<selenium.version>2.53.1</selenium.version>
<testng.version>6.9.10</testng.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Java Code
public class PG1 {
public static void main(String[] args) throws Exception {
String baseUrl = "https://www.google.co/";
// System.setProperty("webdriver.gecko.driver", "C:/Users/Dell/Downloads/geckodriver-v0.29.1-win64/geckodriver.exe");
System.setProperty("webdriver.gecko.driver", "E:/Drive_H/projects/selenium/src/main/resources/geckodriver-firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.close();
}
}
I have tried with this line but error : unable to load driver.
System.setProperty("webdriver.gecko.driver", "geckodriver-firefox.exe");
Use System.getProperty("user.dir") to get the project user directory path and then access the desired executable.
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "\\src\\main\\resources\\geckodriver-firefox.exe");
I am trying to create groovy tests where i use other methods from other Groovy scripts. Whenever i run mvn test i get unable to resolve class error, referring to the illegal import from another groovy script.
This is the testclass that I've written :
import jenkins_methods.gitMethods
import org.junit.Test
class GitMethodsTest extends GroovyTestCase{
GitMethodsTest(){
}
def gitMethods = new gitMethods()
#Test
void testGetDvhBranches(){
try{
gitMethods.getDVH_branches()
}
catch(Exception e){
println e
}
}
}
gitMethods.groovy
package jenkins_methods
// src/jobMethods
import dvh.util.AnsiColorDefinition
import jenkins_methods.fileMethods
String getGitDVH_BranchPath_flash() { return '/git-repository1/git/dvh/branches/' }
String dvhBranchNull() { return 'feature/DV-null-repo' }
String getDVH_branches() {
gitURL_DVH= "censored_url"
String response = sh returnStdout: true, script: "git ls-remote -h ${gitURL_DVH()}"
def branches = response.replaceAll(/[a-z0-9]*\trefs\/heads\//, ',')
branches = branches.substring(1, branches.length()).replaceAll("\n", "").replaceAll("\r", "")
return branches
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>jenkins-jobs</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<configuration>
<providerSelection>2.0</providerSelection>
</configuration>
<goals>
<goal>addSource</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The jenkins_methods package contains various groovy scripts that are necessary for the Tests to become relevant.
Structure of the project is as following :
-src
-test
-groovy
-GitMethodsTest.groovy
-jenkins_methods
-gitMethods.groovy
This is the error that i get when i run mvn test
[ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.5:testCompile (default) on project jenkins-jobs: startup failed:
[ERROR] /C:/Users/jenkins-jobs/src/test/groovy/GitMethodsTest.groovy: 3: unable to resolve class jenkins_methods.gitMethods
[ERROR] # line 3, column 1.
[ERROR] import jenkins_methods.gitMethods
[ERROR] ^
[ERROR]
[ERROR] 1 error
How do i solve this problem?
I am not able to resolve RemoteWebDriver in eclipse due issues in maven dependency. I need RemoteWebDriver to get the browser version (for reporting purpose). I have mentioned the following maven dependencies yet I am not able to resolve RemoteWebDriver. As per the earlier post The import org.openqa.selenium.remote.CapabilityType cannot be resolved I have to manually download selenium-standalone-server. I am not understanding as to why maven dependency is not sufficient? Is there any other maven dependency that can be added to resolve RemoteWebDriver
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.141.59</version>
</dependency>
*************************
//Below is what I am trying to code
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
It is quite enough to have only selenium-java, it will resolve selenium-remote-driver via Maven transitive dependency mechanism
Given you mention that you have to manually download Selenium Standalone Server you don't need this selenium-server dependency as well
So it should be as simple as:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>selenium-java</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>
Test class:
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class SeleniumTest {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "c:/apps/webdriver/chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Capabilities cap = driver.getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
System.out.println(browserName);
driver.quit();
}
}
Demo:
More information:
Remote WebDriver
Selenium with Java
ChromeDriver - Getting Started
I am learning Mongodb for Java and I am trying to run the code below which shows jetty handler error. I tried searching for solutions in different threads of this forum but none of the solutions helps me. Please help. What am I missing?
package com.mongo.practice;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;
public class SparkMain {
public static void main(String[] args){
Spark.get("/", new Route() {
public Object handle(final Request request, final Response response){
return "Hello worldfrom Spark";
}
});
}
}
I get this error when run
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/Handler
at spark.embeddedserver.EmbeddedServers.initialize(EmbeddedServers.java:40)
at spark.Service.lambda$init$2(Service.java:536)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Handler
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
This is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mongo</groupId>
<artifactId>M101J</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.7.2</version>
</dependency>
</dependencies>
</project>
I tried adding this
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.9.v20180320</version>
</dependency>
but it did not solve the issue
You have to add two dependencies. The following:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-util -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.3.24.v20180605</version>
</dependency>
The version for sl4j can varry. But the version of jetty is not flexible. I have tried with the latest version. It does not work for any other version other than this.
Does anybody know how to configure the maven findbugs plugin to output a summary of the bugs to the console (similar to the pmd plugin)?
At present findbugs:check just prints out how many bugs there are in total and I need to check the individual modules target/findbugs directory and each findbugs.xml file to fix the issues.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<xmlOutput>true</xmlOutput>
<xmlOutputDirectory>findbugsreports</xmlOutputDirectory>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlOutputDirectory>target/site/findbugsreports</findbugsXmlOutputDirectory>
<debug>true</debug>
</configuration>
</plugin>
Ideally it would be good to get a summary report back on the command line. Any ideas?
I use this hack, based on maven-groovy-plugin:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.0-rc-5-SNAPSHOT</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def file = new File("${project.build.directory}/findbugsXml.xml")
if (!file.exists()) {
fail("Findbugs XML report is absent: " + file.getPath())
}
def xml = new XmlParser().parse(file)
def bugs = xml.BugInstance
def total = bugs.size()
if (total > 0) {
log.info("Total bugs: " + total)
for (i in 0..total-1) {
def bug = bugs[i]
log.info(
bug.LongMessage.text()
+ " " + bug.Class.'#classname'
+ " " + bug.Class.SourceLine.Message.text()
)
}
}
</source>
</configuration>
</execution>
</executions>
</plugin>
There isn't currently a means to do this using the standard plugin. You can create a plugin to read the findbugsChecks.xml and output the information you need though.
The code below will output the total bugs found and the bugs per package for any project with a findbugsChecks.xml in the output directory. You can configure the name of the file it reads by setting the findBugsChecks property on the configuration:
package name.seller.rich;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
/**
* #goal stats
*/
public class FindbugsStatsMojo extends AbstractMojo {
/**
* Where to read the findbugs stats from
*
* #parameter expression="${findbugsChecks}"
* default-value="${project.build.directory}/findbugsCheck.xml"
*/
private File findbugsChecks;
/**
* Output the Findbus stats for the project to the console.
*/
public void execute() throws MojoExecutionException, MojoFailureException {
if (findbugsChecks != null && findbugsChecks.exists()) {
try {
Xpp3Dom dom = Xpp3DomBuilder.build(new FileReader(
findbugsChecks));
// get the summary and output it
Xpp3Dom summaryDom = dom.getChild("FindBugsSummary");
// output any information needed
getLog().info(
"Total bug count:"
+ summaryDom.getAttribute("total_bugs"));
Xpp3Dom[] packageDoms = summaryDom.getChildren("PackageStats");
getLog().info(packageDoms.length + " package(s)");
for (int i = 0; i < packageDoms.length; i++) {
String info = new StringBuilder().append("package ")
.append(packageDoms[i].getAttribute("package"))
.append(": types:").append(
packageDoms[i].getAttribute("total_types"))
.append(", bugs:").append(
packageDoms[i].getAttribute("total_bugs"))
.toString();
getLog().info(info);
}
} catch (FileNotFoundException e) {
throw new MojoExecutionException(
"Findbugs checks file missing", e);
} catch (XmlPullParserException e) {
throw new MojoExecutionException(
"Unable to parse Findbugs checks file", e);
} catch (IOException e) {
throw new MojoExecutionException(
"Unable to read Findbugs checks file", e);
}
}
}
}
To package this code, add it to the src/main/java folder of a Mavenproject with a POM like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>name.seller.rich</groupId>
<artifactId>maven-findbugs-stats-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.0.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
</project>
Then run mvn install to install the plugin.
To actually use it, you can run it as an additional goal on the command line, or bind it to your project to run as part of the standard lifecycle.
Here's the command to run from the commandline (assuming the project has previously been compiled:
mvn findbugs:check name.seller.rich:maven-findbugs-stats-plugin:0.0.1:stats
To bind the configurations to your project so it will be run on each build, use the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>check</id>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<xmlOutput>true</xmlOutput>
<xmlOutputDirectory>findbugsreports</xmlOutputDirectory>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlOutputDirectory>${findbugsOutputDirectory}</findbugsXmlOutputDirectory>
<debug>true</debug>
<failOnError>false</failOnError>
</configuration>
</plugin>
<plugin>
<groupId>name.seller.rich</groupId>
<artifactId>maven-findbugs-stats-plugin</artifactId>
<executions>
<execution>
<id>stats</id>
<phase>package</phase>
<goals>
<goal>stats</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Following along from the concepts above I have raised this issue on the maven findbugs issue tracker. http://jira.codehaus.org/browse/MFINDBUGS-118. I have also coded and submitted a patch that shows total bugs for each project. It could easily be modified to get other details.
The code ignores projects specified as producing POM outputs and also ignores projects whose POMs specify true in their findbugs configuration. We are running a large multi-module maven build with the patch applied.
With the patch applied you run mvn findbugs:check and you get something like the following output (output obfuscated to protect the guilty :):
[INFO] Summary
[INFO] -------
[INFO] C:\PATH\Abstraction\PalDefinitions\target/findbugsXml.xml 4
[INFO] C:\PATH\System\target/findbugsXml.xml 19
[INFO] C:\PATH\ApplicationLayer\target/findbugsXml.xml 13
[INFO] C:\PATH\Support\ServiceStub\target/findbugsXml.xml 11
[INFO] C:\PATH\Support\MultiPlatform\target/findbugsXml.xml 10
[INFO] C:\PATH\Support\Serializer\target/findbugsXml.xml 19
[INFO] C:\PATH\Support\Brander\target/findbugsXml.xml 19
[INFO] C:\PATH\PlatformAbstraction\Pal1\target/findbugsXml.xml 8
[INFO] C:\PATH\PlatformAbstraction\Pal2\target/findbugsXml.xml 0
[INFO] C:\PATH\PlatformAbstraction\Pal3\target/findbugsXml.xml 0
[INFO] C:\PATH\PlatformAbstraction\Pal4\target/findbugsXml.xml 0
[INFO] C:\PATH\Framework\Common\target/findbugsXml.xml 12
[INFO] C:\PATH\Framework\legacyFramework\target/findbugsXml.xml 7
[INFO] C:\PATH\Framework\UIFramework\target/findbugsXml.xml 7
[INFO] C:\PATH\ExecutionLayer\Stub\target/findbugsXml.xml 0
[INFO] C:\PATH\ExecutionLayer\BB\BB\target/findbugsXml.xml 1
[INFO] TOTAL = 130
[INFO] -------
[INFO] Number of bugs 130 falls BELOW summaryThreshold 260. Check OK
You can do this with Violations Maven Plugin. It is configured with patterns to identify report files on the filesystem. It needs to run after findbugs, or any other static code analysis tool.
It will
Print the violations in the build log.
Optionally fail the build if number of violations found is higher then a configured number.