1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.synchronoss.cpo.plugin;
22
23 import org.apache.maven.plugin.*;
24 import org.apache.maven.plugins.annotations.*;
25 import org.apache.maven.plugins.annotations.Mojo;
26 import org.apache.maven.project.MavenProject;
27 import org.synchronoss.cpo.exporter.*;
28 import org.synchronoss.cpo.meta.CpoMetaDescriptor;
29 import org.synchronoss.cpo.meta.domain.CpoClass;
30
31 import java.io.*;
32 import java.util.StringTokenizer;
33
34
35
36
37 @Mojo (name = "generatejavasource",
38 requiresDependencyResolution = ResolutionScope.RUNTIME,
39 configurator = "include-project-dependencies",
40 defaultPhase = LifecyclePhase.GENERATE_SOURCES)
41 public class GenerateJavaSources extends AbstractMojo {
42
43 private enum Scopes {
44 test
45 }
46
47 @Parameter (property = "cpoConfig", required = true)
48 private String cpoConfig;
49
50
51
52
53 @Parameter (property = "outputDir", required = true, defaultValue = "${project.build.directory}/generated-sources/cpo")
54 private String outputDir;
55
56
57
58
59 @Parameter (property = "testOutputDir", required = true, defaultValue = "${project.build.directory}/generated-test-sources/cpo")
60 private String testOutputDir;
61
62 @Parameter (property = "scope", required = true, defaultValue = "compile")
63 private String scope;
64
65 @Parameter (property = "filter", defaultValue = ".*")
66 private String filter;
67
68 @Parameter (property = "generateInterface", defaultValue = "false")
69 private boolean generateInterface = false;
70
71 @Parameter (property = "generateClass", defaultValue = "true")
72 private boolean generateClass = true;
73
74
75
76
77 @Component
78 protected MavenProject project;
79
80 private final String JAVA_EXT = ".java";
81 private final String META_DESCRIPTOR_NAME = "Generator-" + System.currentTimeMillis();
82
83 public void execute() throws MojoExecutionException {
84 getLog().info("Cpo config: " + cpoConfig);
85
86 if (!generateInterface && !generateClass) {
87 throw new MojoExecutionException("You must generate interfaces, classes or both");
88 }
89
90 File srcDir;
91
92 if (Scopes.test.toString().equals(scope)) {
93
94 srcDir = new File(testOutputDir);
95 project.addTestCompileSourceRoot(srcDir.getAbsolutePath());
96 getLog().debug("Adding " + srcDir.getAbsolutePath() + " to the project's test compile sources.");
97 } else {
98
99 srcDir = new File(outputDir);
100 project.addCompileSourceRoot(srcDir.getAbsolutePath());
101 getLog().debug("Adding " + srcDir.getAbsolutePath() + " to the project's compile sources.");
102 }
103
104 getLog().info("Generating cpo java sources to " + srcDir);
105
106 File outputDirectory = new File(project.getBuild().getOutputDirectory());
107 if (!outputDirectory.exists()) {
108 if (!outputDirectory.mkdirs()) {
109 throw new MojoExecutionException("Unable to create output directory: " + outputDirectory.getAbsolutePath());
110 }
111 }
112
113 try {
114 CpoMetaDescriptor metaDescriptor = CpoMetaDescriptor.getInstance(META_DESCRIPTOR_NAME, cpoConfig, true);
115
116 for (CpoClass cpoClass : metaDescriptor.getCpoClasses()) {
117
118 String className = cpoClass.getName();
119
120
121 if (filter != null && className.matches(filter)) {
122 File classDir = srcDir;
123 if (className.lastIndexOf(".") != -1) {
124 String packageName = className.substring(0, className.lastIndexOf("."));
125 StringTokenizer tok = new StringTokenizer(packageName, ".");
126 while (tok.hasMoreTokens()) {
127 String dirName = tok.nextToken();
128 classDir = new File(classDir, dirName);
129 }
130 }
131
132 if (!classDir.exists()) {
133 if (!classDir.mkdirs()) {
134 throw new MojoExecutionException("Unable to create class directories: " + classDir.getAbsolutePath());
135 }
136 }
137
138 if (generateInterface) {
139 CpoInterfaceSourceGenerator interfaceSourceGenerator = new CpoInterfaceSourceGenerator(metaDescriptor);
140 cpoClass.acceptMetaDFVisitor(interfaceSourceGenerator);
141
142 File interfaceFile = new File(classDir, interfaceSourceGenerator.getInterfaceName() + JAVA_EXT);
143 getLog().info("cpo-plugin generated " + interfaceFile.getAbsolutePath());
144
145 FileWriter iw = new FileWriter(interfaceFile);
146 iw.write(interfaceSourceGenerator.getSourceCode());
147 iw.flush();
148 iw.close();
149 }
150
151 if (generateClass) {
152 CpoClassSourceGenerator classSourceGenerator;
153 if (generateInterface) {
154 classSourceGenerator = new CpoClassSourceGenerator(metaDescriptor);
155 } else {
156 classSourceGenerator = new CpoLegacyClassSourceGenerator(metaDescriptor);
157 }
158 cpoClass.acceptMetaDFVisitor(classSourceGenerator);
159
160 File javaFile = new File(classDir, classSourceGenerator.getClassName() + JAVA_EXT);
161 getLog().info("cpo-plugin generated " + javaFile.getAbsolutePath());
162
163 FileWriter cw = new FileWriter(javaFile);
164 cw.write(classSourceGenerator.getSourceCode());
165 cw.flush();
166 cw.close();
167 }
168 }
169 }
170 } catch (Exception ex) {
171 throw new MojoExecutionException("Exception caught", ex);
172 }
173 }
174 }
175