You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by Henning Schmiedehausen <he...@schmiedehausen.org> on 2023/09/02 06:16:09 UTC

Remove / reset compileSourceRoots list?

I have a mixed kotlin/java multi-module project that uses src/main/java for
the java code and src/main/kotlin for kotlin code.

For the java-only projects, everything is fine. sourceDirectory points to
src/main/java, testSourceDirectory points to src/test/java.

We activate a specific profile that turns on the kotlin compiler if
src/main/kotlin is present. For modules that have both kotlin and java
code, this works fine as well. We add src/main/kotlin to the source roots
and src/test/kotlin to the test roots (using the build-helper plugin).

However, for kotlin-only projects (where no src/main/java and src/test/java
exists), we get a build warning:  "Source root doesn't exist:
.../src/main/java" (e.g. from the kotlin compiler).

As we activate the compiler from a profile, we can not set the initial
sourceDirectory or testSourceDirectory (overriding those values is not
supported in a profile).

Is there a way to clear the list of source roots in the maven project? I
looked into the code and what the build helper does and tried using
project.getCompileSourceRoots().clear() which works only partially.

Is there an official way to clear / set the list of compileSourceRoots and
testSourceRoots from a plugin that can be used from a profile?

-h

Re: Remove / reset compileSourceRoots list?

Posted by Henning Schmiedehausen <he...@schmiedehausen.org>.
On Sun, Sep 3, 2023 at 11:19 PM Guillaume Nodet <gn...@apache.org> wrote:

> Le sam. 2 sept. 2023 à 08:16, Henning Schmiedehausen <
> henning@schmiedehausen.org> a écrit :
>
> > I have a mixed kotlin/java multi-module project that uses src/main/java
> for
> > the java code and src/main/kotlin for kotlin code.
> >
> > For the java-only projects, everything is fine. sourceDirectory points to
> > src/main/java, testSourceDirectory points to src/test/java.
> >
> > We activate a specific profile that turns on the kotlin compiler if
> > src/main/kotlin is present. For modules that have both kotlin and java
> > code, this works fine as well. We add src/main/kotlin to the source roots
> > and src/test/kotlin to the test roots (using the build-helper plugin).
> >
> > However, for kotlin-only projects (where no src/main/java and
> src/test/java
> > exists), we get a build warning:  "Source root doesn't exist:
> > .../src/main/java" (e.g. from the kotlin compiler).
> >
>
> Why is the kotlin compiler given a pointer to a java source root ?
> The java compiler has a property compileSourceRoots which defaults to the
> project.compileSourceRoots but that can be overridden in such cases.
> It seems the kotlin-maven-plugin has a sourceDirs property which you could
> set to only the kotlin one.
>

Yes. It's complicated. Basically, for mixed source trees, the kotlin
compiler needs to be able to read the java and the kotlin source files,
otherwise you could not write kotlin code that references java code in the
same module. See
https://kotlinlang.org/docs/maven.html#compile-kotlin-and-java-sources for
details.


>
>
> >
> > As we activate the compiler from a profile, we can not set the initial
> > sourceDirectory or testSourceDirectory (overriding those values is not
> > supported in a profile).
> >
>
> Have you tried setting those values with properties and override the
> properties in the profile ?
>

Oh, that is an interesting approach. I have not actually tried that.


>
>
> >
> > Is there a way to clear the list of source roots in the maven project? I
> > looked into the code and what the build helper does and tried using
> > project.getCompileSourceRoots().clear() which works only partially.
> >
>
> Why partially ?
>

Because it works for a bit (it seems in the same lifecycle phase) and then
a bit later, plugin execution has the old values (pre calling "clear()")
again. There is some cloning of the project element somewhere, so the
clear() call removes the directories from the cloned Project object but can
not affect the actual source.


>
>
> >
> > Is there an official way to clear / set the list of compileSourceRoots
> and
> > testSourceRoots from a plugin that can be used from a profile?
> >
>
> Not sure about that... ;-)
>

Thanks for looking into this.

-h

Re: Remove / reset compileSourceRoots list?

Posted by Guillaume Nodet <gn...@apache.org>.
Le sam. 2 sept. 2023 à 08:16, Henning Schmiedehausen <
henning@schmiedehausen.org> a écrit :

> I have a mixed kotlin/java multi-module project that uses src/main/java for
> the java code and src/main/kotlin for kotlin code.
>
> For the java-only projects, everything is fine. sourceDirectory points to
> src/main/java, testSourceDirectory points to src/test/java.
>
> We activate a specific profile that turns on the kotlin compiler if
> src/main/kotlin is present. For modules that have both kotlin and java
> code, this works fine as well. We add src/main/kotlin to the source roots
> and src/test/kotlin to the test roots (using the build-helper plugin).
>
> However, for kotlin-only projects (where no src/main/java and src/test/java
> exists), we get a build warning:  "Source root doesn't exist:
> .../src/main/java" (e.g. from the kotlin compiler).
>

Why is the kotlin compiler given a pointer to a java source root ?
The java compiler has a property compileSourceRoots which defaults to the
project.compileSourceRoots but that can be overridden in such cases.
It seems the kotlin-maven-plugin has a sourceDirs property which you could
set to only the kotlin one.


>
> As we activate the compiler from a profile, we can not set the initial
> sourceDirectory or testSourceDirectory (overriding those values is not
> supported in a profile).
>

Have you tried setting those values with properties and override the
properties in the profile ?


>
> Is there a way to clear the list of source roots in the maven project? I
> looked into the code and what the build helper does and tried using
> project.getCompileSourceRoots().clear() which works only partially.
>

Why partially ?


>
> Is there an official way to clear / set the list of compileSourceRoots and
> testSourceRoots from a plugin that can be used from a profile?
>

Not sure about that... ;-)


>
> -h
>


-- 
------------------------
Guillaume Nodet