BUG-2179: split logging out of CodeGenerator
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesProcessor.java
index 809353a4d7f8d60ff5b78b9365e72b7d833c4917..0402e8b8a36517b6378e1bb2082a23208adbfe64 100644 (file)
@@ -32,8 +32,10 @@ import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
 import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
 import org.opendaylight.yangtools.yang2sources.plugin.Util.YangsInZipsResult;
+import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
 import org.opendaylight.yangtools.yang2sources.spi.BuildContextAware;
-import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
+import org.opendaylight.yangtools.yang2sources.spi.MavenLogAware;
+import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
 import org.sonatype.plexus.build.incremental.BuildContext;
 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
 
@@ -49,7 +51,7 @@ class YangToSourcesProcessor {
     private final MavenProject project;
     private final boolean inspectDependencies;
     private final BuildContext buildContext;
-    private YangProvider yangProvider;
+    private final YangProvider yangProvider;
 
     @VisibleForTesting
     YangToSourcesProcessor(Log log, File yangFilesRootDir, File[] excludedFiles, List<CodeGeneratorArg> codeGenerators,
@@ -180,26 +182,39 @@ class YangToSourcesProcessor {
 
     static class YangProvider {
 
-
-
         void addYangsToMetaInf(Log log, MavenProject project, File yangFilesRootDir, File[] excludedFiles)
                 throws MojoFailureException {
 
             // copy project's src/main/yang/*.yang to target/generated-sources/yang/META-INF/yang/*.yang
+
             File generatedYangDir = new File(project.getBasedir(), CodeGeneratorArg.YANG_GENERATED_DIR);
+            addYangsToMetaInf(log, project, yangFilesRootDir, excludedFiles, generatedYangDir);
+
+            // Also copy to the actual build output dir if different than "target". When running in
+            // Eclipse this can differ (eg "target-ide").
+
+            File actualGeneratedYangDir = new File(project.getBuild().getDirectory(),
+                    CodeGeneratorArg.YANG_GENERATED_DIR.replace("target" + File.separator, ""));
+            if(!actualGeneratedYangDir.equals(generatedYangDir)) {
+                addYangsToMetaInf(log, project, yangFilesRootDir, excludedFiles, actualGeneratedYangDir);
+            }
+        }
+
+        private void addYangsToMetaInf(Log log, MavenProject project, File yangFilesRootDir,
+                File[] excludedFiles, File generatedYangDir)
+                throws MojoFailureException {
 
             File withMetaInf = new File(generatedYangDir, META_INF_YANG_STRING);
             withMetaInf.mkdirs();
 
             try {
-                Collection<File> files = Util.listFiles(yangFilesRootDir, excludedFiles, null);
+                Collection<File> files = Util.listFiles(yangFilesRootDir, excludedFiles, log);
                 for (File file : files) {
                     org.apache.commons.io.FileUtils.copyFile(file, new File(withMetaInf, file.getName()));
                 }
             } catch (IOException e) {
-                String message = "Unable to list yang files into resource folder";
-                log.warn(message, e);
-                throw new MojoFailureException(message, e);
+                log.warn(String.format("Failed to generate files into root %s", yangFilesRootDir), e);
+                throw new MojoFailureException("Unable to list yang files into resource folder", e);
             }
 
             setResource(generatedYangDir, project);
@@ -254,7 +269,7 @@ class YangToSourcesProcessor {
 
         codeGeneratorCfg.check();
 
-        CodeGenerator g = Util.getInstance(codeGeneratorCfg.getCodeGeneratorClass(), CodeGenerator.class);
+        BasicCodeGenerator g = Util.getInstance(codeGeneratorCfg.getCodeGeneratorClass(), BasicCodeGenerator.class);
         log.info(Util.message("Code generator instantiated from %s", LOG_PREFIX,
                 codeGeneratorCfg.getCodeGeneratorClass()));
 
@@ -271,11 +286,15 @@ class YangToSourcesProcessor {
         log.debug(Util.message("Additional configuration picked up for : %s: %s", LOG_PREFIX,
                 codeGeneratorCfg.getCodeGeneratorClass(), codeGeneratorCfg.getAdditionalConfiguration()));
 
+        if (g instanceof MavenLogAware) {
+            ((MavenLogAware)g).setLog(log);
+        }
         if (g instanceof BuildContextAware) {
             ((BuildContextAware)g).setBuildContext(buildContext);
         }
-        g.setLog(log);
-        g.setMavenProject(project);
+        if (g instanceof MavenProjectAware) {
+            ((MavenProjectAware)g).setMavenProject(project);
+        }
         g.setAdditionalConfig(codeGeneratorCfg.getAdditionalConfiguration());
         File resourceBaseDir = codeGeneratorCfg.getResourceBaseDir(project);