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