View Javadoc
1   package org.andromda.maven.plugin.cartridge.site;
2   
3   import java.io.File;
4   import java.util.List;
5   import org.andromda.maven.plugin.AndroMDAMojo;
6   import org.apache.commons.lang.exception.ExceptionUtils;
7   import org.apache.maven.plugin.MojoExecutionException;
8   import org.apache.maven.plugin.MojoFailureException;
9   import org.apache.maven.project.MavenProject;
10  import org.apache.maven.settings.Settings;
11  
12  /**
13   * Goal that runs AndroMDA over the howto model to generate the cartridge java source files
14   * which are referenced via the cartridge xhtml docs.  It also unpacks the cartridge related
15   * archive files, such as the howto pictures for the cartridge site documentation,
16   * to target/site/howto locations preparing for deployment.
17   *
18   * @phase site
19   * @goal generate-cartridge-howto-artifacts
20   * @description Goal to run AndroMDA and generate howto source and unpack cartridge
21   * archive files prior to site deployment
22   * @author vancek
23   */
24  public class GenerateCartridgeHowtoArtifactsMojo
25      extends AbstractSiteMojo
26  {
27      /**
28       * Path to the cartridge howto pictures zip source.  This file's path is typically
29       * cartridge/src/site/resource/howto/HowToPictures.zip.
30       *
31       * @parameter expression="${basedir}/src/site/resources/howto/HowToPictures.zip"
32       */
33      private File howtoCartridgePicturesSourcePath;
34  
35      /**
36       * Path to the cartride howto pictures destination extraction directory
37       *
38       * @parameter expression="${basedir}/target/site/howto"
39       */
40      private File howtoCartridgePicturesOutputDirectory;
41  
42      /**
43       * This is the URI to the AndroMDA configuration file.
44       *
45       * @parameter expression="file:${basedir}/conf/howto/andromda.xml"
46       * @required
47       */
48      private String configurationUri;
49  
50      /**
51       * @parameter expression="${project.build.filters}"
52       */
53      private List<String> propertyFiles;
54  
55      /**
56       * The current user system settings for use in Maven. (allows us to pass the user
57       * settings to the AndroMDA configuration).
58       *
59       * @parameter expression="${settings}"
60       * @required
61       * @readonly
62       */
63      private Settings settings;
64  
65      /**
66       * The name of the project injected from pom.xml. Not used.
67       *
68       * @parameter default-value="${project.name}"
69       */
70      @SuppressWarnings("unused")
71      private String projectName;
72  
73      /**
74       * @parameter expression="${project}"
75       * @required
76       * @readonly
77       */
78      protected MavenProject project;
79  
80      /**
81       * The directory where the model generation output history is located
82       * (Modelname file containing a list of files generated by that model).
83       *
84       * @parameter expression="${project.build.directory}/history"
85       */
86      private File modelOutputHistory;
87  
88      /**
89       * @see org.apache.maven.plugin.Mojo#execute()
90       */
91      public void execute()
92          throws MojoExecutionException, MojoFailureException
93      {
94          this.getLog().info("---------------------------------------------------------------------------------------");
95          this.getLog().info("  A n d r o M D A   G e n e r a t e   C a r t r i d g e   H o w T o   A r t i f a c t s");
96          this.getLog().info("---------------------------------------------------------------------------------------");
97  
98          this.unpackHowToPictures();
99          this.generateHowToSource();
100 
101         this.getLog().info("GENERATE CARTRIDGE HOWTO ARTIFACTS SUCCESSFUL");
102     }
103 
104     /**
105      * Unpack the cartridge howto pictures
106      *
107      * @throws MojoExecutionException
108      * @throws MojoFailureException
109      */
110     private void unpackHowToPictures()
111         throws MojoExecutionException, MojoFailureException
112     {
113         try
114         {
115             if (!this.howtoCartridgePicturesSourcePath.exists())
116             {
117                 throw new MojoExecutionException("Cartridge howto pictures source location is invalid");
118             }
119 
120             this.unpack(
121                     this.howtoCartridgePicturesSourcePath,
122                     this.howtoCartridgePicturesOutputDirectory);
123         }
124         catch (final Throwable throwable)
125         {
126             if (throwable instanceof MojoExecutionException)
127             {
128                 throw (MojoExecutionException)throwable;
129             }
130             throw new MojoExecutionException("An error occurred unpacking cartridge howto pictures '" +
131                 this.project.getArtifactId() + '\'',
132                 ExceptionUtils.getRootCause(throwable));
133         }
134     }
135 
136     /**
137      * Generate the howto source by running AndroMDA using the howto specific model
138      * and andromda.xml config.
139      *
140      * @throws MojoExecutionException
141      * @throws MojoFailureException
142      */
143     private void generateHowToSource()
144         throws MojoExecutionException, MojoFailureException
145     {
146         try
147         {
148             final AndroMDAMojo andromdaMojo = new AndroMDAMojo();
149             andromdaMojo.setConfigurationUri(this.configurationUri);
150             andromdaMojo.setProject(this.project);
151             andromdaMojo.setSettings(this.settings);
152             andromdaMojo.setPropertyFiles(this.propertyFiles);
153             andromdaMojo.setModelOutputHistory(this.modelOutputHistory);
154             andromdaMojo.execute();
155         }
156         catch (final Throwable throwable)
157         {
158             if (throwable instanceof MojoExecutionException)
159             {
160                 throw (MojoExecutionException)throwable;
161             }
162             throw new MojoExecutionException(
163                     "An error occurred while running AndroMDA over the cartridge howto model '" +
164                     this.project.getArtifactId() + '\'', ExceptionUtils.getRootCause(throwable));
165         }
166     }
167 }