Fix yang.skip implementation 19/104619/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Feb 2023 12:43:23 +0000 (13:43 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 28 Feb 2023 12:43:23 +0000 (13:43 +0100)
The documentation implies yang.skip disables execution completely, which
is in-line with other Maven plugins' skip function. Fix the
implementation to completely forgo execution.

Change-Id: I1cd58a73dd2c9e1f6f5c42a27de149079bb9175b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojo.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojoTest.java

index 391762e1c0d922e39b9f29f3cec098771cd11e64..3a6c88e07ca2a3137b9f1164fa6f96b480432dcb 100644 (file)
@@ -31,6 +31,8 @@ import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
 import org.opendaylight.yangtools.plugin.generator.api.ModuleResourceResolver;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
@@ -52,6 +54,8 @@ import org.sonatype.plexus.build.incremental.BuildContext;
 public final class YangToSourcesMojo extends AbstractMojo {
     public static final String PLUGIN_NAME = "org.opendaylight.yangtools:yang-maven-plugin";
 
+    private static final Logger LOG = LoggerFactory.getLogger(YangToSourcesMojo.class);
+
     /**
      * {@link FileGenerator} instances resolved via ServiceLoader can hold additional configuration, which details
      * how they are executed.
@@ -92,8 +96,8 @@ public final class YangToSourcesMojo extends AbstractMojo {
     private List<ArtifactRepository> remoteRepos;
 
     // When set to "true", then the execution of the plugin is disabled
-    @Parameter(property = "yang.skip")
-    private String yangSkip;
+    @Parameter(property = "yang.skip", defaultValue = "false")
+    private boolean yangSkip;
 
     @Parameter(defaultValue = "DEFAULT_MODE")
     @Deprecated(forRemoval = true)
@@ -115,17 +119,22 @@ public final class YangToSourcesMojo extends AbstractMojo {
     @Override
     @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "yangFilesRootDir")
     public void execute() throws MojoExecutionException, MojoFailureException {
-        Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
+        if (yangSkip) {
+            LOG.info("{} Skipping YANG code generation because property yang.skip is true",
+                YangToSourcesProcessor.LOG_PREFIX);
+            return;
+        }
 
+        Util.checkClasspath(project, repoSystem, localRepository, remoteRepos);
         if (yangToSourcesProcessor == null) {
             // defaults to ${basedir}/src/main/yang
             File yangFilesRootFile = processYangFilesRootDir(yangFilesRootDir, project.getBasedir());
             Collection<File> excludedFiles = processExcludeFiles(excludeFiles, yangFilesRootFile);
 
             yangToSourcesProcessor = new YangToSourcesProcessor(buildContext, yangFilesRootFile,
-                    excludedFiles, arrayToList(fileGenerators), project, inspectDependencies);
+                excludedFiles, arrayToList(fileGenerators), project, inspectDependencies);
         }
-        yangToSourcesProcessor.conditionalExecute("true".equals(yangSkip));
+        yangToSourcesProcessor.execute();
     }
 
     private static <T> List<T> arrayToList(final T[] array) {
index 7641aa5514929246af19df207eb4348409354118..acc34e79f5159206d2f6258de93d9ed9f5089c09 100644 (file)
@@ -109,11 +109,7 @@ class YangToSourcesProcessor {
             YangProvider.getInstance());
     }
 
-    public void execute() throws MojoExecutionException, MojoFailureException {
-        conditionalExecute(false);
-    }
-
-    void conditionalExecute(final boolean skip) throws MojoExecutionException, MojoFailureException {
+    void execute() throws MojoExecutionException, MojoFailureException {
         var prevState = buildContext.getValue(BUILD_CONTEXT_STATE_NAME);
         if (prevState == null) {
             LOG.debug("{} BuildContext did not provide state", LOG_PREFIX);
@@ -201,37 +197,32 @@ class YangToSourcesProcessor {
             if (optReactor.isPresent()) {
                 final ProcessorModuleReactor reactor = optReactor.orElseThrow();
 
-                if (!skip) {
-                    final Stopwatch sw = Stopwatch.createStarted();
-                    final ContextHolder holder;
+                final Stopwatch sw = Stopwatch.createStarted();
+                final ContextHolder holder;
+
+                try {
+                    holder = reactor.toContext();
+                } catch (YangParserException e) {
+                    throw new MojoFailureException("Failed to process reactor " + reactor, e);
+                } catch (IOException e) {
+                    throw new MojoExecutionException("Failed to read reactor " + reactor, e);
+                }
+
+                LOG.info("{} {} YANG models processed in {}", LOG_PREFIX, holder.getContext().getModules().size(), sw);
+                outputFiles.addAll(generateSources(holder, codeGenerators, parserConfig));
 
+                if (!sourcesPersisted) {
+                    // add META_INF/yang once
+                    final var models = reactor.getModelsInProject();
+                    final Collection<FileState> sourceFileStates;
                     try {
-                        holder = reactor.toContext();
-                    } catch (YangParserException e) {
-                        throw new MojoFailureException("Failed to process reactor " + reactor, e);
+                        sourceFileStates = yangProvider.addYangsToMetaInf(project, models);
                     } catch (IOException e) {
-                        throw new MojoExecutionException("Failed to read reactor " + reactor, e);
+                        throw new MojoExecutionException("Failed write model files for " + models, e);
                     }
 
-                    LOG.info("{} {} YANG models processed in {}", LOG_PREFIX, holder.getContext().getModules().size(),
-                        sw);
-                    outputFiles.addAll(generateSources(holder, codeGenerators, parserConfig));
-
-                    if (!sourcesPersisted) {
-                        // add META_INF/yang once
-                        final var models = reactor.getModelsInProject();
-                        final Collection<FileState> sourceFileStates;
-                        try {
-                            sourceFileStates = yangProvider.addYangsToMetaInf(project, models);
-                        } catch (IOException e) {
-                            throw new MojoExecutionException("Failed write model files for " + models, e);
-                        }
-
-                        sourcesPersisted = true;
-                        outputFiles.addAll(sourceFileStates);
-                    }
-                } else {
-                    LOG.info("{} Skipping YANG code generation because property yang.skip is true", LOG_PREFIX);
+                    sourcesPersisted = true;
+                    outputFiles.addAll(sourceFileStates);
                 }
             }
         }
index 6b2dededf8ca1e73560af9568ea6939e7a566583..b15224c6e4e4c45ed863eaac4dd74f5ca257041b 100644 (file)
@@ -41,6 +41,6 @@ public class YangToSourcesMojoTest {
         mojo = new YangToSourcesMojo(processor);
         mojo.setProject(project);
         mojo.execute();
-        verify(processor).conditionalExecute(false);
+        verify(processor).execute();
     }
 }