View Javadoc

1   package org.apache.maven.plugin.changes;
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.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
26  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
27  import org.apache.maven.artifact.resolver.ArtifactResolver;
28  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
29  import org.apache.maven.artifact.versioning.VersionRange;
30  import org.apache.maven.doxia.module.xhtml.decoration.render.RenderingContext;
31  import org.apache.maven.doxia.site.decoration.Body;
32  import org.apache.maven.doxia.site.decoration.DecorationModel;
33  import org.apache.maven.doxia.site.decoration.Skin;
34  import org.apache.maven.doxia.siterenderer.Renderer;
35  import org.apache.maven.doxia.siterenderer.RendererException;
36  import org.apache.maven.doxia.siterenderer.SiteRenderingContext;
37  import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.apache.maven.project.MavenProject;
40  import org.apache.maven.reporting.AbstractMavenReport;
41  import org.apache.maven.reporting.MavenReportException;
42  import org.codehaus.plexus.i18n.I18N;
43  
44  import java.io.File;
45  import java.io.FileWriter;
46  import java.io.IOException;
47  import java.io.Writer;
48  import java.util.HashMap;
49  import java.util.Locale;
50  import java.util.Map;
51  
52  /**
53   * Base class with the things that should be in AbstractMavenReport anyway.
54   *
55   * Note: This file was copied from r415312 of AbstractProjectInfoReport in
56   * maven-project-info-reports, as a work-around to MCHANGES-88.
57   *
58   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
59   *
60   */
61  public abstract class AbstractChangesReport
62      extends AbstractMavenReport
63  {
64      /**
65       * Report output directory. Note that this parameter is only relevant if the goal is run from the command line or
66       * from the default build lifecycle. If the goal is run indirectly as part of a site generation, the output
67       * directory configured in the Maven Site Plugin is used instead.
68       * 
69       * @parameter default-value="${project.reporting.outputDirectory}"
70       */
71      private File outputDirectory;
72  
73      /**
74       * Doxia Site Renderer.
75       *
76       * @component
77       */
78      protected Renderer siteRenderer;
79  
80      /**
81       * The Maven Project.
82       *
83       * @parameter expression="${project}"
84       * @required
85       * @readonly
86       */
87      protected MavenProject project;
88  
89      /**
90       * Local Repository.
91       *
92       * @parameter expression="${localRepository}"
93       * @required
94       * @readonly
95       */
96      protected ArtifactRepository localRepository;
97  
98      /**
99       * @component
100      */
101     protected ArtifactResolver resolver;
102 
103     /**
104      * @component
105      */
106     protected ArtifactFactory factory;
107 
108     /**
109      * Internationalization.
110      *
111      * @component
112      */
113     protected I18N i18n;
114 
115     private File getSkinArtifactFile()
116         throws MojoExecutionException
117     {
118         Skin skin = Skin.getDefaultSkin();
119 
120         String version = skin.getVersion();
121         Artifact artifact;
122         try
123         {
124             if ( version == null )
125             {
126                 version = Artifact.RELEASE_VERSION;
127             }
128             VersionRange versionSpec = VersionRange.createFromVersionSpec( version );
129             artifact = factory.createDependencyArtifact( skin.getGroupId(), skin.getArtifactId(), versionSpec, "jar",
130                                                          null, null );
131 
132             resolver.resolve( artifact, project.getRemoteArtifactRepositories(), localRepository );
133         }
134         catch ( InvalidVersionSpecificationException e )
135         {
136             throw new MojoExecutionException( "The skin version '" + version + "' is not valid: " + e.getMessage() );
137         }
138         catch ( ArtifactResolutionException e )
139         {
140             throw new MojoExecutionException( "Unable to find skin", e );
141         }
142         catch ( ArtifactNotFoundException e )
143         {
144             throw new MojoExecutionException( "The skin does not exist: " + e.getMessage() );
145         }
146 
147         return artifact.getFile();
148     }
149 
150     public void execute()
151         throws MojoExecutionException
152     {
153         if ( !canGenerateReport() )
154         {
155             return;
156         }
157 
158         // TODO: push to a helper? Could still be improved by taking more of the site information from the site plugin
159         try
160         {
161             DecorationModel model = new DecorationModel();
162             model.setBody( new Body() );
163             Map attributes = new HashMap();
164             attributes.put( "outputEncoding", "UTF-8" );
165             Locale locale = Locale.getDefault();
166             SiteRenderingContext siteContext = siteRenderer.createContextForSkin( getSkinArtifactFile(), attributes,
167                                                                                   model, getName( locale ), locale );
168 
169             RenderingContext context = new RenderingContext( outputDirectory, getOutputName() + ".html" );
170 
171             SiteRendererSink sink = new SiteRendererSink( context );
172             generate( sink, locale );
173 
174             outputDirectory.mkdirs();
175 
176             Writer writer = new FileWriter( new File( outputDirectory, getOutputName() + ".html" ) );
177 
178             siteRenderer.generateDocument( writer, sink, siteContext );
179 
180             siteRenderer.copyResources( siteContext, new File( project.getBasedir(), "src/site/resources" ),
181                                         outputDirectory );
182         }
183         catch ( RendererException e )
184         {
185             throw new MojoExecutionException(
186                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
187         }
188         catch ( IOException e )
189         {
190             throw new MojoExecutionException(
191                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
192         }
193         catch ( MavenReportException e )
194         {
195             throw new MojoExecutionException(
196                 "An error has occurred in " + getName( Locale.ENGLISH ) + " report generation.", e );
197         }
198     }
199 
200     /**
201      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
202      */
203     protected String getOutputDirectory()
204     {
205         return outputDirectory.getAbsolutePath();
206     }
207 
208     /**
209      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
210      */
211     protected MavenProject getProject()
212     {
213         return project;
214     }
215 
216     /**
217      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
218      */
219     protected Renderer getSiteRenderer()
220     {
221         return siteRenderer;
222     }
223 }