Bug 7182 related: yang-maven-plugin no hard-coded target/ (target-ide/)
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangProvider.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import java.io.File;
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.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 class YangProvider {
20     private static final Logger LOG = LoggerFactory.getLogger(YangProvider.class);
21
22     void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
23             final Collection<File> excludedFiles) throws MojoFailureException {
24
25         // copy project's src/main/yang/*.yang to target/generated-sources/yang/META-INF/yang/*.yang
26         File generatedYangDir = new GeneratedDirectories(project).getYangDir();
27         addYangsToMetaInf(project, yangFilesRootDir, excludedFiles, generatedYangDir);
28     }
29
30     private static void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
31             final Collection<File> excludedFiles, final File generatedYangDir) throws MojoFailureException {
32
33         File withMetaInf = new File(generatedYangDir, YangToSourcesProcessor.META_INF_YANG_STRING);
34         withMetaInf.mkdirs();
35
36         try {
37             Collection<File> files = Util.listFiles(yangFilesRootDir, excludedFiles);
38             for (File file : files) {
39                 org.apache.commons.io.FileUtils.copyFile(file, new File(withMetaInf, file.getName()));
40             }
41         } catch (IOException e) {
42             LOG.warn("Failed to generate files into root {}", yangFilesRootDir, e);
43             throw new MojoFailureException("Unable to list yang files into resource folder", e);
44         }
45
46         setResource(generatedYangDir, project);
47
48         LOG.debug("{} Yang files from: {} marked as resources: {}", YangToSourcesProcessor.LOG_PREFIX, yangFilesRootDir,
49                 YangToSourcesProcessor.META_INF_YANG_STRING_JAR);
50     }
51
52     static void setResource(final File targetYangDir, final MavenProject project) {
53         Resource res = new Resource();
54         res.setDirectory(targetYangDir.getPath());
55         project.addResource(res);
56     }
57 }