Skip to content

update: update Compile Kotlin and Java sources in Maven #4785

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

koshachy
Copy link
Collaborator

@koshachy koshachy commented Apr 2, 2025

No description provided.

Copy link
Contributor

github-actions bot commented May 3, 2025

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale label May 3, 2025
@koshachy koshachy removed the Stale label May 9, 2025
@ALikhachev ALikhachev assigned ALikhachev and unassigned antohaby May 27, 2025
@koshachy koshachy force-pushed the update-kotlin-maven branch from ec066ba to b2c8aae Compare May 28, 2025 11:22
@ALikhachev
Copy link
Member

I would suggest additionally highlighting that in the setup Kotlin + Java, we need to let Kotlin compiler know about the Java sources, so that's why we configure ${project.basedir}/src/main/java as a source directory for Kotlin. That is required for Kotlin -> Java references.

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<extensions>true</extensions> <!-- You can set this option
to automatically take information about lifecycles -->
<extensions>true</extensions>
Copy link
Member

@ALikhachev ALikhachev Jun 5, 2025

Choose a reason for hiding this comment

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

It seems that actually this <extensions>true</extensions> does not make any sense. According to the previous comments, it could help avoid some configuration, but that does not appear to be true in Kotlin + Java in this particular configuration.

However, if we modify the example to

    <plugins>
      <!-- Kotlin compiler plugin -->
      <plugin>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-maven-plugin</artifactId>
        <version>${kotlin.version}</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <configuration>
              <sourceDirs>
                <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                <sourceDir>${project.basedir}/src/main/java</sourceDir>
              </sourceDirs>
            </configuration>
          </execution>
          <execution>
            <id>default-test-compile</id>
            <phase>test-compile</phase>
            <configuration>
              <sourceDirs>
                <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
                <sourceDir>${project.basedir}/src/test/java</sourceDir>
              </sourceDirs>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>

we can completely skip the Java plugin configuration and it would just work, thanks to the <extensions> setup. This configuration indeed seems much easier and shows benefit of extensions

Note that I changed execution IDs in this example.

I would suggest introducing something similar to how we show Kotlin/Groovy scripts, aka "tabs" for showing configuration with <extensions>true</extensions> and without it.

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<extensions>true</extensions> <!-- You can set this option
to automatically take information about lifecycles -->
<extensions>true</extensions>
Copy link
Member

Choose a reason for hiding this comment

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

Oh, interesting, I tried just copy-pasting the setup (both before any changes and from this PR) on top of pure Kotlin setup – it leads to incorrect configuration when Java code can see Kotlin code, but Kotlin code cannot see Java.

Either removing the extensions setup or removing of the following configuration helps.

<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

With the abovementioned change of execution id, it works fine. That's because setup from extensions creates executions default-compile and default-test-compile

Copy link
Contributor

github-actions bot commented Jul 6, 2025

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

Copy link
Contributor

github-actions bot commented Aug 7, 2025

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

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.

5 participants