1
2
3
4
5
6 package gov.nist.secauto.metaschema.maven.plugin;
7
8 import gov.nist.secauto.metaschema.core.model.IModule;
9 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10 import gov.nist.secauto.metaschema.databind.codegen.IProduction;
11 import gov.nist.secauto.metaschema.databind.codegen.JavaGenerator;
12 import gov.nist.secauto.metaschema.databind.codegen.config.DefaultBindingConfiguration;
13
14 import org.apache.maven.plugin.MojoExecutionException;
15 import org.apache.maven.plugins.annotations.LifecyclePhase;
16 import org.apache.maven.plugins.annotations.Mojo;
17 import org.apache.maven.plugins.annotations.Parameter;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26
27 import edu.umd.cs.findbugs.annotations.NonNull;
28
29
30
31
32 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
33 public class GenerateSourcesMojo
34 extends AbstractMetaschemaMojo {
35 private static final String STALE_FILE_NAME = "generateSourcesStaleFile";
36
37
38
39
40 @Parameter
41 protected File[] configs;
42
43
44
45
46
47
48
49
50
51
52
53 @Override
54 protected String getStaleFileName() {
55 return STALE_FILE_NAME;
56 }
57
58
59
60
61
62
63 protected List<File> getConfigs() {
64 List<File> retval;
65 if (configs == null) {
66 retval = Collections.emptyList();
67 } else {
68 retval = Arrays.asList(configs);
69 }
70 return retval;
71 }
72
73
74
75
76
77
78
79
80
81 @Override
82 @NonNull
83 protected List<File> generate(@NonNull Set<IModule> modules) throws MojoExecutionException {
84 DefaultBindingConfiguration bindingConfiguration = new DefaultBindingConfiguration();
85 for (File config : getConfigs()) {
86 try {
87 if (getLog().isInfoEnabled()) {
88 getLog().info("Loading binding configuration: " + config.getPath());
89 }
90 bindingConfiguration.load(config);
91 } catch (IOException ex) {
92 throw new MojoExecutionException(
93 String.format("Unable to load binding configuration from '%s'.", config.getPath()), ex);
94 }
95 }
96
97 IProduction production;
98 try {
99 if (getLog().isInfoEnabled()) {
100 getLog().info("Generating Java classes in: " + getOutputDirectory().getPath());
101 }
102 production = JavaGenerator.generate(
103 modules,
104 ObjectUtils.notNull(getOutputDirectory().toPath()),
105 bindingConfiguration);
106 } catch (IOException ex) {
107 throw new MojoExecutionException("Creation of Java classes failed.", ex);
108 }
109
110
111 try {
112 getMavenProject().addCompileSourceRoot(getOutputDirectory().getCanonicalFile().getPath());
113 } catch (IOException ex) {
114 throw new MojoExecutionException("Unable to add output directory to maven sources.", ex);
115 }
116
117 return ObjectUtils.notNull(production.getGeneratedClasses()
118 .map(gen -> gen.getClassFile().toFile())
119 .collect(Collectors.toUnmodifiableList()));
120 }
121 }