﻿# PVS\-Studio Java quick start

This documentation covers the quick start of Java project analysis using plugins for the build systems [Gradle](https://pvs-studio.com/en/docs/manual/6706/) and [Maven](https://pvs-studio.com/en/docs/manual/6705/)\.

PVS\-Studio Java also provides integration with the IntelliJ IDEA IDE via a special plugin, and integration with Visual Studio Code via a special extension\. Learn more from the documentation:

* [How to use PVS\-Studio extension for Visual Studio Code](https://pvs-studio.com/en/docs/manual/6646/)\.
* [Using PVS\-Studio with IntelliJ IDEA and Android Studio](https://pvs-studio.com/en/docs/manual/6704/)\.

## PVS\-Studio quick start via Gradle

### Integration into the project via the build script

To integrate the plugin into a project using **Groovy DSL**, add the following code to the `build.gradle` script:

```cpp
buildscript {
  repositories {
    mavenCentral()
    maven {
      url uri('https://wcdn.pvs-studio.com/java/pvsstudio-maven-repository/')
    }
  }
  dependencies {
    classpath 'com.pvsstudio:pvsstudio-gradle-plugin:latest.release'
  }
}
apply plugin: com.pvsstudio.PvsStudioGradlePlugin
pvsstudio {
  outputType = 'json'
  outputFile = '.PVS-Studio/report.json'
  analysisMode = ['GA', 'OWASP']
}
```

For **Kotlin DSL**, add the following code to `build.gradle.kts`:

```cpp
import com.pvsstudio.WarningGroup

buildscript {
  repositories {
    mavenCentral()
    maven {
      url =
        uri("https://wcdn.pvs-studio.com/java/pvsstudio-maven-repository/")
    }
  }
  dependencies {
    classpath("com.pvsstudio:pvsstudio-gradle-plugin:latest.release")
  }
}

apply<com.pvsstudio.PvsStudioGradlePlugin>()

extensions.configure<com.pvsstudio.AnalyzerConfig>("pvsstudio") {
  outputType = "json"
  outputFile = ".PVS-Studio/report.json"
  analysisMode = setOf(WarningGroup.GA, WarningGroup.OWASP)
}
```

### Running the analysis

Before running the analysis, enter the license and activate it using the following command:

```cpp
./gradlew pvsCredentials "-Ppvsstudio.userName=${USER_NAME}" \
                         "-Ppvsstudio.licenseKey=${LICENSE_KEY}"
```

Replace the `USER_NAME` variable with the analyzer user name tied to the key, and replace `LICENSE_KEY` with the license key\. 

Now you can run the analysis by using the following command:

```cpp
./gradlew pvsAnalyze
```

**Note:** when checking a project, the plugin runs the [Java analyzer core](https://pvs-studio.com/en/docs/manual/6703/), which by default uses the Java version from the `PATH` environment variable\. You can specify a different version using the plugin's `javaPath` setting\. More details are available in the [documentation](https://pvs-studio.com/en/docs/manual/6706/#IDD24CCCC7F4)\.

## PVS\-Studio quick start via Maven

### Integration via the configuration file

To integrate the plugin into the project, add the following code to the `pom.xml` file:

```cpp
<pluginRepositories>
  <pluginRepository>
    <id>pvsstudio-maven-repo</id>
    <url>https://wcdn.pvs-studio.com/java/pvsstudio-maven-repository/</url>
  </pluginRepository>
</pluginRepositories>
<build>
  <plugins>
    <plugin>
      <groupId>com.pvsstudio</groupId>
      <artifactId>pvsstudio-maven-plugin</artifactId>
      <version>{{FULL_VERSION_PVS_JAVA}}</version>
      <configuration>
        <analyzer>
          <outputType>json</outputType>
          <outputFile>.PVS-Studio/report.json</outputFile>
          <analysisMode>GA, OWASP</analysisMode>
        </analyzer>
      </configuration>
    </plugin>
  </plugins>
</build>
```

### Running the analysis

Before running the analysis, enter the license and activate it using the following command:

```cpp
mvn pvsstudio:pvsCredentials "-Dpvsstudio.userName=${USER_NAME}" \
                             "-Dpvsstudio.licenseKey=${LICENSE_KEY}"
```

Replace the `USER_NAME` variable with the analyzer user name tied to the key, and replace `LICENSE_KEY` with the license key\. 

Now you can run the analysis by using the following command:

```cpp
mvn pvsstudio:pvsAnalyze
```

**Note**: when checking a project, the plugin runs the [Java analyzer core](https://pvs-studio.com/en/docs/manual/6703/), which by default uses the Java version from the `PATH` environment variable\. You can specify a different version using the plugin's `javaPath`setting\. More details are available in the [documentation](https://pvs-studio.com/en/docs/manual/6705/#IDD24CCCC7F4)\.

### Running analysis without integration into the build script

You can analyze Maven projects without modifying the `pom.xml` file\. In this case, the analysis starts by running the following command in the project directory:

For PowerShell:

```cpp
$repo = "https://files.pvs-studio.com/java/pvsstudio-maven-repository/"

mvn -f ./pom.xml `
 "-Dmaven.repo.remote= $repo" `
 "-Dmaven.test.skip=true" `
 "-Dpvsstudio.outputType=json" `
 "-Dpvsstudio.outputFile=.PVS-Studio/report.json" `
 "-Dpvsstudio.analysisMode=GA,OWASP" `
 com.pvsstudio:pvsstudio-maven-plugin:{{FULL_VERSION_PVS_JAVA}}:pvsAnalyze
```

For Bash:

```cpp
repo=https://files.pvs-studio.com/java/pvsstudio-maven-repository/ 

mvn -f ./pom.xml \
 -Dmaven.repo.remote= "$repo" \
 -Dmaven.test.skip=true \
 -Dpvsstudio.outputType=json \
 -Dpvsstudio.outputFile=.PVS-Studio/report.json \
 -Dpvsstudio.analysisMode=GA,OWASP \
 com.pvsstudio:pvsstudio-maven-plugin:{{FULL_VERSION_PVS_JAVA}}:pvsAnalyze
```

For cmd:

```cpp
set REPO=https://files.pvs-studio.com/java/pvsstudio-maven-repository/

mvn -f ./pom.xml ^
 "-Dmaven.repo.remote=%REPO%" ^
 "-Dmaven.test.skip=true" ^
 "-Dpvsstudio.outputType=json" ^
 "-Dpvsstudio.outputFile=.PVS-Studio/report.json" ^
 "-Dpvsstudio.analysisMode=GA,OWASP" ^
 com.pvsstudio:pvsstudio-maven-plugin:{{FULL_VERSION_PVS_JAVA}}:pvsAnalyze
```

## Handling analysis results

Once the analysis is complete, a file named `report.json`—containing PVS\-Studio Java analyzer warnings—is created in the `.PVS-Studio` directory at the project root\.

 You can review the report results using:

* [PVS\-Studio plugin for IntelliJ IDEA](https://pvs-studio.com/en/docs/manual/6704/#ID900DC5D0FF);
* [PVS\-Studio extension for Visual Studio Code](https://pvs-studio.com/en/docs/manual/6646/#ID7839E0A55B);
* [code quality tools](https://pvs-studio.com/en/docs/#ID82D66261D1);
* HTML report format\.

### Report conversion

You can generate the PVS\-Studio analyzer report in various formats\. To convert it from one supported format to another, use the [PlogConverter](https://pvs-studio.com/en/docs/manual/0038/) utility\. It is included in the [PVS\-Studio distribution](https://pvs-studio.com/en/pvs-studio/download/)\. One of the supported output formats is `FullHTML`\. It enables you to view the analyzer report in a browser, sort warnings, and view the source code snippet that triggered the analyzer warning\.

By default, the utility is located in the root analyzer directory\. To convert the report, run the following command in the directory that contains the `json` report:

For PowerShell:

```cpp
& "C:\Program Files (x86)\PVS-Studio\PlogConverter.exe" `
   -t FullHtml `
   -a "GA:1,2,3;OWASP:1,2,3" `
   -o . `
   report.json
```

For Bash:

```cpp
plog-converter \
 -t FullHtml \
 -a GA:1,23;OWASP:1,2,3 \
 -o . \
 report.json
```

For cmd:

```cpp
"C:\Program Files (x86)\PVS-Studio\PlogConverter.exe" ^
 -t FullHtml ^
 -a GA:1,2,3;OWASP:1,2,3 ^
 -o . ^
 report.json
```

As a result, the utility creates a `fullhtml` directory containing a set of files that form a web page with the analyzer report\. To view it, open the `index.html` file in your browser\.

## Links

Learn more about working with PVS\-Studio Java via the Gradle and Maven build systems in the documentation sections:

* [Integrating PVS\-Studio Java into the Gradle build system](https://pvs-studio.com/en/docs/manual/6706/)\.
* [Integrating PVS\-Studio Java into the Maven build system](https://pvs-studio.com/en/docs/manual/6705/)\.