From 879c30aba1b057d15f0ebe7927189f41bfacb336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mariusz=20Smyku=C5=82a?= Date: Sat, 16 Apr 2016 21:28:53 +0200 Subject: [PATCH] Feature/create stubs (#10) * removed warnings and simplified * automate stub generation --- pom.xml | 11 ++++ .../accurest/maven/ConvertMojo.groovy | 5 +- .../accurest/maven/GenerateStubsMojo.groovy | 66 ++++++++++++++++--- .../accurest/maven/GenerateTestsMojo.groovy | 7 +- .../io/codearte/accurest/maven/RunMojo.groovy | 3 +- src/test/projects/spring-boot-groovy/pom.xml | 25 +------ src/test/projects/spring-boot-java/pom.xml | 39 ++--------- 7 files changed, 83 insertions(+), 73 deletions(-) diff --git a/pom.xml b/pom.xml index 861223fbab..fd3b43a361 100644 --- a/pom.xml +++ b/pom.xml @@ -282,6 +282,17 @@ provided + + org.apache.maven + maven-archiver + 3.0.0 + + + org.codehaus.plexus + plexus-archiver + 3.1 + + io.takari.maven.plugins takari-plugin-testing diff --git a/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy b/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy index 547198b9c9..af0f7d5944 100644 --- a/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy +++ b/src/main/groovy/io/codearte/accurest/maven/ConvertMojo.groovy @@ -9,14 +9,13 @@ import org.apache.maven.model.path.PathTranslator import org.apache.maven.plugin.AbstractMojo import org.apache.maven.plugin.MojoExecutionException import org.apache.maven.plugin.MojoFailureException -import org.apache.maven.plugins.annotations.Component import org.apache.maven.plugins.annotations.LifecyclePhase import org.apache.maven.plugins.annotations.Mojo import org.apache.maven.plugins.annotations.Parameter import javax.inject.Inject -@Mojo(name = 'convert', requiresProject = false, defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +@Mojo(name = 'convert', requiresProject = false, defaultPhase = LifecyclePhase.PROCESS_TEST_RESOURCES) @CompileStatic class ConvertMojo extends AbstractMojo { @@ -35,7 +34,7 @@ class ConvertMojo extends AbstractMojo { @Parameter(property = 'accurest.skip', defaultValue = 'false') private boolean skip - @Component + @Parameter(defaultValue = '${session}', readonly = true) private MavenSession mavenSession private final PathTranslator translator diff --git a/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy b/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy index 165dc96663..ea75877350 100644 --- a/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy +++ b/src/main/groovy/io/codearte/accurest/maven/GenerateStubsMojo.groovy @@ -1,19 +1,67 @@ package io.codearte.accurest.maven import groovy.transform.CompileStatic -import org.apache.maven.model.path.PathTranslator +import org.apache.maven.plugin.AbstractMojo +import org.apache.maven.plugin.MojoExecutionException +import org.apache.maven.plugin.MojoFailureException +import org.apache.maven.plugins.annotations.Component import org.apache.maven.plugins.annotations.LifecyclePhase import org.apache.maven.plugins.annotations.Mojo +import org.apache.maven.plugins.annotations.Parameter +import org.apache.maven.project.MavenProject +import org.apache.maven.project.MavenProjectHelper +import org.codehaus.plexus.archiver.Archiver +import org.codehaus.plexus.archiver.jar.JarArchiver -import javax.inject.Inject - -@Mojo(name = 'generateStubs', defaultPhase = LifecyclePhase.PROCESS_RESOURCES) +@Mojo(name = 'generateStubs', defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true) @CompileStatic -@Deprecated -class GenerateStubsMojo extends ConvertMojo { +class GenerateStubsMojo extends AbstractMojo { - @Inject - GenerateStubsMojo(PathTranslator translator) { - super(translator) + @Parameter(defaultValue = '${project.build.directory}', readonly = true, required = true) + private File projectBuildDirectory + + @Parameter(property = 'stubsDirectory', defaultValue = '${project.build.directory}/mappings') + private File outputDirectory + + @Parameter(property = 'accurest.skip', defaultValue = 'false') + private boolean skip + + @Component + private MavenProjectHelper projectHelper; + + @Parameter(defaultValue = '${project}', readonly = true) + private MavenProject project + + @Component(role = Archiver.class, hint = "jar") + private JarArchiver jarArchiver; + + void execute() throws MojoExecutionException, MojoFailureException { + + if (skip) { + log.info("Skipping accurest execution: accurest.skip=${skip}") + return + } + + File stubJarFile = createStubJar(outputDirectory) + projectHelper.attachArtifact(project, 'jar', 'stubs', stubJarFile) } + + private File createStubJar(File stubsOutputDir) { + if (!stubsOutputDir.exists()) { + throw new MojoExecutionException("Stubs could not be found: $stubsOutputDir.\nPlease make sure that accurest:convert was invoked"); + } + String stubArchiveName = project.getBuild().getFinalName() + "-stubs.jar"; + File stubJarFile = new File(projectBuildDirectory, stubArchiveName); + + try { + jarArchiver.addDirectory(stubsOutputDir); + jarArchiver.setCompress(true); + jarArchiver.setDestFile(stubJarFile); + jarArchiver.createArchive(); + } catch (Exception e) { + throw new MojoFailureException('Exception while packaging.', e); + } + return stubJarFile + } + } diff --git a/src/main/groovy/io/codearte/accurest/maven/GenerateTestsMojo.groovy b/src/main/groovy/io/codearte/accurest/maven/GenerateTestsMojo.groovy index 14478e96c9..cef5361d73 100644 --- a/src/main/groovy/io/codearte/accurest/maven/GenerateTestsMojo.groovy +++ b/src/main/groovy/io/codearte/accurest/maven/GenerateTestsMojo.groovy @@ -9,7 +9,10 @@ import io.codearte.accurest.config.TestMode import org.apache.maven.plugin.AbstractMojo import org.apache.maven.plugin.MojoExecutionException import org.apache.maven.plugin.MojoFailureException -import org.apache.maven.plugins.annotations.* +import org.apache.maven.plugins.annotations.LifecyclePhase +import org.apache.maven.plugins.annotations.Mojo +import org.apache.maven.plugins.annotations.Parameter +import org.apache.maven.plugins.annotations.ResolutionScope import org.apache.maven.project.MavenProject import static java.lang.String.format @@ -42,7 +45,7 @@ class GenerateTestsMojo extends AbstractMojo { @Parameter private String nameSuffixForTests - @Component + @Parameter(defaultValue = '${project}', readonly = true) private MavenProject project @Parameter(property = 'accurest.skip', defaultValue = 'false') diff --git a/src/main/groovy/io/codearte/accurest/maven/RunMojo.groovy b/src/main/groovy/io/codearte/accurest/maven/RunMojo.groovy index abacda2d43..ea33e18ca8 100644 --- a/src/main/groovy/io/codearte/accurest/maven/RunMojo.groovy +++ b/src/main/groovy/io/codearte/accurest/maven/RunMojo.groovy @@ -9,7 +9,6 @@ import org.apache.maven.model.path.PathTranslator import org.apache.maven.plugin.AbstractMojo import org.apache.maven.plugin.MojoExecutionException import org.apache.maven.plugin.MojoFailureException -import org.apache.maven.plugins.annotations.Component import org.apache.maven.plugins.annotations.Mojo import org.apache.maven.plugins.annotations.Parameter import org.eclipse.aether.RepositorySystemSession @@ -49,7 +48,7 @@ class RunMojo extends AbstractMojo { private String stubsClassifier = 'stubs' - @Component + @Parameter(defaultValue = '${session}', readonly = true) private MavenSession mavenSession private final LocalStubRunner localStubRunner diff --git a/src/test/projects/spring-boot-groovy/pom.xml b/src/test/projects/spring-boot-groovy/pom.xml index 6562e9634a..d1adb9b706 100644 --- a/src/test/projects/spring-boot-groovy/pom.xml +++ b/src/test/projects/spring-boot-groovy/pom.xml @@ -81,6 +81,7 @@ convert + generateStubs generateTests @@ -115,30 +116,6 @@ --> - - maven-assembly-plugin - 2.6 - - - io.codearte.accurest - stubs-assembly-descriptor - 1.1 - - - - - package - - single - - - - - - stubs - - - diff --git a/src/test/projects/spring-boot-java/pom.xml b/src/test/projects/spring-boot-java/pom.xml index 84432a4b1c..3f1f635dbd 100644 --- a/src/test/projects/spring-boot-java/pom.xml +++ b/src/test/projects/spring-boot-java/pom.xml @@ -18,11 +18,14 @@ org.springframework.boot spring-boot-starter-web - + + org.springframework.boot + spring-boot-starter-test + test + ch.qos.logback logback-classic - 1.1.3 @@ -50,12 +53,6 @@ 2.3.0 test - - junit - junit - 4.12 - test - @@ -76,6 +73,7 @@ convert + generateStubs generateTests @@ -85,31 +83,6 @@ hello.BaseMockMvcTest - - maven-assembly-plugin - 2.6 - - - io.codearte.accurest - stubs-assembly-descriptor - 1.1 - - - - - package - - single - - - - - - stubs - - - -