Skip to content

Instantly share code, notes, and snippets.

@spilth
Last active August 29, 2015 14:24
Show Gist options
  • Save spilth/9bc2cc1b48d900ad88d9 to your computer and use it in GitHub Desktop.
Save spilth/9bc2cc1b48d900ad88d9 to your computer and use it in GitHub Desktop.
Maven Cookbook

Default Maven Goal

Want to just type mvn on the command-line instead of mvn clean verify every time?

Add the following to your pom.xml:

<project>
    <build>
        <defaultGoal>clean verify</defaultGoal>
    </build>
</project>

Run a Single Test

To run a single test from the command-line, use:

$ mvn -Dtest=ClassName#methodName test

Disable a Plugin in a Profile

In the execution element to assign the goal used by the plug-in to the phase none.

<profiles>
    <profile>
        <id>skipStuff</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.cj.jshintmojo</groupId>
                    <artifactId>jshint-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>lint</goal>
                            </goals>
                            <phase>none</phase>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Reset a Plugin's Configuration within a Profile

Sometimes you want to use a plugin without inheriting the configuration from the non-profile plugin configuration.

To do this, set the combine.self attribute to override in the plugin's configuration element.

<profiles>
     <profile>
         <id>frenemy</id>
         <build>
             <plugins>
                 <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-surefire-plugin</artifactId>
                     <version>2.18.1</version>
                     <configuration combine.self="override">
                         <includes>
                             <include>**/foo/frenemy/**/*.java</include>
                         </includes>
                     </configuration>
                 </plugin>
             </plugins>
         </build>
     </profile>
</profiles>

Get the list of Dependencies being used

$ dependency:tree

Get the list of Maven Plug-ins being used

$ mvn dependency:resolve-plugins

Convert Maven project to Gradle

Run gradle init in the same directory as your pom.xml.

This will generate build.gradle and gradle.settings files.

Override Plugin Configuration from Command-Line

Define a Custom Property with Default Value

<project>
    <properties>
        <integrationForkCount>8</integrationForkCount>
    </properties>

</project>

Use Property

<project>
    <build>
        <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-failsafe-plugin</artifactId>
                 <version>2.18.1</version>
                 <executions>
                     <execution>
                         <goals>
                             <goal>integration-test</goal>
                             <goal>verify</goal>
                         </goals>
                         <configuration>
                             <forkCount>${integrationForkCount}</forkCount>
                             <reuseForks>false</reuseForks>
                             <argLine>-Xmx1024m</argLine>
                             <argLine>-XX:MaxPermSize=256m</argLine>
                             <useFile>false</useFile>
                         </configuration>
                     </execution>
                 </executions>
            </plugin>
        </plugins>
    </build>
</project>

Override Custom Property on Command-Line

$ mvn -DintegrationForkCount=2 verify

Set Maven Properties in Environment Variables

Set MAVEN_OPTS in your shell's configuration script. For example:

export MAVEN_OPTS=-DintegrationForkCount=4

Then, on that machine, that property will always be set already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment