View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.mojo;
12  
13  import java.util.LinkedHashSet;
14  
15  import org.apache.maven.plugin.AbstractMojo;
16  import org.apache.maven.plugin.MojoExecutionException;
17  import org.apache.maven.plugin.MojoFailureException;
18  import org.apache.maven.project.MavenProject;
19  import org.mod4j.dslcommon.io.DirectoryWalker;
20  import org.mod4j.dslcommon.openarchitectureware.CodegenDirectoryVisitor;
21  import org.mod4j.dslcommon.openarchitectureware.CrossxDirectoryVisitor;
22  import org.mod4j.dslcommon.openarchitectureware.DslExtension;
23  import org.mod4j.dslcommon.openarchitectureware.Mod4jWorkflowException;
24  import org.mod4j.dslcommon.openarchitectureware.OutletDirectoryCleaner;
25  
26  /**
27   * The Mod4j Maven plug-in is used for generating artifacts out of Mod4j model projects. <br/>
28   * 
29   * @goal generateFromModel
30   * @description Executes the process for generating artifacts out of Mod4j DSL model projects.
31   */
32  public class Mod4jGeneratorMojo extends AbstractMojo {
33  
34      /**
35       * The current Maven project we are working with.
36       * 
37       * @parameter expression="${project}"
38       * @required
39       */
40      private MavenProject project;
41  
42      /**
43       * The model dir to process.
44       * 
45       * @parameter default-value="src/model"
46       */
47      private final String modelDir = "src/model";
48      
49      /**
50       * The filename of the generator property file within the modelDir. 
51       * 
52       * @parameter default-value="mod4j.properties"
53       */
54      private final String generatorPropertiesFileName = "mod4j.properties";
55  
56      /**
57       * The list (fifo) with known DSL extensions to process.
58       * 
59       * @parameter
60       */
61      private LinkedHashSet<DslExtension> dslExtensions = new LinkedHashSet<DslExtension>();
62  
63      /**
64       * Indicator for default generation processing.
65       * 
66       * @parameter default-value=true
67       */
68      private boolean defaultGeneration;
69  
70      /*
71       * (non-Javadoc).
72       * 
73       * @see org.apache.maven.plugin.AbstractMojo#execute()
74       */
75      public void execute() throws MojoExecutionException, MojoFailureException {
76  
77          getLog().info("Executing Mod4J Maven plug-in, goal: genrateFromModel");
78  
79          
80          if (defaultGeneration) {
81              addDefaultDslExtensions();
82          }
83          
84          String dir = project.getBasedir().getAbsolutePath();
85  
86          // Run the outlet directory cleaner to clean previously generated sources. 
87          OutletDirectoryCleaner directoryCleaner = new OutletDirectoryCleaner();
88          try {
89              directoryCleaner.clean(dir, dir + "/" + modelDir + "/" + generatorPropertiesFileName);
90          } catch (Mod4jWorkflowException mwfe) {
91              throw new MojoFailureException("ERROR while cleaning outlet directories :" + mwfe.getMessage());
92          }
93          
94          for (DslExtension dslExt : dslExtensions) {
95  
96              try {
97                  getLog().info("Processing DSL : " + dslExt.getDslName());
98                  crossxDslModel(dir, dslExt);
99              } catch (Mod4jWorkflowException we) {
100                 throw new MojoFailureException("Workflow ERROR while processing the " + dslExt.getDslName() + " at:"
101                         + dir);
102             } catch (Exception e) {
103                 throw new MojoFailureException("ERROR while processing " + dslExt.getDslName() + " :" + e.getMessage());
104             }
105         }
106 
107         for (DslExtension dslExt : dslExtensions) {
108 
109             try {
110                 getLog().info("Processing DSL : " + dslExt.getDslName());
111                 processDslModel(dir, dslExt);
112             } catch (Mod4jWorkflowException we) {
113                 throw new MojoFailureException("Workflow ERROR while processing the " + dslExt.getDslName() + " at:"
114                         + dir);
115             } catch (Exception e) {
116                 throw new MojoFailureException("ERROR while processing " + dslExt.getDslName() + " :" + e.getMessage());
117             }
118         }
119     }
120 
121     /**
122      * Add all default extensions to the dslExtension list. The default extensions are: <br>
123      * 1- BusinesDomainDsl (Mod4j) <br>
124      * 2- DataContractDsl (Mod4j) <br>
125      * 3- ServiceDsl (Mod4j) <br>
126      */
127     private void addDefaultDslExtensions() {
128 
129         dslExtensions.add(new DslExtension("Mod4j", "BusinessDomainDsl",
130                 "BusinessDomainDsl.BusinessDomainDslPackage", ".busmod",
131                 "crossx/busmod2crossx.oaw",
132                 "codegen/BusinessDomainDsl.oaw", generatorPropertiesFileName));
133 
134         dslExtensions.add(new DslExtension("Mod4j", "DataContractDsl",
135                 "org.mod4j.dsl.datacontract.mm.DataContractDsl.DataContractDslPackage", ".dtcmod",
136                 "org/mod4j/dsl/datacontract/generator/workflow/dtcmod2crossx.oaw",
137                 "org/mod4j/dsl/datacontract/generator/workflow/DataContractDsl.oaw", generatorPropertiesFileName));
138 
139         dslExtensions.add(new DslExtension("Mod4j", "ServiceDsl",
140                 "org.mod4j.dsl.service.mm.ServiceDsl.ServiceDslPackage", ".sermod", 
141                 "org/mod4j/dsl/service/generator/workflow/sermod2crossx.oaw",
142                 "org/mod4j/dsl/service/generator/workflow/ServiceDsl.oaw", generatorPropertiesFileName));
143         }
144 
145     /**
146      * Method for processing DslExtensions within a project. The following steps will be processed:<br> 1) Walk through
147      * all Mod4j model files for the given <b>DslExtension</b> within the model project and extract reference
148      * information (Crossx) from them. 
149      * 
150      * @param projectDir
151      * @param DslExtension
152      * @throws Exception
153      */
154     public void crossxDslModel(final String projectDir, final DslExtension dsl) throws Exception {
155 
156         DirectoryWalker walker = new DirectoryWalker();
157         CrossxDirectoryVisitor vis = new CrossxDirectoryVisitor(dsl, projectDir, true);
158         walker.walk(projectDir + "/" + modelDir, vis);
159     }
160 
161     /**
162      * Method for processing DslExtensions within a project. The following steps will be processed:<br> 
163      * 2) Run all workflow files in the project, which checks consistency
164      * of the models and generate the code. <br>
165      * 
166      * Note that the crossxDslModel operation should run first to make all the cross reference information available.
167      * @param projectDir
168      * @param DslExtension
169      * @throws Exception
170      */
171     public void processDslModel(final String projectDir, final DslExtension dsl) throws Exception {
172 
173         DirectoryWalker walker = new DirectoryWalker();
174         CodegenDirectoryVisitor codegen = new CodegenDirectoryVisitor(dsl, projectDir, true);
175         walker.walk(projectDir + "/" + modelDir, codegen);
176     }
177 
178 }