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;
10 import com.google.common.annotations.VisibleForTesting;
12 import java.util.Arrays;
13 import java.util.Collections;
14 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.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
29 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
30 import org.sonatype.plexus.build.incremental.BuildContext;
33 * Generate sources from yang files using user provided set of
34 * {@link BasicCodeGenerator}s. Steps of this process:
36 * <li>List yang files from {@link #yangFilesRootDir}</li>
37 * <li>Process yang files using Yang Parser</li>
38 * <li>For each {@link BasicCodeGenerator} from {@link #codeGenerators}:
40 * <li>Instantiate using default constructor</li>
41 * <li>Call {@link BasicCodeGenerator#generateSources(SchemaContext, File, Set)}</li>
45 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
46 public final class YangToSourcesMojo extends AbstractMojo {
47 public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
50 * Classes implementing {@link BasicCodeGenerator} interface. An instance will be
51 * created out of every class using default constructor. Method {@link
52 * CodeGenerator#generateSources(SchemaContext, File, Set<String>
53 * yangModulesNames)} will be called on every instance.
55 @Parameter(required = false)
56 private CodeGeneratorArg[] codeGenerators;
59 * Source directory that will be recursively searched for yang files (ending
62 @Parameter(required = false)
63 // defaults to ${basedir}/src/main/yang
64 private String yangFilesRootDir;
66 @Parameter(required = false)
67 private String[] excludeFiles;
69 @Parameter(property = "project", required = true, readonly = true)
70 private MavenProject project;
72 @Parameter(property = "inspectDependencies")
73 private boolean inspectDependencies;
76 private BuildContext buildContext;
78 private YangToSourcesProcessor yangToSourcesProcessor;
81 private RepositorySystem repoSystem;
83 @Parameter( readonly = true, defaultValue = "${localRepository}" )
84 private ArtifactRepository localRepository;
86 @Parameter( readonly = true, defaultValue = "${project.remoteArtifactRepositories}" )
87 private List<ArtifactRepository> remoteRepos;
89 // When set to "true", then the execution of the plugin is disabled
90 @Parameter( property = "yang.skip" )
91 private String yang_skip;
93 public YangToSourcesMojo() {
96 public void setProject(final MavenProject project) {
97 this.project = project;
101 YangToSourcesMojo(final YangToSourcesProcessor processor) {
102 this.yangToSourcesProcessor = processor;
106 public void execute() throws MojoExecutionException, MojoFailureException {
107 Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
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, yangFilesRootFile,
117 excludedFiles, codeGeneratorArgs, project, inspectDependencies);
119 yangToSourcesProcessor.conditionalExecute("true".equals(yang_skip));
122 private static List<CodeGeneratorArg> processCodeGenerators(final 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(final String yangFilesRootDir, final 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(final String[] excludeFiles, final 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]);