2014-01-30 27 views
3

我是PMD/CPD的新手。我在Maven项目配置PMD如下:PMD/CPD无法检测到重复的代码

<groupId>org.parent</groupId> 
<artifactId>CustRestExampleOsgi</artifactId> 
<version>1.0</version> 

<packaging>pom</packaging> 
<name>CustRestExampleOsgii</name> 

<modules> 
    <module>CustImplProvider</module> 
    <module>CustInterface</module> 
    <module>RestCustConsumer</module> 
</modules> 

<properties> 
<karaf.deploy.build.folder> 
    G:\apache-karaf-3.0.0.RC1\deploy 
</karaf.deploy.build.folder> 
</properties> 

<reporting> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-pmd-plugin</artifactId> 
      <version>3.0</version> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jxr-plugin</artifactId> 
      <version>2.3</version> 
     </plugin> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-javadoc-plugin</artifactId> 
      <version>2.9.1</version> 
     </plugin> 
    </plugins> 
</reporting> 

我的Maven项目通常是编译和mvn jxr:jxr site生成的所有报告。 但我无法找到任何显示重复代码的结果。为了测试这个我已经在我的代码有意引入重复的代码,如:

@Override 
public boolean equals(Object object) { 
    // TODO: Warning - this method won't work in the case the id fields are not set 
    if (!(object instanceof Address)) { 
     return false; 
    } 
    Address other = (Address) object; 
    if ((this.id == null && other.id != null) 
     || (this.id != null && !this.id.equals(other.id))) { 
     return false; 
    } 
    if (!(object instanceof Address)) { //Duplicate is here 
     return false; 
    } 
    return true; 
} 

但始终CPD没有显示出在源代码中检测到的问题。但是,我可以正常发现PMD报告。我是否缺少一些配置或规则集?

请帮忙!

回答

4

确保您将最小令牌计数设置得足够低。您的重复代码短片的默认值少于默认值100.

根据the docs,该属性名为minimumTokens。老版本的Maven PMD插件有一个属性maven.pmd.cpd.minimumtokencount。将其设置为5进行测试。在现实生活中,100的默认值是一个很好的值。

+0

哦哇!它按预期完美工作。非常感谢!!! :-) – Amrit