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.project.MavenProject;
40  
41  import ca.uhn.hl7v2.HL7Exception;
42  import ca.uhn.hl7v2.database.NormativeDatabase;
43  import ca.uhn.hl7v2.sourcegen.EventMapGenerator;
44  import ca.uhn.hl7v2.sourcegen.SourceGenerator;
45  
46  /**
47   * Maven Plugin Mojo for generating HAPI HL7 message/segment/etc source files
48   * 
49   * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
50   * @goal sourcegen
51   * @phase generate-sources
52   * @requiresDependencyResolution runtime
53   * @requiresProject
54   * @inheritedByDefault false
55   */
56  public class SourceGenMojo extends AbstractMojo
57  {
58      private static final Set<String> alreadyMade = new HashSet<>();
59      
60      /**
61       * The maven project.
62       * 
63       * @parameter property="project"
64       * @required
65       * @readonly
66       */
67      private MavenProject project;
68  
69      
70      /**
71       * The target directory for the generated source
72       * 
73       * @parameter
74       * @required
75       */
76      private String targetDirectory;
77  
78      /**
79       * The target directory for the generated resources
80       * 
81       * @parameter
82       * @required
83       */
84      private String targetResourceDirectory;
85  
86      /**
87       * The version for the generated source
88       * 
89       * @parameter
90       */
91      private String version;
92      
93      /**
94       * Should we use the default group names on v2.3.1
95       * 
96       * @parameter
97       */
98      private boolean forceGroupNames;
99      
100     /**
101      * The JDBC URL for the HL7 database
102      * 
103      * @parameter
104      */
105     private String jdbcUrl;
106 
107     /**
108      * The JDBC User for the HL7 database
109      * 
110      * @parameter
111      */
112     private String jdbcUser;
113 
114     /**
115      * The JDBC Password for the HL7 database
116      * 
117      * @parameter
118      */
119     private String jdbcPassword;
120 
121     
122     /**
123      * Should build be skipped
124      *
125      * @parameter
126      */
127     private boolean skip;
128 
129     /**
130      * The package from which to load the templates
131      * 
132      * @parameter default="ca.uhn.hl7v2.sourcegen.templates"
133      */
134     private final String templatePackage = "ca.uhn.hl7v2.sourcegen.templates";
135 
136     /**
137      * Should structures be treated as resources
138      *
139      * @parameter default="false"
140      */
141     private boolean structuresAsResources;
142 
143     /**
144      * {@inheritDoc}
145      */
146     public void execute() throws MojoExecutionException {
147 
148         if (skip) {
149             getLog().warn("Configured to skip");
150         }
151         
152         if (forceGroupNames) {
153         	System.setProperty("force.group", "true");
154         } else {
155         	System.setProperty("force.group", "false");
156         }
157 
158     	if (new File(targetDirectory).exists()) {
159             getLog().warn("Already exists version " + version + ", skipping!");
160     	} else if (!alreadyMade.contains(version)) {
161 
162     		// I haven't entirely figured out why, but Maven runs this plugin 
163     		// several times for each version, which takes forever, so we assume
164     		// that if the directory exists, we don't need to generate again
165             alreadyMade.add(version);
166             
167             if (jdbcUser == null) {
168                 jdbcUser = "";
169             }
170             if (jdbcPassword == null) {
171                 jdbcPassword = "";
172             }
173             
174            System.setProperty(NormativeDatabase.PROP_DATABASE_USER, jdbcUser);
175            System.setProperty(NormativeDatabase.PROP_DATABASE_PASSWORD, jdbcPassword);
176            System.setProperty(NormativeDatabase.PROP_DATABASE_URL, jdbcUrl);
177             
178             try {
179 				EventMapGenerator.generateEventMap(targetResourceDirectory, version);
180 				String targetDir = structuresAsResources ? targetResourceDirectory : targetDirectory;
181                 SourceGenerator.makeAll(targetDir, version, false, templatePackage, "java");
182 			} catch (HL7Exception | IOException | SQLException e) {
183 				throw new MojoExecutionException("Failed to build source ", e);
184 			}
185 
186         } else {
187             getLog().warn("Already made version " + version + ", skipping!");
188         }
189         
190     	if (!structuresAsResources) {
191     	    getLog().info("Adding " + targetDirectory + " to compile source root");
192             project.addCompileSourceRoot(targetDirectory);
193     	}
194     }
195 
196 }