1 package org.apache.torque.generator.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.Set;
32
33 import org.apache.maven.model.Resource;
34 import org.apache.maven.plugin.AbstractMojo;
35 import org.apache.maven.plugin.Mojo;
36 import org.apache.maven.plugin.MojoExecutionException;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.torque.generator.configuration.ConfigurationException;
39 import org.apache.torque.generator.configuration.UnitDescriptor;
40 import org.apache.torque.generator.configuration.UnitDescriptor.Packaging;
41 import org.apache.torque.generator.configuration.controller.Loglevel;
42 import org.apache.torque.generator.configuration.option.MapOptionsConfiguration;
43 import org.apache.torque.generator.configuration.option.OptionsConfiguration;
44 import org.apache.torque.generator.configuration.paths.CustomProjectPaths;
45 import org.apache.torque.generator.configuration.paths.DefaultTorqueGeneratorPaths;
46 import org.apache.torque.generator.configuration.paths.Maven2DirectoryProjectPaths;
47 import org.apache.torque.generator.configuration.paths.Maven2JarProjectPaths;
48 import org.apache.torque.generator.configuration.paths.Maven2ProjectPaths;
49 import org.apache.torque.generator.configuration.paths.ProjectPaths;
50 import org.apache.torque.generator.control.Controller;
51 import org.apache.torque.generator.file.Fileset;
52 import org.apache.torque.generator.source.stream.FileSourceProvider;
53
54
55
56
57
58
59
60
61 public class TorqueGeneratorMojo extends AbstractMojo implements Mojo
62 {
63
64 private enum OutputDirUsage
65 {
66
67
68
69
70 COMPILE("compile"),
71
72
73
74
75 TEST_COMPILE("test-compile"),
76
77
78
79 RESOURCE("resource"),
80
81
82
83 TEST_RESOURCE("test-resource"),
84
85
86
87 NONE("none");
88
89
90 private final String key;
91
92
93
94
95
96
97 private OutputDirUsage(String key)
98 {
99 this.key = key;
100 }
101
102 @Override
103 public String toString()
104 {
105 return key;
106 }
107
108
109
110
111
112
113 public String getKey()
114 {
115 return key;
116 }
117
118 public static OutputDirUsage get(String key)
119 throws MojoExecutionException
120 {
121 for (OutputDirUsage candidate : values())
122 {
123 if (candidate.getKey().equals(key))
124 {
125 return candidate;
126 }
127 }
128 StringBuilder errorMessage = new StringBuilder()
129 .append("targetDirUsage contains illegal value: ")
130 .append(key)
131 .append(". Possible values are :");
132 for (OutputDirUsage targetDirUsage : values())
133 {
134 errorMessage.append(" ").append(targetDirUsage.getKey());
135 }
136 throw new MojoExecutionException(errorMessage.toString());
137 }
138 }
139
140
141
142
143
144
145
146
147 private String packaging;
148
149
150
151
152
153
154
155
156 private File projectRootDir;
157
158
159
160
161
162
163
164 private File configDir;
165
166
167
168
169
170
171
172 private String configPackage;
173
174
175
176
177
178
179 private File sourceDir;
180
181
182
183
184
185
186
187
188
189
190 private Set<String> sourceIncludes;
191
192
193
194
195
196
197
198
199
200
201 private Set<String> sourceExcludes;
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226 private Boolean combineFiles;
227
228
229
230
231
232
233
234
235
236 private File defaultOutputDir;
237
238
239
240
241
242
243
244
245
246
247
248
249 private Map<String, String> outputDirMap = new HashMap<String, String>();
250
251
252
253
254
255
256 private File workDir;
257
258
259
260
261
262
263
264 private String jarFile;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 private String defaultOutputDirUsage;
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 private Map<String, String> outputDirUsageMap
334 = new HashMap<String, String>();
335
336
337
338
339
340
341
342
343 private File overrideConfigDir;
344
345
346
347
348
349
350
351
352 private String overrideConfigPackage;
353
354
355
356
357
358
359
360 private String loglevel;
361
362
363
364
365
366
367
368
369 private Map<String, String> options;
370
371
372
373
374
375
376
377
378
379 private File optionsFile;
380
381
382
383
384
385
386
387
388
389
390
391 private String defaultOutputEncoding;
392
393
394
395
396
397
398
399
400 private MavenProject project;
401
402
403
404
405 public void execute() throws MojoExecutionException
406 {
407 Controller controller = new Controller();
408 List<UnitDescriptor> unitDescriptors = new ArrayList<UnitDescriptor>();
409
410
411 OutputDirUsage defaultOutputDirUsageConverted
412 = OutputDirUsage.get(defaultOutputDirUsage);
413 Map<String, OutputDirUsage> outputDirUsageConvertedMap
414 = new HashMap<String, OutputDirUsage>();
415 for (Map.Entry<String, String> outputDirUsageEntry
416 : outputDirUsageMap.entrySet())
417 {
418 outputDirUsageConvertedMap.put(
419 outputDirUsageEntry.getKey(),
420 OutputDirUsage.get(outputDirUsageEntry.getValue()));
421 }
422
423 UnitDescriptor.Packaging packaging;
424 if ("jar".equals(this.packaging))
425 {
426 packaging = UnitDescriptor.Packaging.JAR;
427 }
428 else if ("directory".equals(this.packaging))
429 {
430 packaging = UnitDescriptor.Packaging.DIRECTORY;
431 }
432 else if ("classpath".equals(this.packaging))
433 {
434 packaging = UnitDescriptor.Packaging.CLASSPATH;
435 }
436 else
437 {
438 throw new IllegalArgumentException(
439 "Unknown packaging " + this.packaging
440 + ", must be jar, directory or classpath");
441 }
442 getLog().debug("Packaging is " + packaging);
443
444 ProjectPaths defaultProjectPaths;
445 if (UnitDescriptor.Packaging.JAR == packaging)
446 {
447 defaultProjectPaths
448 = new Maven2JarProjectPaths(projectRootDir, jarFile);
449 }
450 else if (UnitDescriptor.Packaging.DIRECTORY == packaging)
451 {
452 defaultProjectPaths
453 = new Maven2DirectoryProjectPaths(projectRootDir);
454 }
455 else if (UnitDescriptor.Packaging.CLASSPATH == packaging)
456 {
457 defaultProjectPaths
458 = new Maven2DirectoryProjectPaths(projectRootDir);
459 }
460 else
461 {
462 throw new IllegalStateException("Unknown packaging" + packaging);
463 }
464
465 CustomProjectPaths projectPaths
466 = new CustomProjectPaths(defaultProjectPaths);
467
468 if (UnitDescriptor.Packaging.CLASSPATH == packaging)
469 {
470 if (configPackage == null)
471 {
472 throw new MojoExecutionException(
473 "configPackage must be set for packaging =\"classpath\"");
474 }
475 projectPaths.setConfigurationPackage(configPackage);
476 projectPaths.setConfigurationDir(null);
477 }
478 else
479 {
480 if (configDir != null)
481 {
482 projectPaths.setConfigurationDir(configDir);
483 getLog().debug("Setting config dir to " + configDir.toString());
484 }
485 }
486
487 if (sourceDir != null)
488 {
489 projectPaths.setSourceDir(sourceDir);
490 getLog().debug("Setting source dir to " + sourceDir.toString());
491 }
492
493 FileSourceProvider fileSourceProvider = null;
494 if (sourceIncludes != null || sourceExcludes != null)
495 {
496 Fileset sourceFileset
497 = new Fileset(
498 projectPaths.getDefaultSourcePath(),
499 sourceIncludes,
500 sourceExcludes);
501 getLog().debug("Setting source includes to "
502 + sourceIncludes);
503 getLog().debug("Setting source excludes to "
504 + sourceExcludes);
505 try
506 {
507 fileSourceProvider = new FileSourceProvider(
508 null,
509 sourceFileset,
510 combineFiles);
511 }
512 catch (ConfigurationException e)
513 {
514 throw new MojoExecutionException(
515 "The source provider cannot be instantiated", e);
516 }
517 }
518
519 projectPaths.setOutputDirectory(null, defaultOutputDir);
520 getLog().debug("Setting defaultOutputDir to "
521 + defaultOutputDir.toString());
522 if (outputDirMap != null)
523 {
524 if (outputDirMap.get(Maven2ProjectPaths.MODIFIABLE_OUTPUT_DIR_KEY)
525 == null)
526 {
527 outputDirMap.put(
528 Maven2ProjectPaths.MODIFIABLE_OUTPUT_DIR_KEY,
529 project.getBasedir()
530 + "/"
531 + Maven2ProjectPaths.MODIFIABLE_OUTPUT_DIR);
532 }
533 for (Map.Entry<String, String> outputDirMapEntry
534 : outputDirMap.entrySet())
535 {
536 projectPaths.setOutputDirectory(
537 outputDirMapEntry.getKey(),
538 new File(outputDirMapEntry.getValue()));
539 getLog().debug("Setting output directory with key "
540 + outputDirMapEntry.getKey()
541 + " to "
542 + outputDirMapEntry.getValue());
543 }
544 }
545 if (workDir != null)
546 {
547 projectPaths.setWorkDir(workDir);
548 getLog().debug("Setting workDir to "
549 + workDir.toString());
550 }
551 getLog().debug("ProjectPaths = " + projectPaths);
552
553 OptionsConfiguration optionConfiguration = null;
554 if (options != null || optionsFile != null)
555 {
556 if (options == null)
557 {
558 options = new HashMap<String, String>();
559 }
560 if (optionsFile != null)
561 {
562 Properties optionProperties = new Properties();
563 FileInputStream optionsFileInputStream = null;
564 try
565 {
566 optionsFileInputStream = new FileInputStream(optionsFile);
567 optionProperties.load(optionsFileInputStream);
568 }
569 catch (FileNotFoundException e)
570 {
571 getLog().error(e);
572 throw new MojoExecutionException(e.getMessage());
573 }
574 catch (IOException e)
575 {
576 getLog().error(e);
577 throw new MojoExecutionException(e.getMessage());
578 }
579 finally
580 {
581 if (optionsFileInputStream != null)
582 {
583 try
584 {
585 optionsFileInputStream.close();
586 }
587 catch (IOException e)
588 {
589 getLog().error(e);
590 }
591 }
592 }
593 getLog().debug("loaded options file from "
594 + optionsFile.getAbsolutePath() + ", contents: "
595 + optionProperties);
596 for (Map.Entry<Object, Object> propertiesEntry
597 : optionProperties.entrySet())
598 {
599 if (!options.containsKey(propertiesEntry.getKey()))
600 {
601 options.put(
602 (String) propertiesEntry.getKey(),
603 (String) propertiesEntry.getValue());
604 }
605 }
606 }
607 getLog().debug("options = " + options);
608 optionConfiguration = new MapOptionsConfiguration(options);
609 }
610 Loglevel convertedLoglevel = null;
611 if (this.loglevel != null)
612 {
613 convertedLoglevel = Loglevel.getByKey(loglevel);
614 }
615 String encoding = defaultOutputEncoding;
616 if (encoding == null)
617 {
618 encoding = project.getProperties().getProperty(
619 "project.build.sourceEncoding");
620 }
621 UnitDescriptor unitDescriptor = new UnitDescriptor(
622 packaging,
623 projectPaths,
624 new DefaultTorqueGeneratorPaths());
625 unitDescriptor.setOverrideSourceProvider(fileSourceProvider);
626 unitDescriptor.setOverrideOptions(optionConfiguration);
627 unitDescriptor.setLoglevel(convertedLoglevel);
628 unitDescriptor.setDefaultOutputEncoding(encoding);
629 getLog().debug("unit descriptor created");
630 if (overrideConfigDir != null)
631 {
632 CustomProjectPaths childProjectPaths
633 = new CustomProjectPaths(projectPaths);
634 childProjectPaths.setConfigurationDir(overrideConfigDir);
635
636 UnitDescriptor parentUnitDescriptor = new UnitDescriptor(
637 Packaging.DIRECTORY,
638 childProjectPaths,
639 new DefaultTorqueGeneratorPaths());
640 parentUnitDescriptor.setInheritsFrom(unitDescriptor);
641 parentUnitDescriptor.setOverrideSourceProvider(fileSourceProvider);
642 parentUnitDescriptor.setOverrideOptions(optionConfiguration);
643 parentUnitDescriptor.setLoglevel(convertedLoglevel);
644 parentUnitDescriptor.setDefaultOutputEncoding(encoding);
645 getLog().debug("child unit descriptor created from directory");
646 unitDescriptor = parentUnitDescriptor;
647 }
648 else if (overrideConfigPackage != null)
649 {
650 CustomProjectPaths childProjectPaths
651 = new CustomProjectPaths(projectPaths);
652 childProjectPaths.setConfigurationPackage(overrideConfigPackage);
653
654 UnitDescriptor parentUnitDescriptor = new UnitDescriptor(
655 Packaging.CLASSPATH,
656 childProjectPaths,
657 new DefaultTorqueGeneratorPaths());
658 parentUnitDescriptor.setInheritsFrom(unitDescriptor);
659 parentUnitDescriptor.setOverrideSourceProvider(fileSourceProvider);
660 parentUnitDescriptor.setOverrideOptions(optionConfiguration);
661 parentUnitDescriptor.setLoglevel(convertedLoglevel);
662 parentUnitDescriptor.setDefaultOutputEncoding(encoding);
663 getLog().debug("child unit descriptor created from package");
664 unitDescriptor = parentUnitDescriptor;
665 }
666 unitDescriptors.add(unitDescriptor);
667 try
668 {
669 getLog().debug("Generation started");
670 controller.run(unitDescriptors);
671 getLog().info("Generation successful");
672 }
673 catch (Exception e)
674 {
675 getLog().error(e);
676 throw new MojoExecutionException(e.getMessage());
677 }
678
679 File defaultOutputDirPath = projectPaths.getOutputDirectory(null);
680 if (defaultOutputDirPath.exists())
681 {
682 switch (defaultOutputDirUsageConverted)
683 {
684 case COMPILE:
685 project.addCompileSourceRoot(
686 defaultOutputDirPath.toString());
687 getLog().debug("Added "
688 + defaultOutputDirPath.toString()
689 + " as compile source root");
690 break;
691 case TEST_COMPILE:
692 project.addTestCompileSourceRoot(
693 defaultOutputDirPath.toString());
694 getLog().debug("Added "
695 + defaultOutputDirPath.toString()
696 + " as test compile source root");
697 break;
698 case RESOURCE:
699 Resource resource = new Resource();
700 resource.setDirectory(defaultOutputDirPath.toString());
701 project.addResource(resource);
702 getLog().debug("Added "
703 + defaultOutputDirPath.toString()
704 + " to the project resources");
705 break;
706 case TEST_RESOURCE:
707 resource = new Resource();
708 resource.setDirectory(defaultOutputDirPath.toString());
709 project.addTestResource(resource);
710 getLog().debug("Added "
711 + defaultOutputDirPath.toString()
712 + " to the project test resources");
713 break;
714 case NONE:
715 default:
716 }
717 }
718 else
719 {
720 getLog().info("defaultOutputDirPath "
721 + defaultOutputDirPath.getAbsolutePath()
722 + " does not exist, not applying defaultOutputDirUsage");
723 }
724
725 if (outputDirUsageConvertedMap.get(
726 Maven2ProjectPaths.MODIFIABLE_OUTPUT_DIR_KEY)
727 == null
728 && outputDirMap.get(
729 Maven2ProjectPaths.MODIFIABLE_OUTPUT_DIR_KEY)
730 != null)
731 {
732 outputDirUsageConvertedMap.put(
733 Maven2ProjectPaths.MODIFIABLE_OUTPUT_DIR_KEY,
734 OutputDirUsage.COMPILE);
735 }
736 for (Map.Entry<String, OutputDirUsage> usageEntry
737 : outputDirUsageConvertedMap.entrySet())
738 {
739 String outputDirPath = outputDirMap.get(usageEntry.getKey());
740 if (outputDirPath == null)
741 {
742 getLog().info("outputDirPath set for key "
743 + usageEntry.getKey()
744 + " ignoring this outputDirUsageMap entry");
745 continue;
746 }
747
748 File outputDirFile = new File(outputDirPath);
749 if (!outputDirFile.exists())
750 {
751 getLog().info("outputDirPath "
752 + outputDirFile.getAbsolutePath()
753 + " for outputDirUsageMap with key "
754 + usageEntry.getKey()
755 + " does not exist,"
756 + " ignoring this outputDirUsageMap entry");
757 continue;
758 }
759 switch (usageEntry.getValue())
760 {
761 case COMPILE:
762 project.addCompileSourceRoot(outputDirPath.toString());
763 getLog().debug("Added "
764 + outputDirPath.toString()
765 + " as compile source root");
766 break;
767 case TEST_COMPILE:
768 project.addTestCompileSourceRoot(
769 outputDirPath.toString());
770 getLog().debug("Added "
771 + outputDirPath.toString()
772 + " as test compile source root");
773 break;
774 case RESOURCE:
775 Resource resource = new Resource();
776 resource.setDirectory(outputDirPath.toString());
777 project.addResource(resource);
778 getLog().debug("Added "
779 + outputDirPath.toString()
780 + " to the project resources");
781 break;
782 case TEST_RESOURCE:
783 resource = new Resource();
784 resource.setDirectory(outputDirPath.toString());
785 project.addTestResource(resource);
786 getLog().debug("Added "
787 + outputDirPath.toString()
788 + " to the project test resources");
789 break;
790 case NONE:
791 default:
792 }
793 }
794 }
795
796
797
798
799
800
801 public void setPackaging(String packaging)
802 {
803 this.packaging = packaging;
804 }
805
806
807
808
809
810
811 public void setProjectRootDir(File projectRootDir)
812 {
813 this.projectRootDir = projectRootDir;
814 }
815
816 public void setConfigDir(File configDir)
817 {
818 this.configDir = configDir;
819 }
820
821 public void setConfigPackage(String configPackage)
822 {
823 this.configPackage = configPackage;
824 }
825
826
827
828
829
830
831 public void setDefaultOutputDir(File outputDir)
832 {
833 this.defaultOutputDir = outputDir;
834 }
835
836
837
838
839
840
841
842 public void setOutputDir(String outputDirKey, String outputDir)
843 {
844 this.outputDirMap.put(outputDirKey, outputDir);
845 }
846
847
848
849
850
851
852 public void setJarFile(String jarFile)
853 {
854 this.jarFile = jarFile;
855 }
856
857
858
859
860
861
862 public void setProject(MavenProject project)
863 {
864 this.project = project;
865 }
866
867
868
869
870
871
872 public void setDefaultOutputDirUsage(String defaultOutputDirUsage)
873 {
874 if (defaultOutputDirUsage == null)
875 {
876 throw new NullPointerException(
877 "defaultOutputDirUsage must not be null");
878 }
879 this.defaultOutputDirUsage = defaultOutputDirUsage;
880 }
881
882
883
884
885
886
887
888 public void setOutputDirUsage(
889 String outputDirKey,
890 String outputDirUsage)
891 {
892 if (outputDirKey == null)
893 {
894 throw new NullPointerException(
895 "outputDirKey must not be null");
896 }
897 if (outputDirUsage == null)
898 {
899 throw new NullPointerException(
900 "modifiedFileTargetDirUsage must not be null");
901 }
902 this.outputDirUsageMap.put(outputDirKey, outputDirUsage);
903 }
904
905
906
907
908
909
910 public void setSourceDir(File sourceDir)
911 {
912 this.sourceDir = sourceDir;
913 }
914
915
916
917
918
919
920
921 public void setSourceIncludes(Set<String> sourceIncludes)
922 {
923 this.sourceIncludes = sourceIncludes;
924 }
925
926
927
928
929
930
931
932 public void setSourceExcludes(Set<String> sourceExcludes)
933 {
934 this.sourceExcludes = sourceExcludes;
935 }
936
937
938
939
940
941
942
943
944
945 public void setOverrideConfigDir(File overrideConfigDir)
946 {
947 this.overrideConfigDir = overrideConfigDir;
948 }
949
950
951
952
953
954
955
956
957 public void setLoglevel(String loglevel)
958 {
959 this.loglevel = loglevel;
960 }
961
962
963
964
965
966
967
968
969
970 public void setDefaultOutputEncoding(String defaultOutputEncoding)
971 {
972 this.defaultOutputEncoding = defaultOutputEncoding;
973 }
974
975
976
977
978
979
980
981
982 public void setOptions(Map<String, String> options)
983 {
984 this.options = options;
985 }
986
987
988
989
990
991
992 public void setCombineFiles(Boolean combineFiles)
993 {
994 this.combineFiles = combineFiles;
995 }
996
997
998
999
1000
1001
1002 public void setWorkDir(File workDir)
1003 {
1004 this.workDir = workDir;
1005 }
1006
1007
1008
1009
1010
1011
1012
1013 public void setOptionsFile(File optionsFile)
1014 {
1015 this.optionsFile = optionsFile;
1016 }
1017
1018
1019 }