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