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.plugin.generator.api.ModuleResourceResolver;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
34 import org.sonatype.plexus.build.incremental.BuildContext;
37 * Generate sources from yang files using user provided set of
38 * {@link FileGeneratorArg}s. Steps of this process:
40 * <li>List yang files from {@link #yangFilesRootDir}</li>
41 * <li>Process yang files using Yang Parser</li>
42 * <li>For each {@link FileGeneratorArg} from {@link #fileGenerators}:
44 * <li>Instantiate using default constructor</li>
45 * <li>Call {@link FileGenerator#generateFiles(EffectiveModelContext, Set, ModuleResourceResolver)}</li>
50 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
51 requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true, threadSafe = true)
52 public final class YangToSourcesMojo extends AbstractMojo {
53 public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
56 * {@link FileGenerator} instances resolved via ServiceLoader can hold additional configuration, which details
57 * how they are executed.
59 @Parameter(required = false)
60 private FileGeneratorArg[] fileGenerators;
63 * Source directory that will be recursively searched for yang files (ending
66 @Parameter(required = false)
67 // defaults to ${basedir}/src/main/yang
68 private String yangFilesRootDir;
70 @Parameter(required = false)
71 private String[] excludeFiles;
73 @Parameter(property = "project", required = true, readonly = true)
74 private MavenProject project;
76 @Parameter(property = "inspectDependencies")
77 private boolean inspectDependencies;
81 BuildContext buildContext;
83 private YangToSourcesProcessor yangToSourcesProcessor;
86 private RepositorySystem repoSystem;
88 @Parameter(readonly = true, defaultValue = "${localRepository}")
89 private ArtifactRepository localRepository;
91 @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
92 private List<ArtifactRepository> remoteRepos;
94 // When set to "true", then the execution of the plugin is disabled
95 @Parameter(property = "yang.skip")
96 private String yangSkip;
98 @Parameter(defaultValue = "DEFAULT_MODE")
99 @Deprecated(forRemoval = true)
100 private StatementParserMode parserMode;
102 public YangToSourcesMojo() {
107 YangToSourcesMojo(final YangToSourcesProcessor processor) {
108 yangToSourcesProcessor = processor;
111 public void setProject(final MavenProject project) {
112 this.project = project;
116 @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "yangFilesRootDir")
117 public void execute() throws MojoExecutionException, MojoFailureException {
118 Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
120 if (yangToSourcesProcessor == null) {
121 // defaults to ${basedir}/src/main/yang
122 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
123 Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
125 yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
126 excludedFiles, arrayToList(fileGenerators), project, inspectDependencies);
128 yangToSourcesProcessor.conditionalExecute("true".equals(yangSkip));
131 private static <T> List<T> arrayToList(final T[] array) {
132 return array == null ? ImmutableList.of() : Arrays.asList(array);
135 private static File processYangFilesRootDir(final String yangFilesRootDir, final File baseDir) {
136 File yangFilesRootFile;
137 if (yangFilesRootDir == null) {
138 yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
140 File file = new File(yangFilesRootDir);
141 if (file.isAbsolute()) {
142 yangFilesRootFile = file;
144 yangFilesRootFile = new File(baseDir, file.getPath());
147 return yangFilesRootFile;
150 private static Collection<File> processExcludeFiles(final String[] excludeFiles, final File baseDir) {
151 if (excludeFiles == null) {
152 return ImmutableList.of();
155 return Collections2.transform(Arrays.asList(excludeFiles), f -> new File(baseDir, f));