Friday, February 10, 2012

mvn helpers

recently needed to setup/debug/refactor a complex multi-module maven project.. some maven stuff, learned the hard way....

Generate eclipse setup files in a maven project...
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs

Default eclipse may point M2_REPO to ~/.m2 . If you want to change that, you have to do so in Windows->Preferences->Maven->User Settings .

Skip tests while building the project..
-Dmaven.test.skip  skips both test compilation and execution
-DskipTests=true    skips test execution only


Run tests from named test class only..
-Dtest=<fully-qualified-test-class-name>Executing a class containing main(String[] args) method..
mvn exec:java -Dexec.mainClass=
<fully-qualified-main-class-name>

Build selected modules of a parent pom..
mvn package -pl mod1, mod2 [-am]
-pl : list of modules
-am : also make the dependencies

Build starting from a given module
mvn package -rf :mod3

Downloading dependencies of the project
mvn dependency:copy-dependencies -DexcludeScope=provided


(More at http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html )
 

Getting description of goals supported by a plugin
mvn help:describe -Dplugin=<name> (if plugin group has been added to pluginGroups, look in settings reference below)
or
mvn help:describe -DgroupId=<groupId> -DartifactId=<artifactId>
.. add -Dfull to get more detailed information on goals.

Check out other goals in help plugin itself
mvn help:describe -Dplugin=help -Dfull

some important ones are...

mvn help:effective-settings
mvn help:active-profiles
mvn help:effective-pom

Finding dependencies of your project
mvn dependency:analyze
mvn dependency:tree

dependency graph: http://mvnplugins.fusesource.org/maven/1.8/maven-graph-plugin/


You can also remotely debug the tests run through maven by using
-Dmaven.surefire.debug
 flag. Optionally you can specify all the jvm debugging related parameters using
-Dmaven.surefire.debug="
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE
" (Reference)

You can run a specific testcase with -Dtest flag as described on Ref . Tests can altogether be skipped using -DskipTests flag.
For testng, you can annotate a test like @Test(groups = {"xyz"}) and then just run these tests by using flag -Dgroups=xyz (remember to escape '=' in shell by using '\=')

try using "mvn -U" to force remote repository snapshots update when mvn is failing to find an artifact.

packaging all the dependencies into jar: 
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-my-jar-with-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

Downloading all dependencies--
copies all dependencies to target/dependency, See plugin doc for more options to refine what is copied.
mvn dependency:copy-dependencie [-DexcludeTransitive -DoutputDirectory=<some_dir>]


Resources:
Pom Reference
Settings Reference

Books:
http://maven.apache.org/articles.html
http://www.sonatype.com/books/mvnref-book/reference/