RK !

Let's make comprehension easy ...

Maven

Author: Romaan, Last Updated: March 6, 2017, 1:08 p.m.

Maven is a build automation tool used primarily for Java projects. The word maven means "accumulator of knowledge" in Yiddish. Maven addresses two aspects of building software: first, it describes how software is built, and second, it describes its dependencies.

The projects using Maven should have the following directory structure:

  • src/main/java                         # Application/Library sources
  • src/main/resources                # Application/Library resources
  • src/main/filters                       # Resource filter files
  • src/main/webapp                   # Web application sources
  • src/test/java                           # Test sources
  • src/test/resources                  # Test resources
  • src/test/filters                         # Test resource filter files
  • src/it                                       # Integration tests
  • src/assembly                         # Assembly descriptors
  • src/site                                   # Site
  • LICENSE.txt                          # Project's license
  • NOTICE.txt                            # Notices and attributions required by libraries that the project depends on
  • README.txt                          # Project's readme
  • pom.xml                                # Project Object Model file

The following are the maven life cycle management commands in ascending order:

  • mvn archetype:help             # Create project scaffold
  • mvn validate                        # Validate the project is correct and all necessary information is available
  • mvn compile                        # Compile the project
  • mvn test                               # Run unit test cases of the project
  • mvn package                      # Package in its distributable format
  • mvn integration-test           # Process and deploy the package if necessary into an environment where integration tests can be run
  • mvn verify                           # Run any checks to verify the package is valid and meets quality criteria
  • mvn install                          # Install the package into the local repository, for use as a dependency in other projects locally
  • mvn deploy                        # Done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects
  • mvn clean                         # Cleans all the generated binaries

In order to change the source code compiler version, below are the changes in pom.xml inside <project>:

<build>
     <plugins>
          <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
           <version>3.0</version>
           <configuration >
              <source>1.7</source>
              <target>1.7</target>
           </configuration>
          </plugin>
      </plugins>
  </build>

Below is an example to add dependency (junit and mockito in this case):

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.4</version>
    </dependency>

More about unit testing:

In order to run a particular unit test:

mvn -Dtest=TestClassName#method test

In order to run a test in debug mode:

mvn -Dtest=TestClassName -Dmaven.surefire.debug test    # Listen to remote application debug port 5005 in eclipse and breakpoints will start catching up

List repositories being used:

mvn help:evaluate # When prompted enter: ${project.repositories}

OR

mvn dependency:list-repositories 

See the effective-POM

mvn help:effective-pom

List local repository path:

mvn help:evaluate -Dexpression=settings.localRepository

Popular Tags:


Related Articles:


Comments: