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.plugin.AbstractMojo;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.plugin.MojoFailureException;
19 import org.apache.maven.plugins.annotations.Component;
20 import org.apache.maven.plugins.annotations.LifecyclePhase;
21 import org.apache.maven.plugins.annotations.Mojo;
22 import org.apache.maven.plugins.annotations.Parameter;
23 import org.apache.maven.plugins.annotations.ResolutionScope;
24 import org.apache.maven.project.MavenProject;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
27 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
28 import org.opendaylight.yangtools.yang2sources.spi.CodeGenerator;
29 import org.slf4j.impl.StaticLoggerBinder;
30 import org.sonatype.aether.RepositorySystem;
31 import org.sonatype.aether.RepositorySystemSession;
32 import org.sonatype.aether.repository.RemoteRepository;
33 import org.sonatype.plexus.build.incremental.BuildContext;
35 import com.google.common.annotations.VisibleForTesting;
38 * Generate sources from yang files using user provided set of
39 * {@link CodeGenerator}s. Steps of this process:
41 * <li>List yang files from {@link #yangFilesRootDir}</li>
42 * <li>Process yang files using {@link YangParserImpl}</li>
43 * <li>For each {@link CodeGenerator} from {@link #codeGenerators}:</li>
45 * <li>Instantiate using default constructor</li>
46 * <li>Call {@link CodeGenerator#generateSources(SchemaContext, File, Set)}</li>
50 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
51 public final class YangToSourcesMojo extends AbstractMojo {
52 public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
55 * Classes implementing {@link CodeGenerator} interface. An instance will be
56 * created out of every class using default constructor. Method {@link
57 * CodeGenerator#generateSources(SchemaContext, File, Set<String>
58 * yangModulesNames)} will be called on every instance.
60 @Parameter(required = false)
61 private CodeGeneratorArg[] codeGenerators;
64 * Source directory that will be recursively searched for yang files (ending
67 @Parameter(required = false)
68 // defaults to ${basedir}/src/main/yang
69 private String yangFilesRootDir;
71 @Parameter(required = false)
72 private String[] excludeFiles;
74 @Parameter(property = "project", required = true, readonly = true)
75 private MavenProject project;
77 @Parameter(property = "inspectDependencies", required = true, readonly = true)
78 private boolean inspectDependencies;
81 private BuildContext buildContext;
83 private YangToSourcesProcessor yangToSourcesProcessor;
86 private RepositorySystem repoSystem;
88 @Parameter( readonly = true, defaultValue = "${repositorySystemSession}" )
89 private RepositorySystemSession repoSession;
91 @Parameter( readonly = true, defaultValue = "${project.remoteProjectRepositories}" )
92 private List<RemoteRepository> remoteRepos;
95 public YangToSourcesMojo() {
99 public void setProject(MavenProject project) {
100 this.project = project;
104 YangToSourcesMojo(YangToSourcesProcessor processor) {
105 this.yangToSourcesProcessor = processor;
109 public void execute() throws MojoExecutionException, MojoFailureException {
110 StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
112 Util.checkClasspath(project, getLog(), repoSystem, repoSession, remoteRepos);
114 if (yangToSourcesProcessor == null) {
115 List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
117 // defaults to ${basedir}/src/main/yang
118 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
119 File[] excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
121 yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, getLog(), yangFilesRootFile, excludedFiles,
122 codeGeneratorArgs, project, inspectDependencies);
124 yangToSourcesProcessor.execute();
127 private static List<CodeGeneratorArg> processCodeGenerators(CodeGeneratorArg[] codeGenerators) {
128 List<CodeGeneratorArg> codeGeneratorArgs;
129 if (codeGenerators == null) {
130 codeGeneratorArgs = Collections.emptyList();
132 codeGeneratorArgs = Arrays.asList(codeGenerators);
134 return codeGeneratorArgs;
137 private static File processYangFilesRootDir(String yangFilesRootDir, File baseDir) {
138 File yangFilesRootFile;
139 if (yangFilesRootDir == null) {
140 yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
142 File file = new File(yangFilesRootDir);
143 if (file.isAbsolute()) {
144 yangFilesRootFile = file;
146 yangFilesRootFile = new File(baseDir, file.getPath());
149 return yangFilesRootFile;
152 private static File[] processExcludeFiles(String[] excludeFiles, File baseDir) {
153 if (excludeFiles == null) {
154 return new File[] {};
156 File[] result = new File[excludeFiles.length];
157 for (int i = 0; i < excludeFiles.length; i++) {
158 result[i] = new File(baseDir, excludeFiles[i]);