Skip to content

Conversation

@andriy-dmytruk
Copy link
Contributor

Switch JSON schema to generate Maven mojo based on micronaut-build-plugin-sourcegen.

The generated plugin will:

  • Have the same properties defined
  • Have the javadoc copied as defined in this repo
  • Add the generated files as build sources

This would simplify support, as additional properties would require no manual changes in the plugins. Some properties would already be added in this PR: micronaut-projects/micronaut-json-schema#163.

Current support

Maven Gradle Plugingen
inputURL inputUrl inputURL
inputFile inputFile inputFile
inputDirectory inputDirectory inputDirectory
language default=JAVA lang default=JAVA language default=JAVA
outputDirectory outputDirectory outputDirectory
outputPackageName default=io.micronaut.jsonschema outputPackageName default=io.micronaut.jsonschema outputPackageName default=io.micronaut.jsonschema
outputFileName outputFileName outputFileName
acceptedUrlPatterns acceptedUrlPatterns acceptedUrlPatterns
enabled default=false enabed default=true enabled default=false/true

@alvarosanchez would appreciate your feedback

@andriy-dmytruk andriy-dmytruk force-pushed the andriy/json-schema-plugin-sourcegen branch from beb4ee2 to ec5fa18 Compare April 1, 2025 18:55
*/
@Mojo(name = JsonSchemaGeneratorMojo.MOJO_NAME, defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class JsonSchemaGeneratorMojo extends AbstractMicronautMojo {
public class JsonSchemaGeneratorMojo extends AbstractJsonSchemaGeneratorMojo {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the generated AbstractJsonSchemaGeneratorMojo won't extend from AbstractMicronautMojo, then we need to check what is in the latter that we are missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does extend from the micronaut mojo. The whole generated file is:

/**
 * A configuration class for the JSON schema source generator, encapsulating the configuration
 * properties related to the input and output paths, file names, and URL.
 */
public abstract class AbstractJsonSchemaGeneratorMojo extends AbstractMicronautMojo {
  /**
   * The common prefix for Mojo properties.
   */
  protected static final String PROPERTY_PREFIX = "micronaut.jsonschema.generator";

  @Parameter(
      defaultValue = "${project}",
      required = true,
      readonly = true
  )
  protected MavenProject project;

  /**
   * Determines if this mojo must be executed. The value is true if the mojo is enabled.
   */
  @Parameter(
      property = "micronaut.jsonschema.generator.enabled",
      defaultValue = "false"
  )
  protected boolean enabled;

  /**
   * The URL of the JSON schema file to be downloaded and processed.
   */
  @Parameter(
      property = "micronaut.jsonschema.generator.input.url"
  )
  protected String inputURL;

  /**
   * The JSON schema file, typically ending in {@code .schema.json}.
   */
  @Parameter(
      property = "micronaut.jsonschema.generator.input.file"
  )
  protected File inputFile;

  /**
   * The path to the folder where the input files (JSON schemas) are located.
   * Each of the files in the directory will be used for generation.
   */
  @Parameter(
      property = "micronaut.jsonschema.generator.input.directory"
  )
  protected File inputDirectory;

  /**
   * The package name to be applied to the generated source files
   * This field is optional and can be {@code null} if no package name is needed.
   */
  @Parameter(
      defaultValue = "io.micronaut.jsonschema",
      property = "micronaut.jsonschema.generator.output.package.name"
  )
  protected String outputPackageName;

  /**
   * The name of the file where the generated source code will be written
   * if there is a single output source..
   */
  @Parameter(
      property = "micronaut.jsonschema.generator.output.file.name"
  )
  protected String outputFileName;

  /**
   * The programming language to generate the output types in..
   */
  @Parameter(
      defaultValue = "JAVA",
      property = "micronaut.jsonschema.generator.language"
  )
  protected Language language;

  /**
   * The URL patterns that are allowed.
   * Used to filter or validate input resources specified by URL.
   * URLs matching at least one pattern will be accepted. The default
   * value is {@code "^https://.* /.*.json"}.
   */
  @Parameter
  protected List<String> acceptedUrlPatterns;

  /**
   * The path where generated source files will be saved
   * The path should be a valid, writable directory path.
   */
  @Parameter(
      required = true
  )
  protected File outputDirectory;

  /**
   * Internal output path for Java sources.
   */
  protected abstract File getJavaOutputDirectory();

  /**
   * Internal output path for Groovy sources.
   */
  protected abstract File getGroovyOutputDirectory();

  /**
   * Internal output path for Kotlin sources.
   */
  protected abstract File getKotlinOutputDirectory();

  JsonSchemaGeneratorTask.Language convertLanguage(Language value) {
    if (value == null) {
      return null;
    } else {
      return JsonSchemaGeneratorTask.Language.valueOf(value.name());
    }
  }

  /**
   * Main execution of AbstractJsonSchemaGenerator Mojo.
   */
  public void execute() throws MojoExecutionException, MojoFailureException {
    if (!this.enabled) {
      this.getLog().debug("AbstractJsonSchemaGeneratorMojo is disabled");
    } else {
      this.project.addCompileSourceRoot(this.getJavaOutputDirectory().getAbsolutePath());
      this.project.addCompileSourceRoot(this.getGroovyOutputDirectory().getAbsolutePath());
      this.project.addCompileSourceRoot(this.getKotlinOutputDirectory().getAbsolutePath());
      try {
      JsonSchemaGeneratorTask.Language languageParam = this.convertLanguage(this.language);
      JsonSchemaGeneratorTask task = new io.micronaut.jsonschema.generator.plugin.JsonSchemaGeneratorTask(this.inputURL, this.inputFile, this.inputDirectory, this.outputPackageName, this.outputFileName, languageParam, this.acceptedUrlPatterns, this.outputDirectory, this.getJavaOutputDirectory(), this.getGroovyOutputDirectory(), this.getKotlinOutputDirectory());
      task.generate();

      } catch (IllegalArgumentException e0) {throw new org.apache.maven.plugin.MojoFailureException("Invalid configuration for AbstractJsonSchemaGenerator", e0);

      } catch (Exception e1) {throw new org.apache.maven.plugin.MojoExecutionException("Failed to run AbstractJsonSchemaGenerator", e1);

      }}
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do you specify that? I don't see it in PluginGenerationTrigger

Copy link
Contributor Author

@andriy-dmytruk andriy-dmytruk Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the micronaut parent, PluginGenerationTrigger annotation has useMicronaut with default true.

But the properties here are specified in the json schema repo: https://github.com/micronaut-projects/micronaut-json-schema/pull/165/files#diff-c72e08c7f6ac635067a1a3be17d3b69bb383008b6da3afdfd710e1b97fe5633c

@andriy-dmytruk andriy-dmytruk force-pushed the andriy/json-schema-plugin-sourcegen branch from 9fc0ba7 to b2592eb Compare April 22, 2025 18:26
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
1 New Blocker Issues (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

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.

2 participants