View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo;
20  
21  import java.util.Formatter;
22  import java.util.Locale;
23  
24  import org.apache.maven.doxia.sink.Sink;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.reporting.MavenReportException;
28  import org.codehaus.plexus.i18n.I18N;
29  
30  /**
31   * Generates code snippets of the Maven coordinates to be added to build tools.
32   *
33   * @author <a href="mailto:simonetripodi@apache.org">Simone Tripodi</a>
34   * @since 2.5
35   */
36  @Mojo(name = "dependency-info")
37  public final class DependencyInformationReport extends AbstractProjectInfoReport {
38  
39      private static final String JAR_PACKAGING = "jar";
40      private static final String PLUGIN_PACKAGING = "maven-plugin";
41  
42      /**
43       */
44      @Parameter(defaultValue = "${project.groupId}", required = true)
45      protected String groupId;
46  
47      /**
48       */
49      @Parameter(defaultValue = "${project.artifactId}", required = true)
50      protected String artifactId;
51  
52      /**
53       */
54      @Parameter(defaultValue = "${project.version}", required = true)
55      protected String version;
56  
57      /**
58       */
59      @Parameter(defaultValue = "${project.packaging}", required = true)
60      protected String packaging;
61  
62      // ----------------------------------------------------------------------
63      // Public methods
64      // ----------------------------------------------------------------------
65  
66      /**
67       * {@inheritDoc}
68       */
69      public String getOutputName() {
70          return "dependency-info";
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      protected String getI18Nsection() {
78          return "dependency-info";
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      protected void executeReport(Locale locale) throws MavenReportException {
86          new DependencyInformationRenderer(getSink(), getI18N(locale), locale, groupId, artifactId, version, packaging)
87                  .render();
88      }
89  
90      // ----------------------------------------------------------------------
91      // Private
92      // ----------------------------------------------------------------------
93  
94      private static final class DependencyInformationRenderer extends AbstractProjectInfoRenderer {
95  
96          private final String groupId;
97  
98          private final String artifactId;
99  
100         private final String version;
101 
102         private final String packaging;
103 
104         DependencyInformationRenderer(
105                 Sink sink,
106                 I18N i18n,
107                 Locale locale,
108                 String groupId,
109                 String artifactId,
110                 String version,
111                 String packaging) {
112             super(sink, i18n, locale);
113             this.groupId = groupId;
114             this.artifactId = artifactId;
115             this.version = version;
116             this.packaging = packaging;
117         }
118 
119         /**
120          * {@inheritDoc}
121          */
122         @Override
123         protected String getI18Nsection() {
124             return "dependency-info";
125         }
126 
127         /**
128          * {@inheritDoc}
129          */
130         @Override
131         protected void renderBody() {
132             startSection(getTitle());
133 
134             if (PLUGIN_PACKAGING.equals(packaging)) {
135                 renderMavenPluginCoordinates();
136             } else {
137                 renderMavenDependencyCoordinates();
138                 renderIvyDependencyCoordinates();
139                 renderGrapeDependencyCoordinates();
140                 renderGradleDependencyCoordinates();
141                 renderScalaDependencyCoordinates();
142                 renderLeiningenDependencyCoordinates();
143             }
144 
145             endSection();
146         }
147 
148         private void renderMavenPluginCoordinates() {
149             Formatter plugin = new Formatter()
150                     .format("<plugin>%n")
151                     .format("  <groupId>%s</groupId>%n", groupId)
152                     .format("  <artifactId>%s</artifactId>%n", artifactId)
153                     .format("  <version>%s</version>%n", version)
154                     .format("</plugin>");
155 
156             renderDependencyInfo("Apache Maven", plugin);
157         }
158 
159         private void renderMavenDependencyCoordinates() {
160             Formatter mavenDependency = new Formatter()
161                     .format("<dependency>%n")
162                     .format("  <groupId>%s</groupId>%n", groupId)
163                     .format("  <artifactId>%s</artifactId>%n", artifactId)
164                     .format("  <version>%s</version>%n", version);
165 
166             if (!JAR_PACKAGING.equals(packaging)) {
167                 mavenDependency = mavenDependency.format("  <type>%s</type>%n", packaging);
168             }
169 
170             renderDependencyInfo("Apache Maven", mavenDependency.format("</dependency>"));
171         }
172 
173         private void renderIvyDependencyCoordinates() {
174             renderDependencyInfo(
175                     "Apache Ivy",
176                     new Formatter()
177                             .format("<dependency org=\"%s\" name=\"%s\" rev=\"%s\">%n", groupId, artifactId, version)
178                             .format("  <artifact name=\"%s\" type=\"%s\" />%n", artifactId, packaging)
179                             .format("</dependency>"));
180         }
181 
182         private void renderGrapeDependencyCoordinates() {
183             renderDependencyInfo(
184                     "Groovy Grape",
185                     new Formatter()
186                             .format("@Grapes(%n")
187                             .format("@Grab(group='%s', module='%s', version='%s')%n", groupId, artifactId, version)
188                             .format(")"));
189         }
190 
191         private void renderGradleDependencyCoordinates() {
192             renderDependencyInfo(
193                     "Gradle/Grails", new Formatter().format("implementation '%s:%s:%s'", groupId, artifactId, version));
194         }
195 
196         private void renderScalaDependencyCoordinates() {
197             renderDependencyInfo(
198                     "Scala SBT",
199                     new Formatter()
200                             .format("libraryDependencies += \"%s\" %% \"%s\" %% \"%s\"", groupId, artifactId, version));
201         }
202 
203         private void renderLeiningenDependencyCoordinates() {
204             Formatter leiningenDependency = new Formatter().format("[%s", groupId);
205 
206             if (!groupId.equals(artifactId)) {
207                 leiningenDependency.format("/%s", artifactId);
208             }
209 
210             leiningenDependency.format(" \"%s\"]", version);
211 
212             renderDependencyInfo("Leiningen", leiningenDependency);
213         }
214 
215         private void renderDependencyInfo(String name, Formatter formatter) {
216             startSection(name);
217             verbatimText(formatter.toString());
218             endSection();
219         }
220     }
221 }