[doc] Provide documentation for gradle integration and plain java API integration#2283
Merged
Conversation
Generated by 🚫 Danger |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Description:
This improves the documentation and gives an example on how to call PMD programmatically.
Refs #2274
Here's the content for easier reviewing:
The easiest way to run PMD is to just use a build plugin in your favorite build tool
like Apache Ant, Apache Maven or
Gradle.
There are also many integrations for IDEs available, see Tools.
If you have your own build tool or want to integrate PMD in a different way, you can call PMD programmatically,
as described here.
Dependencies
You'll need to add the dependency to the language, you want to analyze. For Java, it will be
net.sourceforge.pmd:pmd-java. If you use Maven, you can add a new (compile time) dependency like this:Note: You'll need to select a specific version. This is done in the example via the property
pmdVersion.This will transitively pull in the artifact
pmd-corewhich contains the API.Command line interface
The easiest way is to call PMD with the same interface as from command line. The main class is
net.sourceforge.pmd.PMD:It uses the same options as described in PMD CLI reference.
Programmatically, variant 1
This is very similar:
Programmatically, variant 2
This gives you more control over which files are processed, but is also more complicated.
You can also provide your own listeners and renderers.
First we create a
PMDConfiguration. This is currently the only way to specify a ruleset:In order to support type resolution, PMD needs to have access to the compiled classes and dependencies
as well. This is called "auxclasspath" and is also configured here.
Note: you can specify multiple class paths separated by
:on Unix-systems or;under Windows.Then we need a ruleset factory. This is created using the configuration, taking the minimum priority into
account:
PMD operates on a list of
DataSource. You can assemble a own list ofFileDataSource, e.g.For reporting, you can use a built-in renderer, e.g.
XMLRenderer. Note, that you must manually initializethe renderer by setting a suitable
Writerand callingstart(). After the PMD run, you need to callend()andflush(). Then your writer should have received all output.Create a
RuleContext. This is the context instance, that is available then in the rule implementations.Note: when running in multi-threaded mode (which is the default), the rule context instance is cloned for
each thread.
Optionally register a report listener. This allows you to react immediately on found violations. You could also
use such a listener to implement your own renderer. The listener must implement the interface
ThreadSafeReportListenerand can be registered viactx.getReport().addListener(...).Now, all the preparations are done, and PMD can be executed. This is done by calling
PMD.processFiles(...). This method call takes the configuration, the ruleset factory, the filesto process, the rule context and the list of renderers. Provide an empty list, if you don't want to use
any renderer. Note: The auxclasspath needs to be closed explicitly. Otherwise the class or jar files may
remain open and file resources are leaked.
After the call, you need to finish the renderer via
end()andflush().Then you can check the rendered output.
Here is a complete example: