Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement prototype for annotation processing #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

schlagi123
Copy link

@schlagi123 schlagi123 commented Sep 12, 2022

Hi Martin,

I have create a prototype for an annotation processor. The changes are in the spring and core (openapi-processor/openapi-processor-core#100) repository.

One open problem is currently the logging. The Project https://github.com/Cosium/annotation-processor-logger shows an solution to use SLF4J. But if in the future a annotation processor is only used or in a special repository for it exists, it is also possible to change the whole logging process to the javax.annotation.processing.Messager class.

In the following Attachment you can see an example of how to use it:
example.zip

@hauner
Copy link
Member

hauner commented Sep 22, 2022

Thanks! :-)

@hauner
Copy link
Member

hauner commented Nov 19, 2022

I have been playing around with this. Your code shows that it is working. There is one issue that breaks it and I don't see a good solution for it. The issue is to detect changes of the openapi yaml files or the mapping yaml.

Let use gradle as an example.

  • gradle will run java compilation
  • it will compile the annotated class
  • and trigger the annotation processor

now let's change the openapi or mapping file

  • running gradle nothing happens
  • compileJava will just say up-to-date
  • and gradle will not re-run the processor

I can trick gradle into running the processor with some funny task that uses api/mapping as input and generates a new (and different) annotated class to trigger the annotation processor (rec-creating the file with the same source didn't work).

// build.gradle
// ...
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation (libs.processor.annotations)

    annotationProcessor (libs.processor.spring)
    annotationProcessor (libs.logback12)
//    annotationProcessor (libs.slf4j.nop)
// ...
}


sourceSets {
    main {
        java {
            srcDir "$buildDir/api"
        }
    }
}

tasks.register ("api") {
    it.inputs.dir ('src/api')
    it.outputs.file ("$buildDir/api/ApiConfiguration.java")

    it.doLast {
        mkdir ("$buildDir/api")
        file ("$buildDir/api/ApiConfiguration.java").text = """\
import io.openapiprocessor.annotations.SpringApi;

@SpringApi
public class ApiConfiguration {
    private static final String lastModified = "${Instant.now ().toString ()}";
}
"""
    }
}

compileJava {
    options.compilerArgs += [
        "-Aio.openapiprocessor.project.root=${projectDir}",
    ]
}

compileJava.dependsOn (tasks.named ("api"))

Not sure if this would be possible with maven.

I had hoped to minimize the required configuration (just adding the annotation processor dependencies) and that it would make the build plugin obsolete (less stuff to maintain).

Considering that it need additional build logic to run the annotation processor I don't see any benefit over the current plugin solution.

Unfortunately a disappointing outcome... :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants