f031dd666da6c984abcecad92f90222ba08e3815
[appc.git] / appc-client / code-generator / src / main / java / org / openecomp / appc / tools / generator / api / MavenPlugin.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.tools.generator.api;
26
27 import org.openecomp.appc.tools.generator.impl.ModelGenerator;
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.plugins.annotations.LifecyclePhase;
32 import org.apache.maven.plugins.annotations.Mojo;
33 import org.apache.maven.plugins.annotations.Parameter;
34 import org.apache.maven.project.MavenProject;
35
36 import java.io.IOException;
37 import java.nio.file.Paths;
38
39 @Mojo(
40         name = "generate",
41         defaultPhase = LifecyclePhase.GENERATE_SOURCES
42 )
43 public class MavenPlugin extends AbstractMojo {
44
45     @Parameter(property = "templateName", required = true)
46     private String templateName;
47
48     @Parameter(property = "sourceFileName")
49     private String sourceFileName;
50
51     @Parameter(property = "outputFileName")
52     private String outputFileName;
53
54     @Parameter(property = "contextBuilderClassName", required = true)
55     private String contextBuilderClassName;
56
57     @Parameter(property = "contextConfigFileName")
58     private String contextConfigFileName;
59
60     @Parameter (property = "project")
61     private MavenProject project;
62
63     @Override
64     public void execute() throws MojoExecutionException, MojoFailureException {
65         ModelGenerator generator = new ModelGenerator();
66         try {
67             trace("\t === Called MavenPlugin on builder <" + contextBuilderClassName +">\n");
68             generator.execute(sourceFileName,outputFileName,templateName,contextBuilderClassName,contextConfigFileName);
69             String workDirectory = getWorkDirectory(outputFileName);
70             project.addCompileSourceRoot(workDirectory);
71         } catch (Exception e) {
72             e.printStackTrace();
73             throw new MojoExecutionException(e.getMessage());
74         }
75     }
76
77     private String getWorkDirectory(String outputFileName) throws IOException {
78         String workDirPath = Paths.get(outputFileName.toString()).getParent().toString();
79         return workDirPath;
80     }
81
82     private void trace(String message) {
83         getLog().info(message);
84     }
85 }