Skip to content

Instantly share code, notes, and snippets.

@Rishav-Git
Last active February 2, 2018 05:46
Show Gist options
  • Save Rishav-Git/fd06f7910679eb59b53c5a232217921e to your computer and use it in GitHub Desktop.
Save Rishav-Git/fd06f7910679eb59b53c5a232217921e to your computer and use it in GitHub Desktop.
Using Maven as Continuous Build Tool

Maven Basics:

Using eclipse to create a simple maven project:

  1. Select File -> Project.

  2. Expand Maven.

  3. Select Maven Project.

  4. Click next.

  5. Check: Create a simple project.

  6. Check: Use default workspace location.

  7. Click on next.

  8. Enter the Group Id.

  9. Enter the Artifact Id.

  10. Click on finish.

The maven project is ready. The directory structure is created for the maven project. The minimal pom.xml file is created.

The directory structure:

• src/main/java

• src/main/resources

• src/test/java

• src/main/resources

• JRE System Library

• Maven Dependencies

• Src

 Main

 Test

• target

• pom.xml

Now create a java class in src/main/java and a Junit Test case in src/test/java.

Creating a Jar file:

 Compile the java files to create the class files of the java files.

 Run the Tests.

 Create the Jar

When a maven command is run, all the results are stored in the target folder. So, first we need to delete everything from the target folder.

For deleting everything in the target folder:

 Right click on the project.

 Expand Run As

 Select Maven build.

 Enter Goals: clean

 Click on Run.

The target folder is now empty.

Compiling the Java classes:

  1. Right click on the project.

  2. Expand Run As.

  3. Select Maven build.

  4. Enter Goals: compile

  5. Click on Run.

Compiling the Test classes:

  1. Right click on the project.

  2. Expand Run As.

  3. Select Maven build.

  4. Enter Goals: test-compile

  5. Click on Run.

We can go to the target folder to see the class files that are created.

Running the tests:

  1. Right click on the project.

  2. Expand Run As.

  3. Select Maven build.

  4. Enter Goals: test

  5. Click on Run.

Creating the Jar:

  1. Right click on the project.

  2. Expand Run As.

  3. Select Maven build.

  4. Enter Goals: install

  5. Click on Run.

The jar file is created and installed in the local repository.

We can go to target/surefire-reports directory to see the test results.

If we want to create the jar file and not install it in the local repository, then:

  1. Right click on the project.

  2. Expand Run As.

  3. Select Maven build.

  4. Enter Goals: package

  5. Click on Run.

The jar file will be created but it will not be installed in the local repository.

Maven Dependency Management:

Adding Dependency:

  1. Open pom.xml file

  2. Add the following tags and their contents inside the tag:

    <dependencies>
        <dependency>
            <groupId> Group-ID-for-the-dependency</groupId>
            <artifactId>Artifact -ID-for-the-dependency</artifactId>
            <version>Version-of-the-dependency</version>
        </dependency> 
    </dependencies>
    

We have to add dependencies in the above format. We can use google to search for dependencies. Copy the dependencies required and paste it in the pom.xml file.

Adding dependency to the pom.xml file, the required jar file is added to the Maven Dependencies directory and can use the jar file required for the project.

Transitive dependencies:

When we add a dependency to the pom.xml file, the corresponding jar file is added to the Maven Dependencies directory. If along with that jar file other jar files are also added, then the jar file is dependent on the other jar files that came along with that jar file. This is called transitive dependency. Thus, transitive dependency is the dependencies of the dependencies.

We can open the pom.xml file for a particular jar file by ctrl + left click on the dependency. On the pom.xml file of the dependency we can see all the transitive dependencies defined.

Excluding dependencies:

Suppose we want to exclude the dependency which is transitive.

  1. Go to the pom.xml file of the dependency.

  2. Copy the dependency.

  3. Go to the pom.xml file.

  4. Go inside the dependency tag. (Dependency to which it is transitive to)

  5. Enter the following:

    	 <exclusions>
     		 <exclusion>
       			 <paste the transitive dependency except the version tag>
      		</exclusion>
    </exclusion>
    
  6. Save the pom.xml file.

Now the transitive dependency will be excluded.

Scope of Dependency:

Maven scope attribute is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime, etc.). Maven provides six scopes i.e. compile, provided, runtime, test, system, and import.

  <dependencies>
      <dependency>
	      <groupId> Group-ID-for-the-dependency</groupId>
	      <artifactId>Artifact -ID-for-the-dependency</artifactId>
	      <version>Version-of-the-dependency</version>
	      <scope>scope-of-the-dependency</scope>
      </dependency> 
  </dependencies>

Versioning of dependencies:

