View Javadoc
1   package org.andromda.maven.plugin.cartridge;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import org.andromda.core.common.ExceptionUtils;
6   import org.andromda.maven.plugin.AndroMDAMojo;
7   import org.apache.commons.io.FilenameUtils;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugin.MojoFailureException;
10  import org.codehaus.plexus.archiver.Archiver;
11  import org.codehaus.plexus.archiver.ArchiverException;
12  
13  /**
14   * Updates the cartridge expected output with the current cartridge output. Invoke it with
15   * <code>mvn andromda-cartridge:update</code> when you are inside the cartridge root directory.
16   *
17   * @goal update
18   * @requiresDependencyResolution test
19   * @description update AndroMDA Cartridge test archive
20   * @author Chad Brandon
21   * @author Peter Friese
22   * @author Bob Fields
23   */
24  public class CartridgeTestUpdaterMojo
25          extends AbstractCartridgeTestMojo
26  {
27      /**
28       * @see org.apache.maven.plugin.Mojo#execute()
29       */
30      public void execute() throws MojoExecutionException, MojoFailureException
31      {
32          try
33          {
34              this.getLog().info("-----------------------------------------------------------------------------");
35              this.getLog().info("  A n d r o M D A   C a r t r i d g e   T e s t   S u i t e   U p d a t e r  ");
36              this.getLog().info("-----------------------------------------------------------------------------");
37  
38              // - add the cartridge test dependencies (any dependencies of the cartridge test plugin)
39              this.changeScopeForTestDependencies();
40  
41              // - first run AndroMDA with the test configuration
42              final AndroMDAMojo andromdaMojo = new AndroMDAMojo();
43              andromdaMojo.setConfigurationUri(this.configurationUri);
44              andromdaMojo.setProject(this.project);
45              andromdaMojo.setSettings(this.settings);
46              andromdaMojo.setPropertyFiles(this.propertyFiles);
47              andromdaMojo.execute();
48  
49              // - pack the expected output archive
50              this.pack(this.actualDirectory, this.expectedOutputArchive);
51  
52          }
53          catch (final Throwable throwable)
54          {
55              if (throwable instanceof MojoExecutionException && !this.testFailureIgnore)
56              {
57                  throw (MojoExecutionException)throwable;
58              }
59              else if (this.testFailureIgnore)
60              {
61                  this.getLog().error("An error occurred while updating cartridge archive '"
62                          + this.project.getArtifactId() + '\'', ExceptionUtils.getRootCause(throwable));
63              }
64              else
65              {
66                  throw new MojoExecutionException("An error occurred while updating cartridge archive '"
67                          + this.project.getArtifactId() + '\'', ExceptionUtils.getRootCause(throwable));
68              }
69          }
70  
71      }
72  
73      /**
74       * Packs the actual directory contents into the expected archive.
75       *
76       * @param location Location where to put the unpacked files.
77       * @param file Archive file to be created.
78       * @throws MojoExecutionException error packing directory
79       */
80      protected void pack(final File location,
81          final File file) throws MojoExecutionException
82      {
83          final String archiveExt = FilenameUtils.getExtension(file.getAbsolutePath()).toLowerCase();
84          try
85          {
86              final Archiver archiver;
87              archiver = this.archiverManager.getArchiver(archiveExt);
88              archiver.setDestFile(file);
89              archiver.addDirectory(location);
90              archiver.createArchive();
91          }
92          catch (Throwable throwable)
93          {
94              if (this.testFailureIgnore)
95              {
96                  this.getLog().error(this.project.getArtifactId() + "Error packing directory: " + location + "to: " + file, throwable);
97              }
98              else if (throwable instanceof IOException || throwable instanceof ArchiverException)
99              {
100                 throw new MojoExecutionException("Error packing directory: " + location + "to: " + file, throwable);
101             }
102         }
103     }
104 
105 }