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