package org.apache.maven.report.projectinfo;

import java.util.Formatter;
import java.util.Locale;

import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.i18n.I18N;

/**
 * Generates the Dependency code snippets to be added to build tools.
 *
 * @author <a href="mailto:simonetripodi@apache.org">Simone Tripodi</a>
 * @version $Id$
 * @since 2.4.1
 * @goal dependency-info
 */
public final class DependencyInformationReport
    extends AbstractProjectInfoReport
{

    private static final String DEPENDENCY_INFO = "dependency-info";

    /**
     * @parameter default-value="${project.groupId}"
     * @required
     */
    protected String groupId;

    /**
     * @parameter default-value="${project.artifactId}"
     * @required
     */
    protected String artifactId;

    /**
     * @parameter default-value="${project.version}"
     * @required
     */
    protected String version;

    /**
     * @parameter default-value="${project.packaging}"
     * @required
     */
    protected String packaging;

    // ----------------------------------------------------------------------
    // Public methods
    // ----------------------------------------------------------------------

    /**
     * {@inheritDoc}
     */
    public String getOutputName()
    {
        return DEPENDENCY_INFO;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getI18Nsection()
    {
        return DEPENDENCY_INFO;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void executeReport( Locale locale )
        throws MavenReportException
    {
        new DependencyInformationRenderer( getSink(), getI18N( locale ), locale,
                                           groupId, artifactId, version, packaging )
            .render();
    }

    // ----------------------------------------------------------------------
    // Private
    // ----------------------------------------------------------------------

    private static final class  DependencyInformationRenderer
        extends AbstractProjectInfoRenderer
    {

        private final String groupId;

        private final String artifactId;

        private final String version;

        private final String packaging;

        public DependencyInformationRenderer( Sink sink, I18N i18n, Locale locale,
                                              String groupId, String artifactId, String version, String packaging )
        {
            super( sink, i18n, locale );
            this.groupId = groupId;
            this.artifactId = artifactId;
            this.version = version;
            this.packaging = packaging;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected String getI18Nsection()
        {
            return DEPENDENCY_INFO;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        protected void renderBody()
        {
            startSection( getTitle() );

            renderDependencyInfo( "Apache Maven", new Formatter()
                                                        .format( "<dependency>%n" )
                                                        .format( "  <groupId>%s</groupId>%n", groupId )
                                                        .format( "  <artifactId>%s</artifactId>%n", artifactId )
                                                        .format( "  <version>%s</version>%n", version )
                                                        .format( "  <packaging>%s</packaging>%n", packaging )
                                                        .format( "</dependency>" ) );

            renderDependencyInfo( "Apache Buildr", new Formatter().format( "'%s:%s:%s:%s'",
                                                                           groupId, artifactId, packaging, version ) );

            renderDependencyInfo( "Apache Ant", new Formatter()
                                                        .format( "<dependency org=\"%s\" name=\"%s\" rev=\"%s\">%n",
                                                                 groupId, artifactId, version )
                                                        .format( "  <artifact name=\"%s\" type=\"%s\" />%n",
                                                                 artifactId, packaging )
                                                        .format( "</dependency>" ) );

            renderDependencyInfo( "Groovy Grape", new Formatter()
                                                        .format( "@Grapes(%n" )
                                                        .format( "@Grab(group='%s', module='%s', version='%s')%n",
                                                                 groupId,
                                                                 artifactId,
                                                                 version )
                                                        .format( ")" ) );

            renderDependencyInfo( "Grails", new Formatter().format( "compile '%s:%s:%s'",
                                                                    groupId, artifactId, version ) );

            endSection();
        }

        private void renderDependencyInfo( String name, Formatter formatter )
        {
            startSection( name );
            verbatimText( formatter.toString() );
            endSection();
        }

    }

}

@@ -598,16 +598,72 @@
 info%22;%0A%0A
+    private static final String JAR_PACKAGING = %22jar%22;%0A%0A
     /**%0A
@@ -3097,45 +3097,35 @@
     
-renderDependencyInfo( %22Apache Maven%22,
+Formatter mavenDependency =
  new
@@ -3133,32 +3133,24 @@
 Formatter()%0A
-        
             
@@ -3257,32 +3257,24 @@
             
-        
 .format( %22  
@@ -3353,32 +3353,24 @@
             
-      
-  
 .format( %22  
@@ -3458,32 +3458,24 @@
             
-        
 .format( %22  
@@ -3501,32 +3501,34 @@
 n%3E%25n%22, version )
+;%0A
 %0A               
@@ -3520,38 +3520,66 @@
             
-              
+if ( !JAR_PACKAGING.equals( packaging ) )%0A
             
@@ -3570,32 +3570,34 @@
 ) )%0A            
+%7B%0A
                 
@@ -3588,34 +3588,65 @@
                 
-  
+mavenDependency = mavenDependency
 .format( %22  %3Cpac
@@ -3674,32 +3674,33 @@
 %25n%22, packaging )
+;
 %0A               
@@ -3700,52 +3700,76 @@
     
-                                            
+%7D%0A%0A            renderDependencyInfo( %22Apache Maven%22, mavenDependency
 .for

exclude the packaging from Maven dependency if the type is jar, since it is implicit Thanks to Robert Scholte