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   * @since 2.5
36   */
37  @Mojo( name = "dependency-info" )
38  public final class DependencyInformationReport
39      extends AbstractProjectInfoReport
40  {
41  
42      private static final String JAR_PACKAGING = "jar";
43  
44      /**
45       */
46      @Parameter( defaultValue = "${project.groupId}", required = true )
47      protected String groupId;
48  
49      /**
50       */
51      @Parameter( defaultValue = "${project.artifactId}", required = true )
52      protected String artifactId;
53  
54      /**
55       */
56      @Parameter( defaultValue = "${project.version}", required = true )
57      protected String version;
58  
59      /**
60       */
61      @Parameter( defaultValue = "${project.packaging}", required = true )
62      protected String packaging;
63  
64      // ----------------------------------------------------------------------
65      // Public methods
66      // ----------------------------------------------------------------------
67  
68      /**
69       * {@inheritDoc}
70       */
71      public String getOutputName()
72      {
73          return "dependency-info";
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      protected String getI18Nsection()
81      {
82          return "dependency-info";
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      protected void executeReport( Locale locale )
90          throws MavenReportException
91      {
92          new DependencyInformationRenderer( getSink(), getI18N( locale ), locale, groupId, artifactId, version,
93                                             packaging ).render();
94      }
95  
96      // ----------------------------------------------------------------------
97      // Private
98      // ----------------------------------------------------------------------
99  
100     private static final class DependencyInformationRenderer
101         extends AbstractProjectInfoRenderer
102     {
103 
104         private final String groupId;
105 
106         private final String artifactId;
107 
108         private final String version;
109 
110         private final String packaging;
111 
112         DependencyInformationRenderer( Sink sink, I18N i18n, Locale locale, String groupId, String artifactId,
113                                               String version, String packaging )
114         {
115             super( sink, i18n, locale );
116             this.groupId = groupId;
117             this.artifactId = artifactId;
118             this.version = version;
119             this.packaging = packaging;
120         }
121 
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         protected String getI18Nsection()
127         {
128             return "dependency-info";
129         }
130 
131         /**
132          * {@inheritDoc}
133          */
134         @Override
135         protected void renderBody()
136         {
137             startSection( getTitle() );
138 
139             Formatter mavenDependency =
140                 new Formatter().format( "<dependency>%n" ).format( "  <groupId>%s</groupId>%n", groupId ).format(
141                     "  <artifactId>%s</artifactId>%n", artifactId ).format( "  <version>%s</version>%n", version );
142 
143             if ( !JAR_PACKAGING.equals( packaging ) )
144             {
145                 mavenDependency = mavenDependency.format( "  <type>%s</type>%n", packaging );
146             }
147 
148             renderDependencyInfo( "Apache Maven", mavenDependency.format( "</dependency>" ) );
149 
150             renderDependencyInfo( "Apache Buildr",
151                                   new Formatter().format( "'%s:%s:%s:%s'", groupId, artifactId, packaging, version ) );
152 
153             renderDependencyInfo( "Apache Ivy",
154                                   new Formatter().format( "<dependency org=\"%s\" name=\"%s\" rev=\"%s\">%n", groupId,
155                                                           artifactId, version ).format(
156                                       "  <artifact name=\"%s\" type=\"%s\" />%n", artifactId, packaging ).format(
157                                       "</dependency>" ) );
158 
159             renderDependencyInfo( "Groovy Grape", new Formatter().format( "@Grapes(%n" ).format(
160                 "@Grab(group='%s', module='%s', version='%s')%n", groupId, artifactId, version ).format( ")" ) );
161 
162             renderDependencyInfo( "Gradle/Grails",
163                                   new Formatter().format( "compile '%s:%s:%s'", groupId, artifactId, version ) );
164 
165             renderDependencyInfo( "Scala SBT", new Formatter().format(
166                 "libraryDependencies += \"%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             endSection();
182         }
183 
184         private void renderDependencyInfo( String name, Formatter formatter )
185         {
186             startSection( name );
187             verbatimText( formatter.toString() );
188             endSection();
189         }
190 
191     }
192 
193 }