View Javadoc
1   package org.andromda.maven.plugin;
2   
3   import java.io.File;
4   import java.net.URL;
5   import org.andromda.core.AndroMDA;
6   import org.andromda.core.common.ResourceUtils;
7   import org.andromda.core.configuration.Configuration;
8   import org.andromda.core.configuration.Model;
9   import org.andromda.core.configuration.Repository;
10  import org.apache.maven.plugin.MojoExecutionException;
11  
12  /**
13   * A Maven2 plugin to run AndroMDA.
14   *
15   * @author Chad Brandon
16   * @goal run
17   * @phase generate-sources
18   * @requiresDependencyResolution runtime
19   */
20  public class AndroMDAMojo
21      extends AbstractAndroMDAMojo
22  {
23      /**
24       * Whether or not a last modified check should be performed before running AndroMDA again.
25       * Checks files in buildSourceDirectory against configurationUri and referenced model dates.
26       *
27       * @parameter expression="${lastModifiedCheck}"
28       *   default-value=true
29       */
30      private boolean lastModifiedCheck = true;
31  
32      /**
33       * Whether or not processing should be skipped (this is if you just want to force AndroMDA
34       * not to run on your model).
35       *
36       * @parameter expression="${andromda.run.skip}"
37       *   default-value=false
38       */
39      private boolean skipProcessing = false;
40  
41      /**
42       * The directory to which the build source is located (any generated source).
43       *
44       * @parameter expression="${project.build.directory}/src/main/java"
45       */
46      private File buildSourceDirectory;
47  
48      /**
49       * The directory where the model generation output history is located
50       * (Modelname file containing a list of files generated by that model).
51       *
52       * @parameter expression="${project.build.directory}/history"
53       */
54      private File modelOutputHistory;
55  
56      /**
57       * @param lastModifiedCheck the lastModifiedCheck to set
58       */
59      public void setLastModifiedCheck(boolean lastModifiedCheck)
60      {
61          this.lastModifiedCheck = lastModifiedCheck;
62      }
63  
64      /**
65       * @param buildSourceDirectory the buildSourceDirectory to set
66       */
67      public void setBuildSourceDirectory(File buildSourceDirectory)
68      {
69          this.buildSourceDirectory = buildSourceDirectory;
70      }
71  
72      /**
73       * @param modelOutputHistory the modelOutputHistory to set
74       */
75      public void setModelOutputHistory(File modelOutputHistory)
76      {
77          this.modelOutputHistory = modelOutputHistory;
78      }
79  
80      /**
81       * @see org.andromda.maven.plugin.AbstractAndroMDAMojo#execute(org.andromda.core.configuration.Configuration)
82       */
83      public void execute(final Configuration configuration)
84          throws MojoExecutionException
85      {
86          if (getLog().isDebugEnabled())
87          {
88              getLog().debug("lastModifiedCheck="+this.lastModifiedCheck + " skipProcessing="+this.skipProcessing 
89                      + " modelOutputHistory="+this.modelOutputHistory + " allowMultipleRuns="+this.allowMultipleRuns);            
90          }
91          if (!this.skipProcessing)
92          {
93              boolean execute = true;
94              if (this.lastModifiedCheck)
95              {
96                  long date = this.getLastModelConfigDate(configuration);
97                  execute = ResourceUtils.modifiedAfter(date,
98                          this.buildSourceDirectory);
99                  if (getLog().isDebugEnabled())
100                 {
101                     getLog().debug("this.lastModifiedCheck="+this.lastModifiedCheck + " execute="+execute + " date="+date);            
102                 }
103             }
104             if (execute)
105             {
106                 final AndroMDA andromda = AndroMDA.newInstance();
107                 final String historyPath = this.modelOutputHistory!=null ? this.modelOutputHistory.getAbsolutePath() : null;
108                 andromda.run(configuration, lastModifiedCheck,  historyPath);
109                 andromda.shutdown();
110             }
111             else
112             {
113                 this.getLog().info("Files are up-to-date, skipping AndroMDA execution");
114             }
115         }
116         if (this.buildSourceDirectory != null)
117         {
118             this.getProject().addCompileSourceRoot(this.buildSourceDirectory.getPath());
119         }
120     }
121 
122     /**
123      * Find the latest date from the configurationUri (andromda.xml) and all referenced models.
124      * @param configuration The model code generation configuration (model list)
125      */
126     private long getLastModelConfigDate(final Configuration configuration)
127     {
128         final URL configurationUri = ResourceUtils.toURL(this.configurationUri);
129         long date = ResourceUtils.getLastModifiedTime(configurationUri);
130         final Repository[] repositories = configuration.getRepositories();
131         int repositoryCount = repositories.length;
132         for (int ctr = 0; ctr < repositoryCount; ctr++)
133         {
134             final Repository repository = repositories[ctr];
135             if (repository != null)
136             {
137                 final Model[] models = repository.getModels();
138                 final int modelCount = models.length;
139                 for (int ctr2 = 0; ctr2 < modelCount; ctr2++)
140                 {
141                     final Model model = models[ctr2];
142                     long newDate = model.getLastModified();
143                     if (newDate > date)
144                     {
145                         date = newDate;
146                     }
147                 }
148             }
149         }
150         return date;
151     }
152 }