Bug 7182 related: yang-maven-plugin no hard-coded target/ (target-ide/) 40/52440/9
authorMichael Vorburger <vorburger@redhat.com>
Wed, 1 Mar 2017 01:12:57 +0000 (02:12 +0100)
committerRobert Varga <nite@hq.sk>
Thu, 9 Mar 2017 12:34:06 +0000 (12:34 +0000)
Change-Id: Ic90f8f9b808d353cce289010b4907de55204091f
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ConfigArg.java
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/GeneratedDirectories.java [new file with mode: 0644]
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangProvider.java
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/GenerateSourcesTest.java

index 1ccbb30a21f104a1f2a855a821ab83597cf0ecd9..341f2d47a621f94134f6d211d11d3918c4f0063e 100644 (file)
@@ -7,14 +7,12 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
+import com.google.common.base.Preconditions;
 import java.io.File;
+import java.util.HashMap;
 import java.util.Map;
-
 import org.apache.maven.project.MavenProject;
 
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
-
 /**
  * Base complex configuration arguments
  */
@@ -43,16 +41,11 @@ public abstract class ConfigArg {
      * Configuration argument for code generator class and output directory.
      */
     public static final class CodeGeneratorArg extends ConfigArg {
-        private static final String TARGET_GENERATED_SOURCES = "target" + File.separator + "generated-sources";
-        private static final String CODE_GEN_DEFAULT_RESOURCE_DIR = TARGET_GENERATED_SOURCES + File.separator + "spi";
-        public static final String YANG_GENERATED_DIR = TARGET_GENERATED_SOURCES + File.separator + "yang";
-        public static final String YANG_SERVICES_GENERATED_DIR = TARGET_GENERATED_SOURCES + File.separator + "spi";
 
+        private final Map<String, String> additionalConfiguration = new HashMap<>();
 
         private String codeGeneratorClass;
-        private File resourceBaseDir = new File(CODE_GEN_DEFAULT_RESOURCE_DIR);
-
-        private Map<String, String> additionalConfiguration = Maps.newHashMap();
+        private File resourceBaseDir;
 
         public CodeGeneratorArg() {
             super(null);
@@ -83,11 +76,13 @@ public abstract class ConfigArg {
         }
 
         public File getResourceBaseDir(MavenProject project) {
-            if (resourceBaseDir.isAbsolute()) {
-                return resourceBaseDir;
-            } else {
-                return new File(project.getBasedir(), resourceBaseDir.getPath());
+            if (resourceBaseDir == null) {
+                // if it has not been set, use a default (correctly dealing with target/ VS target-ide)
+                return new GeneratedDirectories(project).getYangServicesDir();
             }
+
+            return resourceBaseDir.isAbsolute() ? resourceBaseDir
+                        : new File(project.getBasedir(), resourceBaseDir.getPath());
         }
 
         public Map<String, String> getAdditionalConfiguration() {
diff --git a/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/GeneratedDirectories.java b/yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/GeneratedDirectories.java
new file mode 100644 (file)
index 0000000..5fc303b
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang2sources.plugin;
+
+import java.io.File;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Utility to obtain the correct path to generated directories.
+ * It's important that this does not hard-code "target/" anywhere, but uses
+ * ${project.build.directory}/, to make target-ide/ possible.
+ *
+ * @author Michael Vorburger.ch
+ */
+class GeneratedDirectories {
+
+    private final File targetGeneratedSources;
+
+    public GeneratedDirectories(MavenProject project) {
+        this.targetGeneratedSources = new File(project.getBuild().getDirectory(), "generated-sources");
+    }
+
+    public File getYangServicesDir() {
+        return new File(targetGeneratedSources, "spi");
+    }
+
+    public File getYangDir() {
+        return new File(targetGeneratedSources, "yang");
+    }
+
+}
index 1e37925139a2079d27a876d529d99c4b9b853185..29d72b513a09c629d81915e8558d709701a37d8b 100644 (file)
@@ -13,7 +13,6 @@ import java.util.Collection;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
-import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -24,18 +23,8 @@ class YangProvider {
             final Collection<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);
+        File generatedYangDir = new GeneratedDirectories(project).getYangDir();
         addYangsToMetaInf(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(project, yangFilesRootDir, excludedFiles, actualGeneratedYangDir);
-        }
     }
 
     private static void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
index 58297c2c5804453e510c2e1412c70f6cc108a3bf..3389404a8071de1c4dccf474849f056e21d2c4c2 100644 (file)
@@ -107,7 +107,7 @@ class YangToSourcesProcessor {
             yangProvider.addYangsToMetaInf(project, yangFilesRootDir, excludedFiles);
 
             // add META_INF/services
-            File generatedServicesDir = new File(project.getBasedir(), CodeGeneratorArg.YANG_SERVICES_GENERATED_DIR);
+            File generatedServicesDir = new GeneratedDirectories(project).getYangServicesDir();
             YangProvider.setResource(generatedServicesDir, project);
             LOG.debug("{} Yang services files from: {} marked as resources: {}", LOG_PREFIX, generatedServicesDir,
                     META_INF_YANG_SERVICES_STRING_JAR);
index f9505d03decbf91eaebc837125f7b2434289a43f..e53c53d6b150b85fd92bebad52d49bfd6541a078 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
+import org.apache.maven.model.Build;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.project.MavenProject;
 import org.junit.Before;
@@ -41,8 +42,13 @@ public class GenerateSourcesTest {
     private String yang;
     private YangToSourcesMojo mojo;
     private File outDir;
+
     @Mock
     private MavenProject project;
+
+    @Mock
+    private Build build;
+
     @Mock
     private Plugin plugin;
 
@@ -61,6 +67,8 @@ public class GenerateSourcesTest {
                 mock);
         this.mojo = new YangToSourcesMojo(processor);
         doReturn(new File("")).when(this.project).getBasedir();
+        doReturn("target/").when(this.build).getDirectory();
+        doReturn(this.build).when(this.project).getBuild();
         doReturn(Collections.emptyList()).when(this.plugin).getDependencies();
         doReturn(this.plugin).when(this.project).getPlugin(YangToSourcesMojo.PLUGIN_NAME);
         this.mojo.setProject(this.project);