Specifying the range of versions in the dependencies:

 <version>[<value>,]</version>    -> Maven will get the latest version greater than <value>.
 <version>[<value1>,<value2>]</version>   -> Maven will get version <value2>
 <version>[<value1>,<value2>)</version>    -> Maven will get the latest version below <value2>
 <version>[,<value>]</version>     -> 	Maven will get the version <value>

Therefore,

  1. [] includes the value provided in the version tag.

  2. () excludes the value provided in the version tag.

Passing parameter to a maven goal:

Name-of-the-goal -Dname-of-the-parameter=value-of-the-parameter

For example:

  help:effective-pom -Doutput=effective-pom.xml

This command will print the value of help:effective-pom to the effective-pom.xml file.

help:effective-pom command is used to see the super pom file along with the pom file.

help:effective-settings command: Opens the settings.xml file for the maven project.

Maven from command Line:

Go to the Project folder and open the command prompt to use the maven commands from the command line.

To perform a particular goal from the command line we use “mvn -” command.

For example:

                Mvn -version -> This will print the verion of the maven we are using.

Basics of Multi-Module Maven project:

Creating a Multi-Module Maven project:

  1. Click on file -> project.

  2. Expand Maven.

  3. Click on Maven project.

  4. Click next.

  5. Check: Create a simple project.

  6. Check: Use default workspace location.

  7. Click on next.

  8. Enter the Group Id.

  9. Enter the Artifact Id.

  10. Change the packaging to pom.

  11. Click on Finish.

The Multi-Module maven project is created. The directory structure is also created.

The directory structure is as follows:

  1. Src

a. Site

  1. Pom.xml

This pom.xml file acts as the parent pom. This pom.xml file will be parent to all the pom.xml files that will be underneath it.

Creating Modules:

  1. Select File -> New.

  2. Expand Maven.

  3. Select maven Module.

  4. Click Next.

  5. Check: Create a simple project.

  6. Give a Module name.

  7. Enter the parent.

  8. Click on Finish.

A new project is created with the name of module given. The directory structure is the same as of a simple maven project.

The pom.xml file of the module project contents reference to the parent pom.xml.

The parent pom.xml file contains a module tag which contains the artifact Id of the module created underneath it.

[NOTE: The packaging for the parent pom.xml file is pom and the packaging for the module pom.xml file is jar. The default packaging is jar.]

Now when we use the maven commands, the commands run for all the modules associated with the parent project.

Best Practices of Multi-Module Maven Project:

Suppose we need to add dependencies to the modules of the Multi-Module Maven project. So, we need to add the dependencies to the pom.xml file of the modules. This is how different modules can have different dependencies. Now suppose we have same dependencies in multiple modules and we need to change the version on a later point of time. So, in order to change the version, we have to go all the pom.xml files of the modules and change the version which is a very bad practice. What we can do is remove the version tags from the dependencies of the pom.xml files in the modules. Then go to the parent pom.xml file.

Add the dependencies of the modules inside the dependencyManagement tag in the follow manner:

  <dependencyManagement>
    	<dependencies>
       	 	<dependency>
	      			<groupId> Group-ID-for-the-dependency</groupId>
	     			 	<artifactId>Artifact -ID-for-the-dependency</artifactId>
	      			<version>Version-of-the-dependency</version>	
      		</dependency> 
   		</dependencies>
    </dependencyManagement>

This is how we can use the parent pom.xml file manage the versions of all the dependencies of the modules.

Suppose one module wants to make use of another module. We can do this by adding dependency of a module to another. For example:

 	    <dependency>
	      <groupId> Group-ID-for-the-module</groupId>
	      <artifactId>Artifact -ID-for-the-module</artifactId>
	      <version>${project.version}</version>
    </dependency> 

${Project.version} is the built-in variable which is used specify the version of the module to which we are adding the dependency.

We can also manage this version from the dependencyManagement tag in the parent pom.xml file.

Maven Commands- Tips and Tricks:

  1. mvn clean install -> This command will clean the target folder and will create the jar and install it in the local repository.

  2. mvn clean install -DskipTests -> This command will skip the Tests.

  3. mvn -X clean install -> The maven runs in debug mode. More log file contents are printed.

  4. mvn help:effective-pom -> This command shows the pom.xml file with the super pom file.

  5. mvn dependency:tree -> The entire dependency tree for the project is printed.

  6. mvn dependency:sources -> This command shows the source code for jar files.

  7. mvn –help -> Shows all the commands that are available in maven.

Creating projects with maven archetypes:

The command to use archetype plugin is:

                mvn archetype:generate 

Maven will now generate a number of sample projects. Now maven will ask for the number corresponding to the sample project we want to use. If we don’t enter any number and press Enter then maven will load the default project. Now maven will provide with maven archetype quickstart version to choose from. We need to choose the version and press Enter. Enter the groupId, artifactId, version and package.

Press Y to confirm and the project will be created.

Thus, archetypes are used to generate sample projects.

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