View Javadoc

1   /**
2   The contents of this file are subject to the Mozilla Public License Version 1.1 
3   (the "License"); you may not use this file except in compliance with the License. 
4   You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
5   Software distributed under the License is distributed on an "AS IS" basis, 
6   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
7   specific language governing rights and limitations under the License. 
8   
9   The Original Code is "DataTypeGenerator.java".  Description: 
10  "Generates skeletal source code for Datatype classes based on the 
11    HL7 database" 
12  
13  The Initial Developer of the Original Code is University Health Network. Copyright (C) 
14  2001.  All Rights Reserved. 
15  
16  Contributor(s):  James Agnew 
17  
18  Alternatively, the contents of this file may be used under the terms of the 
19  GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
20  applicable instead of those above.  If you wish to allow use of your version of this 
21  file only under the terms of the GPL and not to allow others to use your version 
22  of this file under the MPL, indicate your decision by deleting  the provisions above 
23  and replace  them with the notice and other provisions required by the GPL License.  
24  If you do not delete the provisions above, a recipient may use your version of 
25  this file under either the MPL or the GPL. 
26  
27  */
28  
29  package ca.uhn.hl7v2.mvnplugin;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.sql.SQLException;
34  import java.util.HashSet;
35  import java.util.Set;
36  
37  import org.apache.maven.plugin.AbstractMojo;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.apache.maven.plugin.MojoFailureException;
40  import org.apache.maven.project.MavenProject;
41  
42  import ca.uhn.hl7v2.HL7Exception;
43  import ca.uhn.hl7v2.database.NormativeDatabase;
44  import ca.uhn.hl7v2.sourcegen.EventMapGenerator;
45  import ca.uhn.hl7v2.sourcegen.SourceGenerator;
46  
47  /**
48   * Maven Plugin Mojo for generating HAPI HL7 message/segment/etc source files
49   * 
50   * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
51   * @goal sourcegen
52   * @phase generate-sources
53   * @requiresDependencyResolution runtime
54   * @requiresProject
55   * @inheritedByDefault false
56   */
57  public class SourceGenMojo extends AbstractMojo
58  {
59      private static final Set<String> alreadyMade = new HashSet<String>();
60      
61      /**
62       * The maven project.
63       * 
64       * @parameter property="project"
65       * @required
66       * @readonly
67       */
68      private MavenProject project;
69  
70      
71      /**
72       * The target directory for the generated source
73       * 
74       * @parameter
75       * @required
76       */
77      private String targetDirectory;
78  
79      /**
80       * The target directory for the generated resources
81       * 
82       * @parameter
83       * @required
84       */
85      private String targetResourceDirectory;
86  
87      /**
88       * The version for the generated source
89       * 
90       * @parameter
91       */
92      private String version;
93      
94      /**
95       * Should we use the default group names on v2.3.1
96       * 
97       * @parameter
98       */
99      private boolean forceGroupNames;
100     
101     /**
102      * The JDBC URL for the HL7 database
103      * 
104      * @parameter
105      */
106     private String jdbcUrl;
107 
108     /**
109      * The JDBC User for the HL7 database
110      * 
111      * @parameter
112      */
113     private String jdbcUser;
114 
115     /**
116      * The JDBC Password for the HL7 database
117      * 
118      * @parameter
119      */
120     private String jdbcPassword;
121 
122     
123     /**
124      * Should build be skipped
125      *
126      * @parameter
127      */
128     private boolean skip;
129 
130     /**
131      * The package from which to load the templates
132      * 
133      * @parameter default="ca.uhn.hl7v2.sourcegen.templates"
134      */
135     private String templatePackage = "ca.uhn.hl7v2.sourcegen.templates";
136 
137     /**
138      * Should structures be treated as resources
139      *
140      * @parameter default="false"
141      */
142     private boolean structuresAsResources;
143 
144     /**
145      * {@inheritDoc}
146      */
147     public void execute() throws MojoExecutionException, MojoFailureException {
148 
149         if (skip) {
150             getLog().warn("Configured to skip");
151         }
152         
153         if (forceGroupNames) {
154         	System.setProperty("force.group", "true");
155         } else {
156         	System.setProperty("force.group", "false");
157         }
158 
159     	if (new File(targetDirectory).exists()) {
160             getLog().warn("Already exists version " + version + ", skipping!");
161     	} else if (!alreadyMade.contains(version)) {
162 
163     		// I haven't entirely figured out why, but Maven runs this plugin 
164     		// several times for each version, which takes forever, so we assume
165     		// that if the directory exists, we don't need to generate again
166             alreadyMade.add(version);
167             
168             if (jdbcUser == null) {
169                 jdbcUser = "";
170             }
171             if (jdbcPassword == null) {
172                 jdbcPassword = "";
173             }
174             
175            System.setProperty(NormativeDatabase.PROP_DATABASE_USER, jdbcUser);
176            System.setProperty(NormativeDatabase.PROP_DATABASE_PASSWORD, jdbcPassword);
177            System.setProperty(NormativeDatabase.PROP_DATABASE_URL, jdbcUrl);
178             
179             try {
180 				EventMapGenerator.generateEventMap(targetResourceDirectory, version);
181 				String targetDir = structuresAsResources ? targetResourceDirectory : targetDirectory;
182                 SourceGenerator.makeAll(targetDir, version, false, templatePackage, "java");
183 			} catch (HL7Exception e) {
184 				throw new MojoExecutionException("Failed to build source ", e);
185 			} catch (SQLException e) {
186 				throw new MojoExecutionException("Failed to build source ", e);
187 			} catch (IOException e) {
188 				throw new MojoExecutionException("Failed to build source ", e);
189 			}
190             
191         } else {
192             getLog().warn("Already made version " + version + ", skipping!");
193         }
194         
195     	if (!structuresAsResources) {
196     	    getLog().info("Adding " + targetDirectory + " to compile source root");
197             project.addCompileSourceRoot(targetDirectory);
198     	}
199     }
200 
201 }