How to Enable Auto-Generate Sources in IntelliJ for Spring Boot, MapStruct, and Annotation Processors

Enable Auto-Generate Sources in IntelliJ

IntelliJ IDEA Community Edition (CE) is a powerful IDE for Java development, but when compared to Eclipse, it lacks automatic source generation for tools like MapStruct and annotation processors such as springdoc.openapi.maven.generator. In Eclipse, modifying a MapStruct mapper interface and saving the file triggers automatic generation of the related implementation, while in IntelliJ, this requires manual steps like running mvn generate-sources or mvn clean install.

If you’re using IntelliJ IDEA CE 2024.3.1.1, Maven 3.9.9 (bundled), Spring Boot 3.4.1, and Java 21, this guide will help you configure IntelliJ to handle automatic source generation.


Why Auto-Generate Sources Matters

When working with tools like MapStruct for mapping data or SpringDoc for generating OpenAPI documentation, automatic source generation saves significant time and reduces manual effort. For instance:

  • MapStruct generates mappers based on interfaces.
  • SpringDoc generates API documentation from annotations in your Spring Boot code.

Without automatic generation, every change to your mappers or annotations requires running Maven commands manually, disrupting your workflow.


Steps to Enable Auto-Generate Sources in IntelliJ

Follow these steps to configure IntelliJ for automatic source generation:

1. Enable Annotation Processors

IntelliJ does not enable annotation processing by default. To turn it on:

  1. Go to File > Settings > Build, Execution, Deployment > Compiler > Annotation Processors.
  2. Select Enable annotation processing.
  3. Set the Processor path to point to your Maven local repository or project-specific dependencies, if necessary.
  4. Apply the changes and rebuild the project.

2. Enable Automatic Build

To ensure the IDE builds the project automatically when changes are made:

  1. Open File > Settings > Advanced Settings.
  2. Look for the Build process section and check Build project automatically.
  3. Press Ctrl+Shift+A (Command+Shift+A on Mac) to open the Actions menu, and search for Registry.
  4. In the Registry, enable the option compiler.automake.allow.when.app.running.

3. Configure Maven for Automatic Source Generation

IntelliJ IDEA CE does not trigger generate-sources automatically. To achieve this:

  1. Open the Maven Tool Window (usually on the right sidebar).
  2. In the lifecycle section, bind generate-sources to the compile phase:
  • Open your pom.xml.
  • Add the following configuration under <build> if not already present:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.springdoc</groupId>
                        <artifactId>springdoc-openapi-maven-plugin</artifactId>
                        <version>${springdoc.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>
  1. Run mvn compile or mvn install to verify that the sources are generated.

4. Use IntelliJ Plugins for Enhanced Support

Although IntelliJ IDEA CE lacks built-in tools like those in the Ultimate Edition, you can enhance its capabilities with plugins:

  • Install Lombok Plugin: If using Lombok, this ensures proper handling of annotations.
  • Install MapStruct Support Plugin: Provides better support for MapStruct and its generated code.

5. Monitor File Changes and Trigger Compilation

If auto-build doesn’t trigger source generation, use file watchers:

  1. Install the File Watchers Plugin from the IntelliJ Plugin Marketplace.
  2. Configure a watcher to run mvn generate-sources whenever files in the src/main/java directory are modified.

Final Thoughts

While IntelliJ IDEA CE doesn’t offer the same out-of-the-box experience as Eclipse for auto-generating sources, these configurations will streamline your workflow for projects using MapStruct, SpringDoc, and other annotation processors. If your project heavily relies on these tools, consider evaluating IntelliJ IDEA Ultimate, which provides enhanced features like built-in Maven lifecycle integration.

By implementing the above steps, you can bridge the gap and enjoy a more efficient development experience in IntelliJ.


Keywords

  • IntelliJ auto-generate sources
  • MapStruct implementation IntelliJ
  • SpringDoc OpenAPI IntelliJ
  • IntelliJ annotation processor configuration
  • Maven generate sources IntelliJ
  • MapStruct auto generation

By following this guide, you can save time and boost productivity in your Java development workflow using IntelliJ IDEA Community Edition.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *