You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In projects, it's usually useful to generate sources as a build step. For that a Maven or/and Gradle plugin for that was a industry standard.
But, I propose to write a a annotation processor instead. In that way the code will be generated at compile time at any tool: Maven, Gradle, SBT or even IDE like Intellij and Eclipse.
It might look like that (annotation):
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface GeneratePact {
/** * A consumer name that must be given. * * @return a consumer name */Stringvalue();
/** * A consumer name that must be given. This is an alias for {@link #value()} * * @return a consumer name */StringconsumerName() defaultEMPTY_VALUE;
/** * A package that should be taken as a root for generated code. * <p> * If not given {@link #packageClass()} will be used instead. * @return a package of generated code */StringpackageRoot() defaultEMPTY_VALUE;
/** * If not given annotated class will be taken as a base for * getting a package * * @return a class whose package will be taken as generated code root */Class<?> packageClass() defaultGeneratePact.class;
/** * A supplier for a PactGenerator. Given supplier must be instantiable * at compile time. * <p> * Will be instantiated by {@link #beanFactory()} * * @return a supplier for Pact Generator */Class<? extendsSupplier<PactGenerator>> pactGenerator()
defaultDefaultPactGeneratorSupplier.class;
/** * A supplier for Jackson's ObjectMapper. Given supplier must be instantiable * at compile time. * <p> * Will be instantiated by {@link #beanFactory()} * @return a supplier for ObjectMapper */Class<? extendsSupplier<ObjectMapper>> objectMapper()
defaultDefaultObjectMapper.class;
/** * A bean factory that will create beans like PactGenerator and others. * Bean factory must be instantiable at compile time. * * @return a supplier for bean factory */Class<? extendsSupplier<BeanFactory>> beanFactory()
defaultReflectionBeanFactory.class;
}
And usage:
// generates resources to package of CompileTimeGeneratorTest@PactGenerate("consumer-name")
publicclassCompileTimeGeneratorTest {
@TestpublicvoidgeneratePactFilesAtCompile() {
URLfile = getClass().getResource("example.pact");
assertThat(Paths.get(url.toURI())).exists();
}
}
The text was updated successfully, but these errors were encountered:
In projects, it's usually useful to generate sources as a build step. For that a Maven or/and Gradle plugin for that was a industry standard.
But, I propose to write a a annotation processor instead. In that way the code will be generated at compile time at any tool: Maven, Gradle, SBT or even IDE like Intellij and Eclipse.
It might look like that (annotation):
And usage:
The text was updated successfully, but these errors were encountered: