X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=appc-client%2Fcode-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fappc%2Ftools%2Fgenerator%2Fapi%2FMavenPlugin.java;h=d2617077480a0b7c2955700901a961d4fc6d161b;hb=f2614e766147fd345058f137852eeaf892181fc1;hp=9e160eed2d679b2156031c4ad020c28dbceade54;hpb=9475355553b59df58d6f516e2c9109babae9ffd0;p=appc.git diff --git a/appc-client/code-generator/src/main/java/org/onap/appc/tools/generator/api/MavenPlugin.java b/appc-client/code-generator/src/main/java/org/onap/appc/tools/generator/api/MavenPlugin.java index 9e160eed2..d26170774 100644 --- a/appc-client/code-generator/src/main/java/org/onap/appc/tools/generator/api/MavenPlugin.java +++ b/appc-client/code-generator/src/main/java/org/onap/appc/tools/generator/api/MavenPlugin.java @@ -24,7 +24,6 @@ package org.onap.appc.tools.generator.api; -import org.onap.appc.tools.generator.impl.ModelGenerator; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -32,12 +31,17 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; +import org.onap.appc.tools.generator.impl.ModelGenerator; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.nio.file.Paths; @Mojo( - name = "generate", + name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES ) public class MavenPlugin extends AbstractMojo { @@ -65,20 +69,84 @@ public class MavenPlugin extends AbstractMojo { ModelGenerator generator = new ModelGenerator(); try { trace("\t === Called MavenPlugin on builder <" + contextBuilderClassName +">\n"); - generator.execute(sourceFileName,outputFileName,templateName,contextBuilderClassName,contextConfigFileName); - String workDirectory = getWorkDirectory(outputFileName); + + //the source file may be in the class path + //or on the file system + URL sourceFileURL = lookupURL(sourceFileName); + + //prefix with the project absolute path to the output file + outputFileName = toAbsoluteFile(outputFileName); + String workDirectory = Paths.get(outputFileName).getParent().toString(); + generator.execute(sourceFileURL,outputFileName,templateName,contextBuilderClassName,contextConfigFileName); project.addCompileSourceRoot(workDirectory); } catch (Exception e) { - e.printStackTrace(); - throw new MojoExecutionException(e.getMessage()); + throw new MojoExecutionException(e.getMessage(),e); } } - private String getWorkDirectory(String outputFileName) throws IOException { - String workDirPath = Paths.get(outputFileName.toString()).getParent().toString(); - return workDirPath; + + /** + * Converts the file to absolute path. If the file does not exist prefix the maven project absolute path. + * @param filePath + * @return + */ + private String toAbsoluteFile(String filePath){ + + File file = new File(filePath); + + //if the file already exist just return the absolutePath + if(file.exists()){ + return file.getAbsolutePath(); + } + + + //prefix with the project absolute path to the output file + if(!file.isAbsolute()){ + File projectDir = new File(this.project.getBuild().getDirectory()).getParentFile(); + filePath = projectDir.getAbsolutePath() + "/" + filePath; + } + + return filePath; } + /** + * Tries three lookups + * First try to lookup the file in the classpath. + * else try relative path + * else try prefixing the relative path with the maven project path. + + * @param filePath - A String denoting the source yang file path. + * @return URL - to the source yang file + * @throws MalformedURLException + */ + private URL lookupURL(String filePath) throws IOException { + //check out the class path first + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + URL sourceYangURL = classLoader.getResource(filePath); + + if (sourceYangURL != null) { + return sourceYangURL; + } + + String errorMessage = String.format( + "YANG file <%s> not found in classpath or on the file system." + ,filePath + ); + + //check the file system first + File sourceFile = new File(toAbsoluteFile(filePath)); + if (!sourceFile.exists()) { + throw new FileNotFoundException(errorMessage); + } + try { + sourceYangURL = sourceFile.toURI().toURL(); + } catch (MalformedURLException e) { + throw new IOException(errorMessage,e); + } + return sourceYangURL; + } + + private void trace(String message) { getLog().info(message); }