*/
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
*/
* 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);
}
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() {
--- /dev/null
+/*
+ * 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");
+ }
+
+}
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;
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,
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);
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;
private String yang;
private YangToSourcesMojo mojo;
private File outDir;
+
@Mock
private MavenProject project;
+
+ @Mock
+ private Build build;
+
@Mock
private Plugin plugin;
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);