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.Locale;
24  
25  import org.apache.maven.doxia.sink.Sink;
26  import org.apache.maven.model.DistributionManagement;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.project.MavenProject;
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  import org.codehaus.plexus.util.StringUtils;
34  
35  /**
36   * Generates the Project Distribution Management report.
37   *
38   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
39   * @since 2.3
40   */
41  @Mojo(name = "distribution-management")
42  public class DistributionManagementReport extends AbstractProjectInfoReport {
43  
44      @Inject
45      public DistributionManagementReport(RepositorySystem repositorySystem, I18N i18n, ProjectBuilder projectBuilder) {
46          super(repositorySystem, i18n, projectBuilder);
47      }
48      // ----------------------------------------------------------------------
49      // Public methods
50      // ----------------------------------------------------------------------
51  
52      @Override
53      public boolean canGenerateReport() throws MavenReportException {
54          boolean result = super.canGenerateReport();
55          if (result && skipEmptyReport) {
56              result = getProject().getDistributionManagement() != null;
57          }
58  
59          return result;
60      }
61  
62      @Override
63      public void executeReport(Locale locale) {
64          DistributionManagementRenderer r =
65                  new DistributionManagementRenderer(getSink(), getProject(), getI18N(locale), locale);
66  
67          r.render();
68      }
69  
70      /** {@inheritDoc} */
71      public String getOutputName() {
72          return "distribution-management";
73      }
74  
75      @Override
76      protected String getI18Nsection() {
77          return "distribution-management";
78      }
79  
80      // ----------------------------------------------------------------------
81      // Private
82      // ----------------------------------------------------------------------
83  
84      /**
85       * Internal renderer class
86       */
87      private static class DistributionManagementRenderer extends AbstractProjectInfoRenderer {
88          private final MavenProject project;
89  
90          DistributionManagementRenderer(Sink sink, MavenProject project, I18N i18n, Locale locale) {
91              super(sink, i18n, locale);
92  
93              this.project = project;
94          }
95  
96          @Override
97          protected String getI18Nsection() {
98              return "distribution-management";
99          }
100 
101         @Override
102         protected void renderBody() {
103             DistributionManagement distributionManagement = project.getDistributionManagement();
104             if (distributionManagement == null) {
105                 startSection(getI18nString("overview.title"));
106 
107                 paragraph(getI18nString("nodistributionmanagement"));
108 
109                 endSection();
110 
111                 return;
112             }
113 
114             startSection(getI18nString("overview.title"));
115             paragraph(getI18nString("overview.intro"));
116 
117             if (StringUtils.isNotEmpty(distributionManagement.getDownloadUrl())) {
118                 startSection(getI18nString("downloadURL"));
119                 internalLink(distributionManagement.getDownloadUrl());
120                 endSection();
121             }
122 
123             if (distributionManagement.getRelocation() != null) {
124                 startSection(getI18nString("relocation"));
125                 startTable();
126                 tableHeader(new String[] {getI18nString("field"), getI18nString("value")});
127                 tableRow(new String[] {
128                     getI18nString("relocation.groupid"),
129                     distributionManagement.getRelocation().getGroupId()
130                 });
131                 tableRow(new String[] {
132                     getI18nString("relocation.artifactid"),
133                     distributionManagement.getRelocation().getArtifactId()
134                 });
135                 tableRow(new String[] {
136                     getI18nString("relocation.version"),
137                     distributionManagement.getRelocation().getVersion()
138                 });
139                 tableRow(new String[] {
140                     getI18nString("relocation.message"),
141                     distributionManagement.getRelocation().getMessage()
142                 });
143                 endTable();
144                 endSection();
145             }
146 
147             if (distributionManagement.getRepository() != null
148                     && StringUtils.isNotEmpty(
149                             distributionManagement.getRepository().getUrl())) {
150                 startSection(getI18nString("repository")
151                         + getRepoName(distributionManagement.getRepository().getId()));
152                 internalLink(distributionManagement.getRepository().getUrl());
153                 endSection();
154             }
155 
156             if (distributionManagement.getSnapshotRepository() != null
157                     && StringUtils.isNotEmpty(
158                             distributionManagement.getSnapshotRepository().getUrl())) {
159                 startSection(getI18nString("snapshotRepository")
160                         + getRepoName(
161                                 distributionManagement.getSnapshotRepository().getId()));
162                 internalLink(distributionManagement.getSnapshotRepository().getUrl());
163                 endSection();
164             }
165 
166             if (distributionManagement.getSite() != null
167                     && StringUtils.isNotEmpty(distributionManagement.getSite().getUrl())) {
168                 startSection(getI18nString("site")
169                         + getRepoName(distributionManagement.getSite().getId()));
170                 internalLink(distributionManagement.getSite().getUrl());
171                 endSection();
172             }
173 
174             endSection();
175         }
176 
177         private void internalLink(String url) {
178             if (url == null || url.isEmpty()) {
179                 return;
180             }
181 
182             String urlLowerCase = url.trim().toLowerCase(Locale.ENGLISH);
183             if (urlLowerCase.startsWith("http") || urlLowerCase.startsWith("https") || urlLowerCase.startsWith("ftp")) {
184                 link(url, url);
185             } else {
186                 paragraph(url);
187             }
188         }
189 
190         private String getRepoName(String name) {
191             if (name != null && !name.isEmpty()) {
192                 return " - " + name;
193             }
194 
195             return "";
196         }
197     }
198 }