Attach up-to-date stubs to maven (#1871)

* Move PluginUnitTest into integration-test phase

* Fix test resources

Contracts that used messageFrom, messageBody, messageHeaders were modified to use triggeredBy input as seen in 1999066c5a

* Attach stubs to maven project even when they are up-to-date; fixes gh-1860
This commit is contained in:
_NewAge
2023-02-20 13:57:24 +01:00
committed by GitHub
parent 65b463e487
commit d4967d8607
5 changed files with 73 additions and 15 deletions

View File

@@ -46,9 +46,41 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/PluginUnitTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<dependencies>
<!-- let JUnit vintage engine run JUnit 3 or JUnit 4 tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-vintage.version}</version>
</dependency>
</dependencies>
<configuration>
<reportFormat>plain</reportFormat>
<failIfNoTests>true</failIfNoTests>
<junitPlatformArtifactName>
org.junit.jupiter:junit-vintage-engine
</junitPlatformArtifactName>
<includes>
<include>**/PluginUnitTest.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>

View File

@@ -120,21 +120,22 @@ public class GenerateStubsMojo extends AbstractMojo {
throw new MojoExecutionException("Stubs could not be found: [" + this.outputDirectory.getAbsolutePath()
+ "] .\nPlease make sure that spring-cloud-contract:convert was invoked");
}
File stubsJarFile = getStubJarDestFile();
if (this.incrementalContractStubsJar && !inputFilesChangeDetected(outputDirectory, mojoExecution, session)) {
getLog().info("Nothing to generate - stubs jar is up to date");
return;
}
File stubsJarFile = createStubJar(this.outputDirectory);
else {
fillStubJar(this.outputDirectory, stubsJarFile);
}
this.projectHelper.attachArtifact(this.project, "jar", this.classifier, stubsJarFile);
}
private File createStubJar(File stubsOutputDir) throws MojoFailureException, MojoExecutionException {
if (!stubsOutputDir.exists()) {
throw new MojoExecutionException("Stubs could not be found: [" + stubsOutputDir.getAbsolutePath()
+ "] .\nPlease make sure that spring-cloud-contract:convert was invoked");
}
private File getStubJarDestFile() {
String stubArchiveName = this.projectFinalName + "-" + this.classifier + ".jar";
File stubsJarFile = new File(this.projectBuildDirectory, stubArchiveName);
return new File(this.projectBuildDirectory, stubArchiveName);
}
private void fillStubJar(File stubsOutputDir, File stubsJarFile) throws MojoFailureException {
String[] excludes = excludes();
getLog().info(
"Files matching this pattern will be excluded from " + "stubs generation " + Arrays.toString(excludes));
@@ -149,7 +150,6 @@ public class GenerateStubsMojo extends AbstractMojo {
catch (Exception e) {
throw new MojoFailureException("Exception while packaging " + this.classifier + " jar.", e);
}
return stubsJarFile;
}
private boolean stubsOutputMissing(File stubsOutputDir) {

View File

@@ -63,10 +63,18 @@ public abstract class AbstractMojoTest {
return new File(this.tmpFolder, name);
}
protected void executeMojo(File baseDir, String goal, Xpp3Dom... parameters) throws Exception {
protected MavenSession prepareMavenSession(File baseDir) throws Exception {
MavenProject mavenProject = rule.readMavenProject(baseDir);
MavenSession mavenSession = rule.newMavenSession(mavenProject);
rule.executeMojo(mavenSession, mavenProject, goal, parameters);
return rule.newMavenSession(mavenProject);
}
protected void executeMojo(File baseDir, String goal, Xpp3Dom... parameters) throws Exception {
MavenSession mavenSession = prepareMavenSession(baseDir);
executeMojo(mavenSession, goal, parameters);
}
protected void executeMojo(MavenSession mavenSession, String goal, Xpp3Dom... parameters) throws Exception {
rule.executeMojo(mavenSession, mavenSession.getCurrentProject(), goal, parameters);
}
protected void assertFilesPresent(File file, String name) {

View File

@@ -18,8 +18,11 @@ package org.springframework.cloud.contract.maven.verifier;
import java.io.File;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.junit.Ignore;
import org.junit.Test;
import static java.nio.charset.Charset.defaultCharset;
@@ -27,8 +30,6 @@ import static org.apache.commons.io.FileUtils.readFileToString;
import static org.apache.maven.plugin.testing.MojoParameters.newParameter;
import static org.assertj.core.api.BDDAssertions.then;
// TODO: Move this to integration tests
@Ignore("Testing harness uses an old version of maven")
public class PluginUnitTest extends AbstractMojoTest {
@Test
@@ -329,4 +330,21 @@ public class PluginUnitTest extends AbstractMojoTest {
"target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/common_repo_with_inclusion/reward_rules/src/main/resources/contracts/reward_rules/rest/admin/V1Test.java");
}
@Test
public void shouldAttachUpToDateStubs() throws Exception {
File basedir = getBasedir("generatedStubs");
MavenSession firstSession = prepareMavenSession(basedir);
MavenProject firstProject = firstSession.getCurrentProject();
Artifact stubArtifact = new DefaultArtifact(firstProject.getGroupId(), firstProject.getArtifactId(),
firstProject.getVersion(), "", "jar", "stubs", firstProject.getArtifact().getArtifactHandler());
executeMojo(firstSession, "generateStubs");
then(firstSession.getCurrentProject().getAttachedArtifacts()).contains(stubArtifact);
MavenSession secondSession = prepareMavenSession(basedir);
executeMojo(secondSession, "generateStubs");
then(secondSession.getCurrentProject().getAttachedArtifacts()).contains(stubArtifact);
}
}