forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerationMojo.java
More file actions
117 lines (96 loc) · 4.6 KB
/
GenerationMojo.java
File metadata and controls
117 lines (96 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights
* Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.codegen.maven.plugin;
import com.amazonaws.codegen.C2jModels;
import com.amazonaws.codegen.CodeGenerator;
import com.amazonaws.codegen.internal.Utils;
import com.amazonaws.codegen.utils.ModelLoaderUtils;
import com.amazonaws.codegen.model.config.BasicCodeGenConfig;
import com.amazonaws.codegen.model.config.customization.CustomizationConfig;
import com.amazonaws.codegen.model.intermediate.ServiceExamples;
import com.amazonaws.codegen.model.service.ServiceModel;
import com.amazonaws.codegen.model.service.Waiters;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import java.io.File;
import java.util.Optional;
/**
* The Maven mojo to generate Java client code using aws-java-sdk-code-generator.
*/
@Mojo(name = "generate")
public class GenerationMojo extends AbstractMojo {
@Parameter(property = "modelFile", defaultValue = "code-gen/service-2.json")
private String serviceModelLocation;
@Parameter(property = "codeGenConfigFile", defaultValue = "code-gen/codegen.config")
private String codeGenConfigLocation;
@Parameter(property = "customizationConfigFile", defaultValue = "code-gen/customization.config")
private String customizationConfigLocation;
@Parameter(property = "examplesFile", defaultValue = "code-gen/examples-1.json")
private String serviceExamplesLocation;
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/generated-src")
private String outputDirectory;
@Parameter(property = "resourcesDirectory", defaultValue = "${basedir}/src/main/resources")
private String resourcesDirectory;
@Component
private MavenProject project;
public void execute() throws MojoExecutionException {
generateCode(C2jModels.builder()
.codeGenConfig(loadCodeGenConfig())
.customizationConfig(loadCustomizationConfig())
.serviceModel(loadServiceModel())
// Maven plugin doesn't support Waiters
.waitersModel(Waiters.NONE)
.examplesModel(loadExamplesModel())
.build());
project.addCompileSourceRoot(outputDirectory);
}
private void generateCode(C2jModels models) {
new CodeGenerator(models, outputDirectory, resourcesDirectory, Utils.getFileNamePrefix(models.serviceModel(), models.customizationConfig())).execute();
}
private BasicCodeGenConfig loadCodeGenConfig() throws MojoExecutionException {
return loadRequiredModel(BasicCodeGenConfig.class, codeGenConfigLocation);
}
private CustomizationConfig loadCustomizationConfig() {
return loadOptionalModel(CustomizationConfig.class, customizationConfigLocation).orElse(CustomizationConfig.DEFAULT);
}
private ServiceModel loadServiceModel() throws MojoExecutionException {
return loadRequiredModel(ServiceModel.class, serviceModelLocation);
}
private ServiceExamples loadExamplesModel() {
return loadOptionalModel(ServiceExamples.class, serviceExamplesLocation).orElse(ServiceExamples.NONE);
}
/**
* Load required model from the project resources.
*/
private <T> T loadRequiredModel(Class<T> clzz, String location) throws MojoExecutionException {
return ModelLoaderUtils.loadModel(clzz, getResourceLocation(location));
}
/**
* Load an optional model from the project resources.
*
* @return Model or empty optional if not present.
*/
private <T> Optional<T> loadOptionalModel(Class<T> clzz, String location) {
return ModelLoaderUtils.loadOptionalModel(clzz, getResourceLocation(location));
}
private File getResourceLocation(String location) {
return new File(resourcesDirectory, location);
}
}