Coverage Report - org.apache.maven.report.projectinfo.ProjectSummaryReport
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectSummaryReport
100 %
6/6
N/A
4,7
ProjectSummaryReport$ProjectSummaryRenderer
59 %
58/98
24 %
14/58
4,7
 
 1  
 package org.apache.maven.report.projectinfo;
 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 org.apache.maven.doxia.sink.Sink;
 23  
 import org.apache.maven.model.DistributionManagement;
 24  
 import org.apache.maven.model.Organization;
 25  
 import org.apache.maven.plugins.annotations.Mojo;
 26  
 import org.apache.maven.project.MavenProject;
 27  
 import org.apache.maven.reporting.MavenReportException;
 28  
 import org.codehaus.plexus.util.FileUtils;
 29  
 import org.codehaus.plexus.util.StringUtils;
 30  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 31  
 
 32  
 import java.io.File;
 33  
 import java.io.IOException;
 34  
 import java.util.Locale;
 35  
 
 36  
 /**
 37  
  * Generates the project information reports summary.
 38  
  *
 39  
  * @author Edwin Punzalan
 40  
  * @version $Id: ProjectSummaryReport.java 1359783 2012-07-10 16:57:48Z tchemit $
 41  
  * @since 2.0
 42  
  */
 43  
 @Mojo( name = "summary" )
 44  1
 public class ProjectSummaryReport
 45  
     extends AbstractProjectInfoReport
 46  
 {
 47  
     // ----------------------------------------------------------------------
 48  
     // Public methods
 49  
     // ----------------------------------------------------------------------
 50  
 
 51  
     @Override
 52  
     protected void executeReport( Locale locale )
 53  
         throws MavenReportException
 54  
     {
 55  1
         new ProjectSummaryRenderer( getSink(), locale ).render();
 56  1
     }
 57  
 
 58  
     /** {@inheritDoc} */
 59  
     public String getOutputName()
 60  
     {
 61  2
         return "project-summary";
 62  
     }
 63  
 
 64  
     @Override
 65  
     protected String getI18Nsection()
 66  
     {
 67  1
         return "summary";
 68  
     }
 69  
 
 70  
     // ----------------------------------------------------------------------
 71  
     // Private
 72  
     // ----------------------------------------------------------------------
 73  
 
 74  
     /**
 75  
      * Internal renderer class
 76  
      */
 77  1
     private class ProjectSummaryRenderer
 78  
         extends AbstractProjectInfoRenderer
 79  
     {
 80  
         ProjectSummaryRenderer( Sink sink, Locale locale )
 81  1
         {
 82  1
             super( sink, getI18N( locale ), locale );
 83  1
         }
 84  
 
 85  
         @Override
 86  
         protected String getI18Nsection()
 87  
         {
 88  18
             return "summary";
 89  
         }
 90  
 
 91  
         @Override
 92  
         protected void renderBody()
 93  
         {
 94  1
             startSection( getTitle() );
 95  
 
 96  
             // general information sub-section
 97  1
             startSection( getI18nString( "general.title" ) );
 98  1
             startTable();
 99  1
             tableHeader( new String[] { getI18nString( "field" ), getI18nString( "value" ) } );
 100  1
             tableRow( new String[] { getI18nString( "general.name" ), project.getName() } );
 101  1
             tableRow( new String[] { getI18nString( "general.description" ), project.getDescription() } );
 102  1
             tableRowWithLink( new String[] { getI18nString( "general.homepage" ), project.getUrl() } );
 103  1
             endTable();
 104  1
             endSection();
 105  
 
 106  
             // organization sub-section
 107  1
             startSection( getI18nString( "organization.title" ) );
 108  1
             Organization organization = project.getOrganization();
 109  1
             if ( organization == null )
 110  
             {
 111  1
                 paragraph( getI18nString( "noorganization" ) );
 112  
             }
 113  
             else
 114  
             {
 115  0
                 startTable();
 116  0
                 tableHeader( new String[] { getI18nString( "field" ), getI18nString( "value" ) } );
 117  0
                 tableRow( new String[] { getI18nString( "organization.name" ), organization.getName() } );
 118  0
                 tableRowWithLink( new String[] { getI18nString( "organization.url" ), organization.getUrl() } );
 119  0
                 endTable();
 120  
             }
 121  1
             endSection();
 122  
 
 123  
             // build section
 124  1
             startSection( getI18nString( "build.title" ) );
 125  1
             startTable();
 126  1
             tableHeader( new String[] { getI18nString( "field" ), getI18nString( "value" ) } );
 127  1
             tableRow( new String[] { getI18nString( "build.groupid" ), project.getGroupId() } );
 128  1
             tableRow( new String[] { getI18nString( "build.artifactid" ), project.getArtifactId() } );
 129  1
             tableRow( new String[] { getI18nString( "build.version" ), project.getVersion() } );
 130  1
             tableRow( new String[] { getI18nString( "build.type" ), project.getPackaging() } );
 131  1
             if ( isJavaProject( project ) )
 132  
             {
 133  1
                 tableRow( new String[] { getI18nString( "build.jdk" ), getMinimumJavaVersion() } );
 134  
             }
 135  1
             endTable();
 136  1
             endSection();
 137  
 
 138  
             // download section
 139  1
             DistributionManagement distributionManagement = project.getDistributionManagement();
 140  1
             if ( distributionManagement != null )
 141  
             {
 142  0
                 if ( StringUtils.isNotEmpty( distributionManagement.getDownloadUrl() ) )
 143  
                 {
 144  0
                     startSection( getI18nString( "download" ) );
 145  0
                     link( distributionManagement.getDownloadUrl(), distributionManagement.getDownloadUrl() );
 146  0
                     endSection();
 147  
                 }
 148  
             }
 149  
 
 150  1
             endSection();
 151  1
         }
 152  
 
 153  
         private String getMinimumJavaVersion()
 154  
         {
 155  1
             Xpp3Dom pluginConfig =
 156  
                 project.getGoalConfiguration( "org.apache.maven.plugins", "maven-compiler-plugin", null, null );
 157  
 
 158  1
             String source = null;
 159  1
             String target = null;
 160  1
             String compilerVersion = null;
 161  
 
 162  1
             if ( pluginConfig != null )
 163  
             {
 164  0
                 source = getChildValue( pluginConfig, "source" );
 165  0
                 target = getChildValue( pluginConfig, "target" );
 166  
 
 167  0
                 String fork = getChildValue( pluginConfig, "fork" );
 168  0
                 if ( "true".equalsIgnoreCase( fork ) )
 169  
                 {
 170  0
                     compilerVersion = getChildValue( pluginConfig, "compilerVersion" );
 171  
                 }
 172  
             }
 173  
 
 174  1
             String minimumJavaVersion = compilerVersion;
 175  1
             if ( target != null )
 176  
             {
 177  0
                 minimumJavaVersion = target;
 178  
             }
 179  1
             else if ( source != null )
 180  
             {
 181  0
                 minimumJavaVersion = source;
 182  
             }
 183  1
             else if ( compilerVersion != null )
 184  
             {
 185  0
                 minimumJavaVersion = compilerVersion;
 186  
             }
 187  
             else
 188  
             {
 189  
                 // no source, target, compilerVersion: toolchain? default target attribute of current
 190  
                 // maven-compiler-plugin's version? analyze packaged jar (like dependencies)?
 191  
             }
 192  
 
 193  1
             return minimumJavaVersion;
 194  
         }
 195  
 
 196  
         private String getChildValue( Xpp3Dom parent, String childName )
 197  
         {
 198  0
             if ( parent == null )
 199  
             {
 200  0
                 return null;
 201  
             }
 202  
 
 203  0
             Xpp3Dom child = parent.getChild( childName );
 204  
 
 205  0
             if ( child == null )
 206  
             {
 207  0
                 return null;
 208  
             }
 209  
 
 210  0
             String value = child.getValue();
 211  
 
 212  0
             if ( value == null || value.trim().length() == 0 )
 213  
             {
 214  0
                 return null;
 215  
             }
 216  
 
 217  0
             return value.trim();
 218  
         }
 219  
 
 220  
         private void tableRowWithLink( String[] content )
 221  
         {
 222  1
             sink.tableRow();
 223  
 
 224  3
             for ( int ctr = 0; ctr < content.length; ctr++ )
 225  
             {
 226  2
                 String cell = content[ctr];
 227  
 
 228  2
                 sink.tableCell();
 229  
 
 230  2
                 if ( StringUtils.isEmpty( cell ) )
 231  
                 {
 232  1
                     sink.text( "-" );
 233  
                 }
 234  1
                 else if ( ctr == content.length - 1 && cell.length() > 0 )
 235  
                 {
 236  0
                     sink.link( cell );
 237  0
                     sink.text( cell );
 238  0
                     sink.link_();
 239  
                 }
 240  
                 else
 241  
                 {
 242  1
                     sink.text( cell );
 243  
                 }
 244  2
                 sink.tableCell_();
 245  
             }
 246  
 
 247  1
             sink.tableRow_();
 248  1
         }
 249  
 
 250  
         /**
 251  
          * @param project not null
 252  
          * @return return <code>true</code> if the Maven project sounds like a Java Project, i.e. has a java type
 253  
          *         packaging (like jar, war...) or java files in the source directory, <code>false</code> otherwise.
 254  
          * @since 2.3
 255  
          */
 256  
         private boolean isJavaProject( MavenProject project )
 257  
         {
 258  1
             String packaging = project.getPackaging().trim().toLowerCase( Locale.ENGLISH );
 259  1
             if ( packaging.equals( "pom" ) )
 260  
             {
 261  0
                 return false;
 262  
             }
 263  
 
 264  
             // some commons java packaging
 265  1
             if ( packaging.equals( "jar" ) || packaging.equals( "ear" ) || packaging.equals( "war" )
 266  
                 || packaging.equals( "rar" ) || packaging.equals( "sar" ) || packaging.equals( "har" )
 267  
                 || packaging.equals( "par" ) || packaging.equals( "ejb" ) )
 268  
             {
 269  1
                 return true;
 270  
             }
 271  
 
 272  
             // java files in the source directory?
 273  0
             final File sourceDir = new File( project.getBuild().getSourceDirectory() );
 274  0
             if ( sourceDir.exists() )
 275  
             {
 276  
                 try
 277  
                 {
 278  0
                     if ( FileUtils.getFileNames( sourceDir, "**/*.java", null, false ).size() > 0 )
 279  
                     {
 280  0
                         return true;
 281  
                     }
 282  
                 }
 283  0
                 catch ( IOException e )
 284  
                 {
 285  
                     //ignored
 286  0
                 }
 287  
             }
 288  
 
 289  
             // maven-compiler-plugin ?
 290  0
             Xpp3Dom pluginConfig =
 291  
                 project.getGoalConfiguration( "org.apache.maven.plugins", "maven-compiler-plugin", null, null );
 292  0
             if ( pluginConfig != null )
 293  
             {
 294  0
                 return true;
 295  
             }
 296  
 
 297  0
             return false;
 298  
         }
 299  
     }
 300  
 }