bbrother92
12/3/2017 - 12:21 AM

#maven #cheatsheet

#maven #cheatsheet

Setting up

Set environment variables: M2_HOME and bin directory.
linux: export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.1 export M2=$M2_HOME/bin or
export PATH=/Users/xxx/apache-maven-3.0.5/bin:$PATH

Project structure

  |-- pom.xml
  `-- src
      |-- main
      |   `-- java
      |       `--ru
      |           `-- apache_maven
      |                   `-- App.java
      `-- test
          `-- java
              `-- ru
                  `-- apache_maven
                          `-- AppTest.java

Configuration

sets jdk version for compiler and deletes *.log files on every clean phase

<build>
<plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
        <configuration>
            <source>${jdk.version}</source>
            <target>${jdk.version}</target>
        </configuration>
    </plugin>

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
    <configuration>
    <filesets>
    <fileset>
        <directory>${basedir}/src/test/resources</directory>
        <includes>
            <include>*.log</include>
        </includes>
    </fileset>
    </filesets>
    </configuration>
    </plugin>
</plugins>
</build>

Tests

To run the tests selectively in the Maven you need to specify the surefire plugin or it will run all the tests:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
    <configuration>
        <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>

By default Maven uses the following naming conventions when looking for tests to run: Test* , *Test, *TestCase
Otherwise, you need to configure:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
    <configuration>
        <includes>
            <include>Sample.java</include>
        </includes>
    </configuration>
</plugin>

Plugins

mvn org.apache.maven.plugins:maven-checkstyle-plugin:check or mvn checkstyle:check

Commands

mvn test -X to see full debug
mvn dependency:tree tree dependencies
mvn -o dependency:list displays the list of dependencies
mvn dependency:go-offline resolves all project dependencies, including plugins
mvn clean -X | grep -P --color=always '.+(?=settings.xml)' show settings folder
mvn help:effective-pom displays the effective POM, with the active profiles
mvn help:effective-settings displays the effective settings
mvn help:all-profiles lists the profiles mvn help:system system info ``

Tests

mvn test -Dmaven.compiler.verbose=true runs test phase with verbose property
mvn compiler:testCompileruns only plugin goal mvn install -DskipTests this will skip running tests, but still compile it
mvn install -Dmaven.test.skip=true this will skip compilation and execution of tests
mvn install -Dtest=FooTest runs only FooTest
mvn -Dmaven.surefire.debug test to debug your tests
mvn '-Dtest=de.mypackage.**.*Test' test with subpackages included