Merge "Improve TypeProviderImpl.sortTypeDefinitionAccordingDepth()"
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesProcessor.java
index 7a49d58e09df3810fee9c951e0f389529d3dcc54..d3de2502a121179c927c2b29ffb4c1a04f0c8a4e 100644 (file)
@@ -41,7 +41,6 @@ class YangToSourcesProcessor {
     static final String LOG_PREFIX = "yang-to-sources:";
     static final String META_INF_YANG_STRING = "META-INF" + File.separator + "yang";
     static final String META_INF_YANG_STRING_JAR = "META-INF" + "/" + "yang";
-    static final File META_INF_YANG_DIR = new File(META_INF_YANG_STRING);
 
     private final Log log;
     private final File yangFilesRootDir;
@@ -50,7 +49,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,
@@ -181,35 +180,50 @@ class YangToSourcesProcessor {
 
     static class YangProvider {
 
-        private static final String YANG_RESOURCE_DIR = "target" + File.separator + "yang";
-
         void addYangsToMetaInf(Log log, MavenProject project, File yangFilesRootDir, File[] excludedFiles)
                 throws MojoFailureException {
-            File targetYangDir = new File(project.getBasedir(), YANG_RESOURCE_DIR);
+
+            // 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);
                 for (File file : files) {
-                    org.apache.commons.io.FileUtils.copyFile(file, new File(targetYangDir, file.getName()));
+                    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(targetYangDir, META_INF_YANG_STRING_JAR, project);
+            setResource(generatedYangDir, project);
 
             log.debug(Util.message("Yang files from: %s marked as resources: %s", LOG_PREFIX, yangFilesRootDir,
                     META_INF_YANG_STRING_JAR));
         }
 
-        private static void setResource(File targetYangDir, String targetPath, MavenProject project) {
+        private static void setResource(File targetYangDir, MavenProject project) {
             Resource res = new Resource();
             res.setDirectory(targetYangDir.getPath());
-            if (targetPath != null) {
-                res.setTargetPath(targetPath);
-            }
             project.addResource(res);
         }
     }
@@ -278,7 +292,7 @@ class YangToSourcesProcessor {
         g.setAdditionalConfig(codeGeneratorCfg.getAdditionalConfiguration());
         File resourceBaseDir = codeGeneratorCfg.getResourceBaseDir(project);
 
-        YangProvider.setResource(resourceBaseDir, null, project);
+        YangProvider.setResource(resourceBaseDir, project);
         g.setResourceBaseDir(resourceBaseDir);
         log.debug(Util.message("Folder: %s marked as resources for generator: %s", LOG_PREFIX, resourceBaseDir,
                 codeGeneratorCfg.getCodeGeneratorClass()));