View Javadoc

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.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.reporting.MavenReportException;
26  import org.codehaus.plexus.i18n.I18N;
27  
28  import java.util.Formatter;
29  import java.util.Locale;
30  
31  /**
32   * Generates code snippets to be added to build tools.
33   *
34   * @author <a href="mailto:simonetripodi@apache.org">Simone Tripodi</a>
35   * @version $Id: DependencyInformationReport.java 1371107 2012-08-09 09:09:27Z hboutemy $
36   * @since 2.5
37   */
38  @Mojo( name = "dependency-info" )
39  public final class DependencyInformationReport
40      extends AbstractProjectInfoReport
41  {
42  
43      private static final String DEPENDENCY_INFO = "dependency-info";
44  
45      private static final String JAR_PACKAGING = "jar";
46  
47      /**
48       */
49      @Parameter( defaultValue = "${project.groupId}", required = true )
50      protected String groupId;
51  
52      /**
53       */
54      @Parameter( defaultValue = "${project.artifactId}", required = true )
55      protected String artifactId;
56  
57      /**
58       */
59      @Parameter( defaultValue = "${project.version}", required = true )
60      protected String version;
61  
62      /**
63       */
64      @Parameter( defaultValue = "${project.packaging}", required = true )
65      protected String packaging;
66  
67      // ----------------------------------------------------------------------
68      // Public methods
69      // ----------------------------------------------------------------------
70  
71      /**
72       * {@inheritDoc}
73       */
74      public String getOutputName()
75      {
76          return DEPENDENCY_INFO;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      protected String getI18Nsection()
84      {
85          return DEPENDENCY_INFO;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      protected void executeReport( Locale locale )
93          throws MavenReportException
94      {
95          new DependencyInformationRenderer( getSink(), getI18N( locale ), locale, groupId, artifactId, version,
96                                             packaging ).render();
97      }
98  
99      // ----------------------------------------------------------------------
100     // Private
101     // ----------------------------------------------------------------------
102 
103     private static final class DependencyInformationRenderer
104         extends AbstractProjectInfoRenderer
105     {
106 
107         private final String groupId;
108 
109         private final String artifactId;
110 
111         private final String version;
112 
113         private final String packaging;
114 
115         public DependencyInformationRenderer( Sink sink, I18N i18n, Locale locale, String groupId, String artifactId,
116                                               String version, String packaging )
117         {
118             super( sink, i18n, locale );
119             this.groupId = groupId;
120             this.artifactId = artifactId;
121             this.version = version;
122             this.packaging = packaging;
123         }
124 
125         /**
126          * {@inheritDoc}
127          */
128         @Override
129         protected String getI18Nsection()
130         {
131             return DEPENDENCY_INFO;
132         }
133 
134         /**
135          * {@inheritDoc}
136          */
137         @Override
138         protected void renderBody()
139         {
140             startSection( getTitle() );
141 
142             Formatter mavenDependency =
143                 new Formatter().format( "<dependency>%n" ).format( "  <groupId>%s</groupId>%n", groupId ).format(
144                     "  <artifactId>%s</artifactId>%n", artifactId ).format( "  <version>%s</version>%n", version );
145 
146             if ( !JAR_PACKAGING.equals( packaging ) )
147             {
148                 mavenDependency = mavenDependency.format( "  <type>%s</type>%n", packaging );
149             }
150 
151             renderDependencyInfo( "Apache Maven", mavenDependency.format( "</dependency>" ) );
152 
153             renderDependencyInfo( "Apache Buildr",
154                                   new Formatter().format( "'%s:%s:%s:%s'", groupId, artifactId, packaging, version ) );
155 
156             renderDependencyInfo( "Apache Ant",
157                                   new Formatter().format( "<dependency org=\"%s\" name=\"%s\" rev=\"%s\">%n", groupId,
158                                                           artifactId, version ).format(
159                                       "  <artifact name=\"%s\" type=\"%s\" />%n", artifactId, packaging ).format(
160                                       "</dependency>" ) );
161 
162             renderDependencyInfo( "Groovy Grape", new Formatter().format( "@Grapes(%n" ).format(
163                 "@Grab(group='%s', module='%s', version='%s')%n", groupId, artifactId, version ).format( ")" ) );
164 
165             renderDependencyInfo( "Grails",
166                                   new Formatter().format( "compile '%s:%s:%s'", groupId, artifactId, version ) );
167 
168             // Leiningen
169 
170             Formatter leiningenDependency = new Formatter().format( "[%s", groupId );
171 
172             if ( !groupId.equals( artifactId ) )
173             {
174                 leiningenDependency.format( "/%s", artifactId );
175             }
176 
177             leiningenDependency.format( " \"%s\"]", version );
178 
179             renderDependencyInfo( "Leiningen", leiningenDependency );
180 
181             // sbt
182 
183             renderDependencyInfo( "SBT", new Formatter().format( "libraryDependencies += \"%s\" %%%% \"%s\" %% \"%s\"",
184                                                                  groupId, artifactId, version ) );
185 
186             endSection();
187         }
188 
189         private void renderDependencyInfo( String name, Formatter formatter )
190         {
191             startSection( name );
192             verbatimText( formatter.toString() );
193             endSection();
194         }
195 
196     }
197 
198 }