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;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
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.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
32 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
33 import org.sonatype.plexus.build.incremental.BuildContext;
36 * Generate sources from yang files using user provided set of
37 * {@link BasicCodeGenerator}s. Steps of this process:
39 * <li>List yang files from {@link #yangFilesRootDir}</li>
40 * <li>Process yang files using Yang Parser</li>
41 * <li>For each {@link BasicCodeGenerator} from {@link #codeGenerators}:
43 * <li>Instantiate using default constructor</li>
44 * <li>Call {@link BasicCodeGenerator#generateSources(SchemaContext, File, Set)}</li>
48 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
49 requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
50 public final class YangToSourcesMojo extends AbstractMojo {
51 public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
54 * Classes implementing {@link BasicCodeGenerator} interface. An instance will be
55 * created out of every class using default constructor. Method {@link
56 * BasicCodeGenerator#generateSources(SchemaContext, File, Set)} will be called on every instance.
58 @Parameter(required = false)
59 private CodeGeneratorArg[] codeGenerators;
62 * Source directory that will be recursively searched for yang files (ending
65 @Parameter(required = false)
66 // defaults to ${basedir}/src/main/yang
67 private String yangFilesRootDir;
69 @Parameter(required = false)
70 private String[] excludeFiles;
72 @Parameter(property = "project", required = true, readonly = true)
73 private MavenProject project;
75 @Parameter(property = "inspectDependencies")
76 private boolean inspectDependencies;
79 private BuildContext buildContext;
81 private YangToSourcesProcessor yangToSourcesProcessor;
84 private RepositorySystem repoSystem;
86 @Parameter(readonly = true, defaultValue = "${localRepository}")
87 private ArtifactRepository localRepository;
89 @Parameter(readonly = true, defaultValue = "${project.remoteArtifactRepositories}")
90 private List<ArtifactRepository> remoteRepos;
92 // When set to "true", then the execution of the plugin is disabled
93 @Parameter(property = "yang.skip")
94 private String yangSkip;
96 public YangToSourcesMojo() {
99 public void setProject(final MavenProject project) {
100 this.project = project;
104 YangToSourcesMojo(final YangToSourcesProcessor processor) {
105 this.yangToSourcesProcessor = processor;
109 public void execute() throws MojoExecutionException, MojoFailureException {
110 Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
112 if (yangToSourcesProcessor == null) {
113 List<CodeGeneratorArg> codeGeneratorArgs = processCodeGenerators(codeGenerators);
115 // defaults to ${basedir}/src/main/yang
116 File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
117 Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
119 yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
120 excludedFiles, codeGeneratorArgs, project, inspectDependencies);
122 yangToSourcesProcessor.conditionalExecute("true".equals(yangSkip));
125 private static List<CodeGeneratorArg> processCodeGenerators(final CodeGeneratorArg[] codeGenerators) {
126 List<CodeGeneratorArg> codeGeneratorArgs;
127 if (codeGenerators == null) {
128 codeGeneratorArgs = Collections.emptyList();
130 codeGeneratorArgs = Arrays.asList(codeGenerators);
132 return codeGeneratorArgs;
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));