(If you want more freedom take a look at gradle)
Writing our own plugin is time consuming but we can easily extend maven capabilities with executing groovy code during the build.
All we need to do is add gmaven plugin into pom.xml.
Let's see an example that will make our maven build tell us which operating system we are using:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>pl.mjedynak</groupId> | |
<artifactId>gmaven</artifactId> | |
<version>1.0</version> | |
<packaging>jar</packaging> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.codehaus.gmaven</groupId> | |
<artifactId>gmaven-plugin</artifactId> | |
<version>1.4</version> | |
<executions> | |
<execution> | |
<phase>validate</phase> | |
<goals> | |
<goal>execute</goal> | |
</goals> | |
<configuration> | |
<source> | |
import org.apache.commons.lang3.SystemUtils | |
if (SystemUtils.IS_OS_WINDOWS) { | |
println("running on Windows") | |
} else { | |
println("running on Unix") | |
} | |
</source> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-lang3</artifactId> | |
<version>3.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
If we don't want to inline our script we can specify the file that contains it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<source>${pom.basedir}/src/main/script/whichOS.groovy</source> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<plugin> | |
<groupId>org.codehaus.gmaven</groupId> | |
<artifactId>gmaven-plugin</artifactId> | |
<version>1.4</version> | |
<executions> | |
<execution> | |
<phase>validate</phase> | |
<goals> | |
<goal>execute</goal> | |
</goals> | |
<configuration> | |
<source> | |
project.properties['findbugsSettingsDirectory'] = 'c:/findbugs' | |
</source> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>findbugs-maven-plugin</artifactId> | |
<version>2.3.3</version> | |
<executions> | |
<execution> | |
<phase>compile</phase> | |
<goals> | |
<goal>check</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<includeFilterFile>${findbugsSettingsDirectory}/findbugs.xml</includeFilterFile> | |
</configuration> | |
</plugin> |