What I learned today — 19 January 2018

Niel de Wet
2 min readJan 21, 2018

--

Much of this Friday was spent refactoring the maven configuration of one of our projects after migrating a module to a new repository and a new parent module. There is an excellent guide on how to move files from one git repo to another while maintaining history at gbayer.com.

I spent much time refactoring the maven plugin management in the parent pom.xml file and learned that the inheritance between the plugin management element and the plugin itself is really efficient.

In the parent module you may have something like this Kotlin plugin defined:

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
</sourceDirs>
</configuration>
</execution>
</executions>

<!-- https://blog.jetbrains.com/kotlin/2016/12/kotlin-1-0-6-is-here/ -->
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>

Essentially all that the above configuration does is to look for Kotlin files to compile in the src/main/java and src/test/java directories during the process-sources and test-compile phases, respectively. It also adds the kotlin-spring compiler plugin which transparently marks all classes annotated with Spring stereotypes as open.

Now any child module can get all of the above by simply specifying the plugin’s groupId and artifactId.

<build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Specializing the parent module’s configuration is also very simple:

<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/wsimport</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>

As long as the changed section can be identified (in this case, using the <id/> attribute), you only need to specify the changed attribute. Everything else is merged.

--

--

No responses yet