|
| 1 | + ------ |
| 2 | + Finding duplicated code in C# |
| 3 | + ------ |
| 4 | + Andreas Dangel |
| 5 | + ------ |
| 6 | + 2020-10-02 |
| 7 | + ------ |
| 8 | + |
| 9 | + ~~ Licensed to the Apache Software Foundation (ASF) under one |
| 10 | + ~~ or more contributor license agreements. See the NOTICE file |
| 11 | + ~~ distributed with this work for additional information |
| 12 | + ~~ regarding copyright ownership. The ASF licenses this file |
| 13 | + ~~ to you under the Apache License, Version 2.0 (the |
| 14 | + ~~ "License"); you may not use this file except in compliance |
| 15 | + ~~ with the License. You may obtain a copy of the License at |
| 16 | + ~~ |
| 17 | + ~~ http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | + ~~ |
| 19 | + ~~ Unless required by applicable law or agreed to in writing, |
| 20 | + ~~ software distributed under the License is distributed on an |
| 21 | + ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 22 | + ~~ KIND, either express or implied. See the License for the |
| 23 | + ~~ specific language governing permissions and limitations |
| 24 | + ~~ under the License. |
| 25 | + |
| 26 | +~~ NOTE: For help with the syntax of this file, see: |
| 27 | +~~ http://maven.apache.org/doxia/references/apt-format.html |
| 28 | + |
| 29 | +Finding duplicated code in C# |
| 30 | + |
| 31 | + By default, the maven-pmd-plugin only supports the languages Java, JavaScript and JSP. |
| 32 | + But {{{https://pmd.github.io/latest/pmd_userdocs_cpd.html#supported-languages}CPD supports many more languages}}, |
| 33 | + e.g. C#. In order to enable C# in your build, you need to |
| 34 | + configure several parts: |
| 35 | + |
| 36 | + * Add an additional plugin dependency for c# (pmd-cs module) |
| 37 | + |
| 38 | + * Select the language <<<cs>>>. |
| 39 | + |
| 40 | + * Configure the includes filter to consider <<<*.cs>>> (otherwise only java files will be analyzed) |
| 41 | + |
| 42 | + * Configure the source directory (by default, only <<<src/main/java>>> is analyzed) |
| 43 | + |
| 44 | + |
| 45 | ++-----+ |
| 46 | +<project> |
| 47 | + ... |
| 48 | + <build> |
| 49 | + <plugins> |
| 50 | + <plugin> |
| 51 | + <groupId>org.apache.maven.plugins</groupId> |
| 52 | + <artifactId>maven-pmd-plugin</artifactId> |
| 53 | + <version>${project.version}</version> |
| 54 | + <configuration> |
| 55 | + <language>cs</language> |
| 56 | + <minimumTokens>10</minimumTokens> |
| 57 | + <includes> |
| 58 | + <include>**/*.cs</include> |
| 59 | + </includes> |
| 60 | + <compileSourceRoots> |
| 61 | + <compileSourceRoot>${basedir}/src/main/cs</compileSourceRoot> |
| 62 | + </compileSourceRoots> |
| 63 | + <printFailingErrors>true</printFailingErrors> |
| 64 | + </configuration> |
| 65 | + <executions> |
| 66 | + <execution> |
| 67 | + <goals> |
| 68 | + <goal>cpd-check</goal> |
| 69 | + </goals> |
| 70 | + </execution> |
| 71 | + </executions> |
| 72 | + <dependencies> |
| 73 | + <dependency> |
| 74 | + <groupId>net.sourceforge.pmd</groupId> |
| 75 | + <artifactId>pmd-cs</artifactId> |
| 76 | + <version>${pmdVersion}</version> |
| 77 | + </dependency> |
| 78 | + </dependencies> |
| 79 | + </plugin> |
| 80 | + ... |
| 81 | + </plugins> |
| 82 | + </build> |
| 83 | +</project> |
| 84 | ++-----+ |
| 85 | + |
| 86 | + In this example, the C# source files are located in <<<src/main/cs>>>. |
| 87 | + |
| 88 | + <<Note:>> The version for <<<net.sourceforge.pmd:pmd-cs>>> needs to match the PMD version, that is |
| 89 | + being used. If you {{{./upgrading-PMD-at-runtime.html}upgrade PMD at runtime}} you'll need to make |
| 90 | + sure, to change the version here as well. |
| 91 | + |
0 commit comments