2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang2sources.plugin;
11 import java.io.IOException;
12 import java.util.Collection;
13 import org.apache.maven.model.Resource;
14 import org.apache.maven.plugin.MojoFailureException;
15 import org.apache.maven.project.MavenProject;
16 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
21 private static final Logger LOG = LoggerFactory.getLogger(YangProvider.class);
23 void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
24 final Collection<File> excludedFiles) throws MojoFailureException {
26 // copy project's src/main/yang/*.yang to target/generated-sources/yang/META-INF/yang/*.yang
28 File generatedYangDir = new File(project.getBasedir(), CodeGeneratorArg.YANG_GENERATED_DIR);
29 addYangsToMetaInf(project, yangFilesRootDir, excludedFiles, generatedYangDir);
31 // Also copy to the actual build output dir if different than "target". When running in
32 // Eclipse this can differ (eg "target-ide").
34 File actualGeneratedYangDir = new File(project.getBuild().getDirectory(),
35 CodeGeneratorArg.YANG_GENERATED_DIR.replace("target" + File.separator, ""));
36 if (!actualGeneratedYangDir.equals(generatedYangDir)) {
37 addYangsToMetaInf(project, yangFilesRootDir, excludedFiles, actualGeneratedYangDir);
41 private static void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
42 final Collection<File> excludedFiles, final File generatedYangDir) throws MojoFailureException {
44 File withMetaInf = new File(generatedYangDir, YangToSourcesProcessor.META_INF_YANG_STRING);
48 Collection<File> files = Util.listFiles(yangFilesRootDir, excludedFiles);
49 for (File file : files) {
50 org.apache.commons.io.FileUtils.copyFile(file, new File(withMetaInf, file.getName()));
52 } catch (IOException e) {
53 LOG.warn("Failed to generate files into root {}", yangFilesRootDir, e);
54 throw new MojoFailureException("Unable to list yang files into resource folder", e);
57 setResource(generatedYangDir, project);
59 LOG.debug("{} Yang files from: {} marked as resources: {}", YangToSourcesProcessor.LOG_PREFIX, yangFilesRootDir,
60 YangToSourcesProcessor.META_INF_YANG_STRING_JAR);
63 static void setResource(final File targetYangDir, final MavenProject project) {
64 Resource res = new Resource();
65 res.setDirectory(targetYangDir.getPath());
66 project.addResource(res);