View Javadoc

1   /*
2    * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.util.report;
17  
18  import java.io.File;
19  
20  import org.apache.maven.artifact.Artifact;
21  import org.apache.maven.artifact.factory.ArtifactFactory;
22  import org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
24  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
25  import org.apache.maven.artifact.resolver.ArtifactResolver;
26  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
27  import org.apache.maven.artifact.versioning.VersionRange;
28  import org.apache.maven.doxia.site.decoration.Skin;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.project.MavenProject;
31  
32  /**
33   * Utilities for Maven for generating reports.
34   *
35   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
36   * @version $Revision:591 $
37   */
38  public final class ReportUtils
39  {
40    // ********************************* Fields *********************************
41  
42    // --- constants ------------------------------------------------------------
43  
44    // --- members --------------------------------------------------------------
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    /**
51     * Utility class pattern.
52     */
53    private ReportUtils()
54    {
55    }
56  
57    // ****************************** Inner Classes *****************************
58  
59    // ********************************* Methods ********************************
60  
61    // --- init -----------------------------------------------------------------
62  
63    // --- get&set --------------------------------------------------------------
64  
65    // --- business -------------------------------------------------------------
66  
67    /**
68     * Returns a file reference to the default skin useful for rendering
69     * standalone run reports.
70     * <p>
71     * Stolen from the changes plugin.
72     * </p>
73     *
74     * @param project the project of the plugin that calls this method.
75     * @param localRepository a reference to the local repository to reference to.
76     * @param resolver to resolve the skin artifact.
77     * @param factory to resolve dependencies.
78     * @return a file reference to the default skin.
79     * @throws MojoExecutionException if the skin artifact cannot be resolved.
80     */
81    public static File getSkinArtifactFile(
82        final MavenProject project,
83        final ArtifactRepository localRepository,
84        final ArtifactResolver resolver,
85        final ArtifactFactory factory) throws MojoExecutionException
86    {
87      final Skin skin = Skin.getDefaultSkin();
88      final String version = determineVersion(skin);
89      try
90      {
91        final VersionRange versionSpec =
92            VersionRange.createFromVersionSpec(version);
93        final Artifact artifact =
94            factory.createDependencyArtifact(skin.getGroupId(), skin
95                .getArtifactId(), versionSpec, "jar", null, null);
96        resolver.resolve(artifact, project.getRemoteArtifactRepositories(),
97            localRepository);
98  
99        return artifact.getFile();
100     }
101     catch (InvalidVersionSpecificationException e)
102     {
103       throw new MojoExecutionException("The skin version '" + version
104           + "' is not valid: " + e.getMessage(), e);
105     }
106     catch (ArtifactResolutionException e)
107     {
108       throw new MojoExecutionException("Unable to find skin", e);
109     }
110     catch (ArtifactNotFoundException e)
111     {
112       throw new MojoExecutionException("The skin does not exist: "
113           + e.getMessage(), e);
114     }
115   }
116 
117   /**
118    * Determines the version of the given skin. If the version is not set in the
119    * skin, the {@link Artifact#RELEASE_VERSION} is returned.
120    *
121    * @param skin the skin whose version is requested.
122    * @return the version of the skin or {@link Artifact#RELEASE_VERSION} as
123    *         default.
124    */
125   private static String determineVersion(final Skin skin)
126   {
127     String version = skin.getVersion();
128     if (version == null)
129     {
130       version = Artifact.RELEASE_VERSION;
131     }
132     return version;
133   }
134 
135   // --- object basics --------------------------------------------------------
136 
137 }