View Javadoc

1   package org.apache.maven.plugin.pmd;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugin.pmd.model.PmdErrorDetail;
31  import org.apache.maven.plugin.pmd.model.PmdFile;
32  import org.apache.maven.plugin.pmd.model.Violation;
33  import org.apache.maven.plugin.pmd.model.io.xpp3.PmdXpp3Reader;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
36  
37  /**
38   * Fail the build if there were any PMD violations in the source code.
39   *
40   * @since 2.0
41   * @version $Id: PmdViolationCheckMojo.html 816696 2012-05-08 15:19:20Z hboutemy $
42   * @goal check
43   * @phase verify
44   * @execute goal="pmd"
45   * @threadSafe
46   */
47  public class PmdViolationCheckMojo
48      extends AbstractPmdViolationCheckMojo<Violation>
49  {
50      /**
51       * What priority level to fail the build on. Failures at or above this level
52       * will stop the build. Anything below will be warnings and will be
53       * displayed in the build output if verbose=true. Note: Minimum Priority = 5
54       * Maximum Priority = 0
55       *
56       * @parameter expression="${pmd.failurePriority}" default-value="5"
57       * @required
58       */
59      private int failurePriority;
60  
61      /**
62       * Skip the PMD checks.  Most useful on the command line
63       * via "-Dpmd.skip=true".
64       *
65       * @parameter expression="${pmd.skip}" default-value="false"
66       */
67      private boolean skip;
68  
69      /** {@inheritDoc} */
70      public void execute()
71          throws MojoExecutionException, MojoFailureException
72      {
73          if ( !skip )
74          {
75              executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority );
76          }
77      }
78  
79      /** {@inheritDoc} */
80      protected void printError( Violation item, String severity )
81      {
82  
83          StringBuffer buff = new StringBuffer( 100 );
84          buff.append( "PMD " + severity + ": " );
85          if ( item.getViolationClass() != null )
86          {
87              if ( item.getViolationPackage() != null )
88              {
89                  buff.append( item.getViolationPackage() );
90                  buff.append( "." );
91              }
92              buff.append( item.getViolationClass() );
93          }
94          else
95          {
96              buff.append( item.getFileName() );
97          }
98          buff.append( ":" );
99          buff.append( item.getBeginline() );
100         buff.append( " Rule:" ).append( item.getRule() );
101         buff.append( " Priority:" ).append( item.getPriority() );
102         buff.append( " " ).append( item.getText() ).append( "." );
103 
104         this.getLog().info( buff.toString() );
105     }
106     
107     @Override
108     protected List<Violation> getErrorDetails( File pmdFile )
109         throws XmlPullParserException, IOException
110     {
111         PmdXpp3Reader reader = new PmdXpp3Reader();
112         PmdErrorDetail details = reader.read( new FileReader( pmdFile ), false );
113 
114         List<Violation> violations = new ArrayList<Violation>();
115         for( PmdFile file : details.getFiles() )
116         {
117             String fullPath = file.getName();
118             
119             for ( Violation violation : file.getViolations() )
120             {
121                 violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) );
122                 violations.add( violation );
123             }
124         }
125         return violations;
126     }
127     
128     @Override
129     protected int getPriority( Violation errorDetail )
130     {
131         return errorDetail.getPriority();
132     }
133     
134     @Override
135     protected ViolationDetails<Violation> newViolationDetailsInstance()
136     {
137         return new ViolationDetails<Violation>();
138     }
139     
140     private String getFilename( String fullpath, String pkg )
141     {
142         int index = fullpath.lastIndexOf( File.separatorChar );
143 
144         while ( StringUtils.isNotEmpty( pkg ) )
145         {
146             index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar );
147 
148             int dot = pkg.indexOf( '.' );
149 
150             if ( dot < 0 )
151             {
152                 break;
153             }
154             pkg = pkg.substring( dot + 1 );
155         }
156 
157         return fullpath.substring( index + 1 );
158     }
159 }