I'm working on a Kotlin project that uses Maven for build management and automation. And I use the command mvn verify
to build and compile the project and run the tests.
I want when the project compiles, it not only fails when it encounters an error, but also treats warnings as errors and doesn't compile if there are any warnings. I searched the internet and found a possible solution: add the argument -Werror
to the artifact's configuration kotlin-maven-plugin
. Like this:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<configuration>
<experimentalCoroutines>enable</experimentalCoroutines>
<jvmTarget>1.8</jvmTarget>
<args>
<arg>
-Werror
</arg>
</args>
</configuration>
...
But when I do mvn verify
I get the following error:
Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.1.51:compile (compile) on project fridge: Invalid argument: -Werror -> [Help 1]
Interestingly, if I do Build > Rebuild Project from the IntelliJ menu, I don't get that error and the build fails when there is a warning (as I want). Why can that happen and what can I do to fix it?
I'm using:
- Apache Maven 3.5.2 (Java version 1.8.0_151)
- Kotlin Library 1.1.51
- IntelliJ IDEA Community 2017.2
- Kotlin Plugin for IntelliJ 1.2.10-release-IJ2017.2-1
The problem was that the version of Kotlin we were using and the version of the IntelliJ plugin weren't the same, and the argument
-Werror
wasn't included until a more modern version of Kotlin. Doingmvn verify
from the command line used Kotlin 1.1.51 (which doesn't support-Werror
); while doing Build > Rebuild Project used 1.2.10 (which does allow-Werror
).The solution was, after verifying that the project was not going to be affected by the change, to update the version of the Kotlin library to a more modern one (1.2.21) and incidentally also update the IntelliJ Kotlin plugin to use the same version.
To update the version of Kotlin used in the project, it is indicated in the pom file:
And all new libraries and dependencies will be downloaded and updated automatically when you compile the project.