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.Collections;
18 import java.util.List;
20 import java.util.function.Function;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.plugin.AbstractMojo;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugins.annotations.Component;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.plugins.annotations.ResolutionScope;
30 import org.apache.maven.project.MavenProject;
31 import org.apache.maven.repository.RepositorySystem;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
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, Function)}</li>
50 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
51 requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
52 public final class YangToSourcesMojo extends AbstractMojo {
53 public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
56 * Classes implementing {@link BasicCodeGenerator} interface. An instance will be
57 * created out of every class using default constructor. Method {@link
58 * BasicCodeGenerator#generateSources(SchemaContext, File, Set)} 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")
78 private boolean inspectDependencies;
82 BuildContext buildContext;
84 private YangToSourcesProcessor yangToSourcesProcessor;
87 private RepositorySystem repoSystem;
89 @Parameter(readonly = true, defaultValue = "${localRepository}")
90 private ArtifactRepository localRepository;
92 @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
93 private List<ArtifactRepository> remoteRepos;
95 // When set to "true", then the execution of the plugin is disabled
96 @Parameter(property = "yang.skip")
97 private String yangSkip;
99 public YangToSourcesMojo() {
104 YangToSourcesMojo(final YangToSourcesProcessor processor) {
105 this.yangToSourcesProcessor = processor;
108 public void setProject(final MavenProject project) {
109 this.project = project;
113 @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "yangFilesRootDir")
114 public void execute() throws MojoExecutionException, MojoFailureException {
115 Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
117 if (yangToSourcesProcessor == null) {
118 List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
120 // defaults to ${basedir}/src/main/yang
121 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
122 Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
124 yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
125 excludedFiles, codeGeneratorArgs, project, inspectDependencies);
127 yangToSourcesProcessor.conditionalExecute("true".equals(yangSkip));
130 private static List<CodeGeneratorArg> processCodeGenerators(final CodeGeneratorArg[] codeGenerators) {
131 List<CodeGeneratorArg> codeGeneratorArgs;
132 if (codeGenerators == null) {
133 codeGeneratorArgs = Collections.emptyList();
135 codeGeneratorArgs = Arrays.asList(codeGenerators);
137 return codeGeneratorArgs;
140 private static File processYangFilesRootDir(final String yangFilesRootDir, final File baseDir) {
141 File yangFilesRootFile;
142 if (yangFilesRootDir == null) {
143 yangFilesRootFile = new File(baseDir, "src" + File.separator + "main" + File.separator + "yang");
145 File file = new File(yangFilesRootDir);
146 if (file.isAbsolute()) {
147 yangFilesRootFile = file;
149 yangFilesRootFile = new File(baseDir, file.getPath());
152 return yangFilesRootFile;
155 private static Collection<File> processExcludeFiles(final String[] excludeFiles, final File baseDir) {
156 if (excludeFiles == null) {
157 return ImmutableList.of();
160 return Collections2.transform(Arrays.asList(excludeFiles), f -> new File(baseDir, f));