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;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableList;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.List;
19 import org.apache.maven.artifact.repository.ArtifactRepository;
20 import org.apache.maven.plugin.AbstractMojo;
21 import org.apache.maven.plugin.MojoExecutionException;
22 import org.apache.maven.plugin.MojoFailureException;
23 import org.apache.maven.plugins.annotations.Component;
24 import org.apache.maven.plugins.annotations.LifecyclePhase;
25 import org.apache.maven.plugins.annotations.Mojo;
26 import org.apache.maven.plugins.annotations.Parameter;
27 import org.apache.maven.plugins.annotations.ResolutionScope;
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.repository.RepositorySystem;
30 import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
33 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
34 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
35 import org.sonatype.plexus.build.incremental.BuildContext;
38 * Generate sources from yang files using user provided set of
39 * {@link BasicCodeGenerator}s. Steps of this process:
41 * <li>List yang files from {@link #yangFilesRootDir}</li>
42 * <li>Process yang files using Yang Parser</li>
43 * <li>For each {@link BasicCodeGenerator} from {@link #codeGenerators}:
45 * <li>Instantiate using default constructor</li>
46 * <li>Call {@link BasicCodeGenerator#generateSources(EffectiveModelContext, File, Set,
47 * org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver)}</li>
52 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
53 requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true, threadSafe = true)
54 public final class YangToSourcesMojo extends AbstractMojo {
55 public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
58 * Classes implementing {@link BasicCodeGenerator} interface. An instance will be
59 * created out of every class using default constructor. Method {@link
60 * BasicCodeGenerator#generateSources(EffectiveModelContext, File, Set)} will be called on every instance.
62 @Parameter(required = false)
63 private CodeGeneratorArg[] codeGenerators;
66 * {@link FileGenerator} instances resolved via ServiceLoader can hold additional configuration, which details
67 * how they are executed.
69 @Parameter(required = false)
70 private FileGeneratorArg[] fileGenerators;
73 * Source directory that will be recursively searched for yang files (ending
76 @Parameter(required = false)
77 // defaults to ${basedir}/src/main/yang
78 private String yangFilesRootDir;
80 @Parameter(required = false)
81 private String[] excludeFiles;
83 @Parameter(property = "project", required = true, readonly = true)
84 private MavenProject project;
86 @Parameter(property = "inspectDependencies")
87 private boolean inspectDependencies;
91 BuildContext buildContext;
93 private YangToSourcesProcessor yangToSourcesProcessor;
96 private RepositorySystem repoSystem;
98 @Parameter(readonly = true, defaultValue = "${localRepository}")
99 private ArtifactRepository localRepository;
101 @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
102 private List<ArtifactRepository> remoteRepos;
104 // When set to "true", then the execution of the plugin is disabled
105 @Parameter(property = "yang.skip")
106 private String yangSkip;
108 @Parameter(defaultValue = "DEFAULT_MODE")
109 @Deprecated(forRemoval = true)
110 private StatementParserMode parserMode;
112 public YangToSourcesMojo() {
117 YangToSourcesMojo(final YangToSourcesProcessor processor) {
118 this.yangToSourcesProcessor = processor;
121 public void setProject(final MavenProject project) {
122 this.project = project;
126 @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "yangFilesRootDir")
127 public void execute() throws MojoExecutionException, MojoFailureException {
128 Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
130 if (yangToSourcesProcessor == null) {
131 // defaults to ${basedir}/src/main/yang
132 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
133 Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
135 yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
136 excludedFiles, arrayToList(codeGenerators), arrayToList(fileGenerators), project,
137 inspectDependencies);
139 yangToSourcesProcessor.conditionalExecute("true".equals(yangSkip));
142 private static <T> List<T> arrayToList(final T[] array) {
143 return array == null ? ImmutableList.of() : Arrays.asList(array);
146 private static File processYangFilesRootDir(final String yangFilesRootDir, final File baseDir) {
147 File yangFilesRootFile;
148 if (yangFilesRootDir == null) {
149 yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
151 File file = new File(yangFilesRootDir);
152 if (file.isAbsolute()) {
153 yangFilesRootFile = file;
155 yangFilesRootFile = new File(baseDir, file.getPath());
158 return yangFilesRootFile;
161 private static Collection<File> processExcludeFiles(final String[] excludeFiles, final File baseDir) {
162 if (excludeFiles == null) {
163 return ImmutableList.of();
166 return Collections2.transform(Arrays.asList(excludeFiles), f -> new File(baseDir, f));