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