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.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
16 import org.apache.maven.artifact.repository.ArtifactRepository;
17 import org.apache.maven.plugin.AbstractMojo;
18 import org.apache.maven.plugin.MojoExecutionException;
19 import org.apache.maven.plugin.MojoFailureException;
20 import org.apache.maven.plugins.annotations.Component;
21 import org.apache.maven.plugins.annotations.LifecyclePhase;
22 import org.apache.maven.plugins.annotations.Mojo;
23 import org.apache.maven.plugins.annotations.Parameter;
24 import org.apache.maven.plugins.annotations.ResolutionScope;
25 import org.apache.maven.project.MavenProject;
26 import org.apache.maven.repository.RepositorySystem;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
30 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
31 import org.sonatype.plexus.build.incremental.BuildContext;
33 import com.google.common.annotations.VisibleForTesting;
36 * Generate sources from yang files using user provided set of
37 * {@link CodeGenerator}s. Steps of this process:
39 * <li>List yang files from {@link #yangFilesRootDir}</li>
40 * <li>Process yang files using {@link YangParserImpl}</li>
41 * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:
43 * <li>Instantiate using default constructor</li>
44 * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File, Set)}</li>
48 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
49 public final class YangToSourcesMojo extends AbstractMojo {
50 public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
53 * Classes implementing {@link CodeGenerator} interface. An instance will be
54 * created out of every class using default constructor. Method {@link
55 * CodeGenerator#generateSources(SchemaContext, File, Set<String>
56 * yangModulesNames)} will be called on every instance.
58 @Parameter(required = false)
59 private CodeGeneratorArg[] codeGenerators;
62 * Source directory that will be recursively searched for yang files (ending
65 @Parameter(required = false)
66 // defaults to ${basedir}/src/main/yang
67 private String yangFilesRootDir;
69 @Parameter(required = false)
70 private String[] excludeFiles;
72 @Parameter(property = "project", required = true, readonly = true)
73 private MavenProject project;
75 @Parameter(property = "inspectDependencies", required = true, readonly = true)
76 private boolean inspectDependencies;
79 private BuildContext buildContext;
81 private YangToSourcesProcessor yangToSourcesProcessor;
84 private RepositorySystem repoSystem;
86 @Parameter( readonly = true, defaultValue = "${localRepository}" )
87 private ArtifactRepository localRepository;
89 @Parameter( readonly = true, defaultValue = "${project.remoteArtifactRepositories}" )
90 private List<ArtifactRepository> remoteRepos;
93 public YangToSourcesMojo() {
96 public void setProject(MavenProject project) {
97 this.project = project;
101 YangToSourcesMojo(YangToSourcesProcessor processor) {
102 this.yangToSourcesProcessor = processor;
106 public void execute() throws MojoExecutionException, MojoFailureException {
107 Util.checkClasspath(project, repoSystem, localRepository, remoteRepos, getLog());
109 if (yangToSourcesProcessor == null) {
110 List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
112 // defaults to ${basedir}/src/main/yang
113 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
114 File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
116 yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, getLog(), yangFilesRootFile,
117 excludedFiles, codeGeneratorArgs, project, inspectDependencies);
119 yangToSourcesProcessor.execute();
122 private static List<CodeGeneratorArg> processCodeGenerators(CodeGeneratorArg[] codeGenerators) {
123 List<CodeGeneratorArg> codeGeneratorArgs;
124 if (codeGenerators == null) {
125 codeGeneratorArgs = Collections.emptyList();
127 codeGeneratorArgs = Arrays.asList(codeGenerators);
129 return codeGeneratorArgs;
132 private static File processYangFilesRootDir(String yangFilesRootDir, File baseDir) {
133 File yangFilesRootFile;
134 if (yangFilesRootDir == null) {
135 yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
137 File file = new File(yangFilesRootDir);
138 if (file.isAbsolute()) {
139 yangFilesRootFile = file;
141 yangFilesRootFile = new File(baseDir, file.getPath());
144 return yangFilesRootFile;
147 private static File[] processExcludeFiles(String[] excludeFiles, File baseDir) {
148 if (excludeFiles == null) {
149 return new File[] {};
151 File[] result = new File[excludeFiles.length];
152 for (int i = 0; i < excludeFiles.length; i++) {
153 result[i] = new File(baseDir, excludeFiles[i]);