awhitford / lombok.maven

Maven Plugin for Project Lombok
http://projectlombok.org/
MIT License
110 stars 36 forks source link

Delomboking Fails with Multiple Source Folders #103

Open ncovercash opened 2 years ago

ncovercash commented 2 years ago

Heya!

I'm working on a Spring Boot application and, as part of it, generate some code based on an API schema (with openapi-generator). The templates I have for this work great and take advantage of @Data and some other Lombok annotations, as does my main source code.

With openapi-generator, it dumps all of its created source code into /target/generated-sources/api (and adds it to the build path). My normal sources are in /src/main/java, however, so I cannot delombok all of this at once.

Here is my attempted Maven configuration:

<plugin>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok-maven-plugin</artifactId>
  <version>${lombok-maven-plugin.version}</version>
  <configuration>
    <addOutputDirectory>false</addOutputDirectory>
  </configuration>
  <executions>
    <execution>
      <id>delombok-openapi</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>delombok</goal>
      </goals>
      <configuration>
        <sourceDirectory>${project.build.directory}/generated-sources/api</sourceDirectory>
        <outputDirectory>${project.build.directory}/generated-sources/delombok</outputDirectory>
      </configuration>
    </execution>
    <execution>
      <id>delombok-main</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>delombok</goal>
      </goals>
      <configuration>
        <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
        <outputDirectory>${project.build.directory}/generated-sources/delombok/src/main/java</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

This will successfully delombok the generated API sources (delombok-openapi), however, as soon as it attempts to delombok the src/main/java sources (delombok-main), it cannot find any of the lombok-generated methods (getters, setters, etc) that should have been generated in the delombok-openapi step (or at least generated at that point, since either delombok execution should be able to generate all the setters/etc?).

Without this plugin's executions my build works fine, so the code itself is (somehow) working. Both folders of code rely on each other and both use Lombok, so I'm not sure why it's so picky. Swapping the execution order changes nothing (it's still main with the issues).

ncovercash commented 2 years ago

If anyone else has this issue, I got around it by invoking delombok directly, the old-fashioned way:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <executable>java</executable>
    <arguments>
      <argument>-classpath</argument>
      <classpath />
      <argument>lombok.launch.Main</argument>
      <argument>delombok</argument>
      <argument>--verbose</argument>
      <argument>--nocopy</argument>
      <argument>${project.build.directory}/generated-sources/api</argument>
      <argument>${project.basedir}/src/main/java</argument>
      <argument>-d</argument>
      <argument>${project.build.directory}/generated-sources/delombok</argument>
    </arguments>
  </configuration>
  <executions>
    <execution>
      <id>delombok</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>exec</goal>
      </goals>
    </execution>
  </executions>
</plugin>

To add multiple source folders, you can just add additional arguments. --nocopy prevents it from copying extra things like pom.xml or other stuff and -d specifies the output folder.

nils-christian commented 1 year ago

We would need this functionality as well, so +1 from me for this feature request (and thank you for the workaround).

nils-christian commented 1 year ago

Hi,

I added a PR for this one: #157

Please note that this is an incompatible API change regarding the configuration (as can be seen in the example pom.xml).