1 package uk.co.flamingpenguin;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.util.Properties;
9
10 import org.apache.maven.plugin.AbstractMojo;
11 import org.apache.maven.plugin.MojoExecutionException;
12 import org.apache.maven.plugin.MojoFailureException;
13 import org.apache.maven.project.MavenProject;
14
15 /**
16 * Goal which generates a Janel configuration
17 *
18 * @goal janel
19 * @phase generate-resources
20 */
21 public class JanelMojo extends AbstractMojo
22 {
23 private static final String LINE_SEPARATOR = System.getProperty("line.separator");
24
25
26 /**
27 * Location that the output files should be placed in
28 *
29 * @parameter expression="${project.build.directory}"
30 * @required
31 */
32 private File outputDirectory;
33
34 /**
35 * The class that contains the main method to execute
36 * @parameter default-value="lib"
37 */
38 private String relativeLibraryDirectory;
39
40 /**
41 * The class that contains the main method to execute
42 *
43 * @parameter
44 * @required
45 */
46 private String mainClass;
47
48 /**
49 * The name of the Janel Output
50 *
51 * @parameter expression="project.getArtifactId()"
52 * @required
53 */
54 private String outputName;
55
56 /**
57 * properties to put in the .lap file
58 *
59 * @parameter
60 */
61 private String[] laplines;
62
63 /**
64 * The Maven project
65 *
66 * @parameter expression="${project}"
67 * @required
68 * @readonly
69 */
70 protected MavenProject project;
71
72
73 /**
74 * {@inheritDoc}
75 */
76 public void execute() throws MojoExecutionException, MojoFailureException
77 {
78 verifyOutputDirectory();
79 final File exeFile = new File(outputDirectory, outputName + ".exe");
80 final File lapFile = new File(outputDirectory, outputName + ".lap");
81
82 outputJanelExe(exeFile);
83
84 final Properties lapProperties = new Properties();
85
86 addLapLines(lapProperties);
87
88 lapProperties.put("janel.main.class", mainClass);
89 lapProperties.put("janel.classpath.jars.dir", relativeLibraryDirectory);
90
91 final StringBuilder lapHeader = new StringBuilder();
92 lapHeader.append(".lap file for ").append(project.getGroupId()).append(".").append(project.getArtifactId()).append(LINE_SEPARATOR);
93
94 writeLapProperties(lapFile, lapProperties, lapHeader);
95 }
96
97 private void addLapLines(final Properties lapProperties) throws MojoFailureException
98 {
99 if(laplines != null)
100 {
101 for (final String line : laplines)
102 {
103 if(!line.contains("="))
104 {
105 throw new MojoFailureException("line must be in the format \"key=value\". invalid property " + line);
106 }
107 lapProperties.put(line.substring(0, line.indexOf("=")), line.substring(line.indexOf("=") + 1));
108 }
109 }
110 }
111
112 private void writeLapProperties(final File lapFile, final Properties lapProperties, final StringBuilder lapHeader) throws MojoExecutionException
113 {
114 final FileOutputStream lapFileOutputStream;
115 try
116 {
117 lapFileOutputStream = new FileOutputStream(lapFile);
118 }
119 catch (final FileNotFoundException e)
120 {
121 throw new MojoExecutionException("unable to open .lap file for writing " + lapFile, e);
122 }
123
124 try
125 {
126 lapProperties.store(lapFileOutputStream, lapHeader.toString());
127 }
128 catch (final IOException e)
129 {
130 throw new MojoExecutionException("unable to write to output .lap file", e);
131 }
132 finally
133 {
134 try
135 {
136 lapFileOutputStream.close();
137 }
138 catch (final IOException e)
139 {
140 throw new MojoExecutionException("unable to close output .lap file", e);
141 }
142 }
143 }
144
145 private void outputJanelExe(final File exeFile) throws MojoExecutionException
146 {
147 getLog().info("creating Janel EXE " + exeFile);
148 final InputStream exeInputStream = getClass().getClassLoader().getResourceAsStream("JanelConsole-2.9.0.exe");
149 try
150 {
151 copyFile(exeInputStream, exeFile);
152 }
153 finally
154 {
155 try
156 {
157 exeInputStream.close();
158 }
159 catch (final IOException e)
160 {
161 throw new MojoExecutionException("unable to close input resource", e);
162 }
163 }
164 }
165
166 private void verifyOutputDirectory() throws MojoExecutionException
167 {
168 if (!outputDirectory.exists())
169 {
170 outputDirectory.mkdirs();
171 }
172 if (!outputDirectory.isDirectory())
173 {
174 throw new MojoExecutionException("OutputDirectory is not a directory " + outputDirectory);
175 }
176 }
177
178 private void copyFile(final InputStream fileInputStream, final File outputFile) throws MojoExecutionException
179 {
180 final FileOutputStream fileOutputStream;
181 try
182 {
183 fileOutputStream = new FileOutputStream(outputFile);
184 }
185 catch (final FileNotFoundException e)
186 {
187 throw new MojoExecutionException("unable to find output file " + outputFile, e);
188 }
189
190 try
191 {
192 final int bufferSize = 1024 * 4;
193 final byte[] buffer = new byte[bufferSize];
194 int bytesRead;
195
196 try
197 {
198 while ((bytesRead = fileInputStream.read(buffer)) >= 0)
199 {
200 fileOutputStream.write(buffer, 0, bytesRead);
201 }
202 }
203 catch (final IOException e)
204 {
205 throw new MojoExecutionException("unable to create output file " + outputFile, e);
206 }
207 }
208 finally
209 {
210 try
211 {
212 fileOutputStream.close();
213 }
214 catch (final IOException e)
215 {
216 throw new MojoExecutionException("unable to close output file " + outputFile, e);
217 }
218 }
219 }
220 }