From 43efab89ef0b3867ecdfbed128da330d03e55245 Mon Sep 17 00:00:00 2001 From: Matt Rich Date: Fri, 13 May 2016 11:25:27 +0100 Subject: [PATCH] Add skipGeneration flag to skip code generation --- raml-maven-plugin/pom.xml | 1 + .../raml/maven/generator/GenerateMojo.java | 31 ++++++++++++------- .../maven/generator/GenerateMojoTest.java | 11 +++++-- .../src/test/resources/skip-execution/pom.xml | 30 ++++++++++++++++++ .../skip-execution/src/raml/example.raml | 2 ++ 5 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 raml-maven-plugin/src/test/resources/skip-execution/pom.xml create mode 100644 raml-maven-plugin/src/test/resources/skip-execution/src/raml/example.raml diff --git a/raml-maven-plugin/pom.xml b/raml-maven-plugin/pom.xml index 285ba31..c7da727 100644 --- a/raml-maven-plugin/pom.xml +++ b/raml-maven-plugin/pom.xml @@ -23,6 +23,7 @@ maven-plugin-plugin 3.4 + false raml diff --git a/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/generator/GenerateMojo.java b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/generator/GenerateMojo.java index c764180..0eb8424 100644 --- a/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/generator/GenerateMojo.java +++ b/raml-maven-plugin/src/main/java/uk/gov/justice/raml/maven/generator/GenerateMojo.java @@ -34,7 +34,6 @@ public class GenerateMojo extends BasicMojo { @Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}/generated-sources") private File outputDirectory; - /** * Base package name used for generated Java classes. */ @@ -44,24 +43,32 @@ public class GenerateMojo extends BasicMojo { @Parameter(property = "generatorProperties", required = false) private Map generatorProperties; + @Parameter(property = "skipGeneration", defaultValue = "false") + private boolean skip = false; + @Override public void execute() throws MojoExecutionException { - configureDefaultFileIncludes(); + if (!skip) { + configureDefaultFileIncludes(); - project.addCompileSourceRoot(outputDirectory.getPath()); - project.addTestCompileSourceRoot(outputDirectory.getPath()); + project.addCompileSourceRoot(outputDirectory.getPath()); + project.addTestCompileSourceRoot(outputDirectory.getPath()); - final List sourcePaths = new ArrayList<>(); - project.getCompileSourceRoots().stream().forEach(root -> sourcePaths.add(Paths.get(root))); + final List sourcePaths = new ArrayList<>(); + project.getCompileSourceRoots().stream().forEach(root -> sourcePaths.add(Paths.get(root))); - try { - FileUtils.forceMkdir(outputDirectory); - new GenerateGoalProcessor(new GeneratorFactory(), new FileTreeScannerFactory()) - .generate(configuration(sourcePaths)); - } catch (Exception e) { - throw new MojoExecutionException("Failed to apply generator to raml", e); + try { + FileUtils.forceMkdir(outputDirectory); + new GenerateGoalProcessor(new GeneratorFactory(), new FileTreeScannerFactory()) + .generate(configuration(sourcePaths)); + } catch (Exception e) { + throw new MojoExecutionException("Failed to apply generator to raml", e); + } + } else { + getLog().info("Skipping generation plugin execution as generation.skip flag is enabled."); } + } private GenerateGoalConfig configuration(final List sourcePaths) { diff --git a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/generator/GenerateMojoTest.java b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/generator/GenerateMojoTest.java index 5f65f32..ecf8f77 100644 --- a/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/generator/GenerateMojoTest.java +++ b/raml-maven-plugin/src/test/java/uk/gov/justice/raml/maven/generator/GenerateMojoTest.java @@ -96,8 +96,6 @@ public void testShouldNotTryToInstatiateGeneratorIfNoRamlFiles() throws Exceptio GenerateMojo mojo = (GenerateMojo) lookupConfiguredMojo(pom, "generate"); mojo.execute(); - - } public void testShouldGenerateFromOnlyIncludedRaml() throws Exception { @@ -130,4 +128,13 @@ public void testShouldIncludeRamlFilesFromTheClasspath() throws Exception { hasProperty("title", equalTo("external-2.raml")))); } + public void testShouldSkipExecution() throws Exception { + File pom = getTestFile("src/test/resources/skip-execution/pom.xml"); + GenerateMojo mojo = (GenerateMojo) lookupConfiguredMojo(pom, "generate"); + + mojo.execute(); + + List> capturedGeneratorArgs = DummyGeneratorCaptor.getInstance().capturedArgs(); + assertThat(capturedGeneratorArgs, hasSize(0)); + } } diff --git a/raml-maven-plugin/src/test/resources/skip-execution/pom.xml b/raml-maven-plugin/src/test/resources/skip-execution/pom.xml new file mode 100644 index 0000000..bbd1584 --- /dev/null +++ b/raml-maven-plugin/src/test/resources/skip-execution/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + uk.gov.justice.raml.test + project-to-test + 1.0.0-SNAPSHOT + + + + + raml-maven-plugin + uk.gov.justice.maven + + uk.gov.justice.raml.maven.generator.DummyGenerator + + uk.gov.justice.api + ${basedir}/src/raml + + propertyValueABC + propertyValueDDD + + true + + + + + + diff --git a/raml-maven-plugin/src/test/resources/skip-execution/src/raml/example.raml b/raml-maven-plugin/src/test/resources/skip-execution/src/raml/example.raml new file mode 100644 index 0000000..184df9c --- /dev/null +++ b/raml-maven-plugin/src/test/resources/skip-execution/src/raml/example.raml @@ -0,0 +1,2 @@ +#%RAML 0.8 +title: example.raml