The Duplicates Finder (Java) Build Runner is intended for catching similar code fragments and providing a report on discovered repetitive blocks of Java code. This runner is based on IntelliJ IDEA capabilities, thus an IntelliJ IDEA project file (.ipr) or directory (.idea) is required to configure the runner. The Duplicates Finder (Java) can also find Java duplicates in projects being built by Maven2.
note
In order to run inspections for your project you should have either an IntelliJ IDEA project file (.ipr)/project directory (.idea), or Maven2 pom.xml of your project checked into your version control.
IntelliJ IDEA Project Settings
Option
Description
Project file type
To be able to run IntelliJ IDEA duplicates finder engine, TeamCity requires either IntelliJ IDEA project file/directory, or Maven2 pom.xml to be specified here.
Path to the project
Depending on what type of project you have selected in the Project file type, specify here:
For IntelliJ IDEA project: the path to the project file (.ipr) or path to the project directory (root directory of the project that contains .idea folder).
For Maven2 project: path to the pom.xml file. This information is required by this build runner to understand the structure of the project.
tip
Specified path should be relative to the checkout directory.
Detect global libraries and module-based JDK in the *.iml files
In IntelliJ IDEA module settings are stored in *.iml files, thus if this option is checked, all the module files will be automatically scanned for references to the global libraries and module JDKs when saved. This helps you ensure all references will be properly resolved.
warning
When this option is selected, the process of opening and saving the build runner settings may become time-consuming, because it involves loading and parsing all project module files.
Check/Reparse Project
Click this button to reparse your Maven2/IntelliJ IDEA project and import build settings right from the project, for example the list of JDKs.
note
If you update your project settings in IntelliJ IDEA or in the pom.xml - add new jdks, libraries, don't forget to update build runner settings by clicking Check/Reparse Project.
This section is displayed, when an IntelliJ IDEA module file (.iml) referenced from IPR-file:
cannot be found
allows you to enter the values of path variables used in the IPR-file.
To refresh values in this section click Check/Reparse Project.
Option
Description
<path_variable_name>
This field appears, if the project file contains path macros, defined in the Path Variables dialog of IntelliJ IDEA's Settings dialog. In the Set value to field, specify a path to project resources, to be used on different build agents.
Project JDKs
This section provides the list of JDKs detected in the project.
Option
Description
JDK Home
Use this field to specify JDK home for the project.
note
When building with the Ipr runner, this JDK will be used to compile the sources of corresponding IDEA modules. For Inspections and Duplicate Finder builds, this JDK will be used internally to resolve the Java API used in your project. To run the build process itself the JDK specified in the JAVA_HOME environment variable will be used.
JDK Jar File Patterns
Click this link to open a text area, where you can define templates for the jar files of the project JDK. Use Ant rules to define the jar file patterns. The default value is used for Linux and Windows operating systems:
jre/lib/*.jar
For Mac OS X, use the following lines:
lib/*.jar ../Classes/*.jar
IDEA Home
If your project uses the IDEA JDK, specify the location of IDEA home directory
IDEA Jar Files Patterns
Click this link to open a text area, where you can define templates for the jar files of the IDEA JDK.
note
You can use references to external properties when defining the values, like %\system.idea_home% or %\env.JDK_1_3%. This will add a Agent Requirements for the corresponding property.
Java Parameters
Option
Description
JDK home path
Use this field to specify the path to your custom JDK which should be used to run the build. If the field is left blank, the path to JDK Home is read either from the JAVA_HOME environment variable on agent computer, or from env.JAVA_HOME property specified in the build agent configuration file (buildAgent.properties). If these both values are not specified, TeamCity uses Java home of the build agent process itself.
JVM command line parameters
Specify the desired Java Virtual Machine parameters, such as maximum heap size or parameters that enable remote debugging. These settings are passed to the JVM used to run your build. Example:
-Xmx512m -Xms256m
Duplicate Finder Settings
Option
Description
Test sources
If this option is checked, the test sources will be included in the duplicates analysis.
tip
Tests contain the test data which can be duplicated. Thus, it does not make much sense to verify tests for duplicates. We recommend you not to select this option to avoid too long builds creation.
Detalization level
Use these options to define which elements of the source code should be distinguished when searching for repetitive code fragments. Code fragments can be considered duplicated, if they are structurally similar, but contain different variables, fields, methods, types or literals. Refer to the samples below:
Distinguish Variables
If this option is checked, the similar contents with different variable names will be recognized as different. If this option is not checked, such contents will be recognized as duplicated:
public static void main(String[] args) { int i = 0; int j = 0; if (i == j) { System.out.println("sum of " + i + " and " + j + " = " + i + j); } long k = 0; long n = 0; if (k == n) { System.out.println("sum of " + k + " and " + n + " = " + k + n); } }
Distinguish Fields
If this option is checked, the similar contents with different field names will be recognized as different. If this option is not checked, such contents will be recognized as duplicated:
If this option is checked, the methods of similar structure will be recognized as different. If this option is not checked, such methods will be recognized as duplicated. In this case, they can be extracted and reused. Initial version:
public void buildCanceled(Build build, SessionData data) { /* ... **/ for (IListener listener : getListeners()) { listener.buildCanceled(build, data); } } public void buildFinished(Build build, SessionData data) { /* ... **/ for (IListener listener : getListeners()) { listener.buildFinished(build, data); } }
After duplicates analysis without distinguishing methods, the duplicated fragments can be extracted:
public void buildCanceled(final Build build, final SessionData data) { enumerateListeners(new Processor() { public void process(final IListener listener) { listener.buildCanceled(build, data); } }); } public void buildFinished(final Build build, final SessionData data) { enumerateListeners(new Processor() { public void process(final IListener listener) { listener.buildFinished(build, data); } }); } private void enumerateListeners(Processor processor) {/* ... **/ for (IListener listener : getListeners()) { processor.process(listener); } } private interface Processor { void process(IListener listener); }
Distinguish Types
If this option is checked, the similar code fragments with different type names will be recognized as different. If this option is not checked, such code fragments will be recognized as duplicates.
new MyIDE().updateStatus() new TheirIDE().updateStatus()
Distinguish Literals
If this option is checked, similar line of code with different litarels will be considered different If this option is not checked, such lines will be recognized as duplicates.
Complexity of the source code is defined by the amount of statements, expressions, declarations and method calls. Complexity of each of them is defined by its cost. Summarized costs of all these elements of the source code fragment yields the total complexity. Use this field to specify the lowest level of complexity of the source code to be taken into consideration when detecting duplicates. For meaningful results start with value 10.
Ignore Duplicate Subexpressions with Complexity Simpler Than
Use this field to specify the lowest level of complexity of subexpressions to be taken into consideration when detecting duplicates.
Check if Subexpression Can be Extracted
If this option is checked, the duplicated subexpressions can be extracted.
tip
If you need to restrict the sources scope on which to run the Inspections or Duplicates runner, you can specify additional include / exclude patterns. Include / exclude patterns are newline-delimited set of rules of the form:
[+:|-:]pattern
Where pattern must satisfy these rules:
must end with either ** or * (this effectively limits the patterns to only the directories level, they do not support file-level patterns)
references to modules can be included as [module_name]/<path_within_module>
Some notes on patterns processing:
excludes have precedence over includes
if include patterns are specified, only directories matching these patterns will be included, all other directories will be excluded
"include" pattern has a special behavior (due to underlying limitations): it includes the directory specified and all the files residing directly in the directories above the one specified.
For the file paths to be reported correctly, "References to resources outside project/module file directory" option for the project and all modules should be set to "Relative" in IDEA project.