how to install, configure, and start using maven. The basic concepts and how you can start working with the commands
Introduction to Maven What is Maven? Maven is one of the build systems available. It is capable to manage any Java-Based Project What is a build system? A build system is platform which allows the source code to be built an efficient manner. A standard build system is expected to contain the following 1. Compile source code 2. Copy resource 3. Compile and Run test 4. Package project 5. Deploy Project 6. Cleanup What is the main component in Maven based architecture? Maven is a software management tool based on POM. POM helps in project build, reporting, and documentations from a central point of view. Maven can be used to manage any Java-Based project What is POM? POM: Project Object Model. It is an XML file, which contains the information, dependencies and other details required by maven to build the project The POM contains the following 1. Name, description, and version of the Project 2. Artifact type 3. Source code location 4. Dependencies 5. Plugins 6. Profiles (alternate build configurations) What are the important features of Maven? 1. Making the build process easy 2. Including and upgrading the new set of dependencies Managing different environment based specifications in Profiles, so same code repository can be used for the different environment. =====================================================
Install Maven Pre-requisies 1. JDK must be installed 2. JAVA_HOME must be set in system variables cmd> java -version cmd> echo $JAVA_HOME Alternately you can also see this from the 'System Variable > Path Variables' from the computer properties Download Visit - https://maven.apache.org/download.cgi and down the binary zip. Installation Unzip the downloaded file to your desired location. Set the M2_HOME in Environment variable Variable_name: M2_HOME Variable_value: Path_of_the_maven_directory (Note: do not get inside the bin.) Some Projects uses the name 'MAVEN_HOME' to identify the maven home, hence also add another system variable as follows Variable_name: MAVEN_HOME Variable_value: Path_of_the_maven_directory Add the Path of the Maven in the System Variable Path Search the system variable named PATH, and append the location of the 'bin' directory of the maven directory Check Cmd> mvn -version =====================================================
Create Maven Projects Create a DemoMavenProject from cmdLine mvn archetype:generate -DgroupId=ToolsQA -DartifactId=DemoMavenProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false Inputs (You can chosse the number from the list generated in the cmdline after executing the above command) Choose archetype: Maven-archetype-webapp Select other values as per the command prompt required inputs Alternately you can use an IDE, like eclipse If you are using an IDE you have to give details such as groupID and artifactID. =====================================================
Create JARs Overview 1. Compile All your Source file , this will result in '.class' file. 2. Compile All your Test file , this will result in '.class' files 3. Run all test 4. If all test passes, jar files are created. Steps: 1. Navigate to the Project_Home directory 2. Clean your project. This will clean any previously present target directory, so you can get a completely fresh build. a. cmd>mvn clean b. Alternately, you can use the IDE, right click on the Project_Name -> Run as -> Maven Build... -> Goals: clean. 3. Compile your source code. This will compile and create a '.class' file for you. a. cmd>mvn compile b. If you are using the IDE, right click on the Project_Name -> Run as -> Maven Build... -> Goals: compile 4. Compile your test file. a. cmd>mvn test-compile b. If you are using the IDE, right click on the Project_Name -> Run as -> Maven Build... -> Goals: test-compile 5. Run the test a. cmd>mvn test b. If you are using the IDE, right click on the Project_Name -> Run as -> Maven Build... -> Goals: test c. This will create a folder named 'surefire-reports' inside the targets Here it is assumed all dependencies are clear and you have included all required dependencies in POM. 6. Create JAR a. cmd> mvn install b. If you are using the IDE, right click on the Project_Name -> Run as -> Maven Build... -> Goals: install The Jar file will create as artifactID-version.jar =====================================================
Maven Build LifeCycle Maven is based on central concept of build lifecycle. This means the steps required for building and deploying a project is clearly defined. For a developer, it means he just has to make sure all dependencies are mentioned in the POM, and the rest can be managed by a set of maven commands. The following are the list of commands that a developer is supposed to know 1. Validate (validate your dependencies) 2. Compile (compile your source code) 3. Test (Checks if all test conditions are clear) 4. Package (Packages the file into a portable format like JAR) 5. Integration-test (If any integration is required, it is done) 6. Verify (run any checks if packages and verify if it meets the qualifying criteria) 7. Install (install the package into a local repository, so you can even use it as dependencies in other projects) 8. Deploy (it is done in environments the App/Project is supposed to be released) The above commands follows a hierarchy, Like if you directly give the 'compile' command it will automatically perform the 'validate' and then go for 'compile'. For example, if you used the 'install' (point 7 of the above hierarchy), points 1-6 are automatically performed for you. There are some other commands as well, such as 'clean', which is not a part of the life cycle, but really helps in maintainability of the Project =====================================================
Transitive Dependencies When you add any dependencies in the POM file, it automatically adds relevant JAR inside the Maven dependencies. Sometimes you will notice that it automatically add another JARs which are indirectly added. For example, If you are adding JUnit dependencies in POM, it add JUnit JAR and also includes hamcrest JAR by itself. To get a better picture of this you can even view this under dependencies hierarchy of your IDE, say Eclipse. The dependencies of a dependency are called Transitive dependencies. =====================================================
Exclude Dependencies Suppose you don't want to include a transitive dependencies as it can conflict with some other dependency of your choice. For example, if 'jboss-logging' is included as a transitive dependency of 'hibernate' but you dont want to use it and would like to use any alternate logging. For this you have to mention the groupID and artifactID of the transitive dependency under 'exclusions' for the respective dependency <dependency> <groupID>org-hibernate</groupID> <artifactID>hibernate-core</artifactID> <version>5.1.0.Final</version> <exclusions> <exclusion> <groupID>org.jboss.logging</groupID> <artifactID>jboss-logging</artifactID> <exclusion> </exclusions> </dependency> To identify the 'groupID' and 'artifactID' of the transitive dependencies you have to follow the dependencies hierarchy in your IDE, alternately you can go to POM, 'CTRL+Click' on the dependency, this will show the pom of the dependency. Inside the pom, you can see the groupID and artifactID of the transitive dependencies. =====================================================
Scope Dependency Dependencies are generally global in nature, but in case if you want to restrict the dependencies you can even achieve this. For example - Like JUnit is required only for Testing and has not use in Source code dependencies. To do this you have to add the 'scope' tag. <dependency> <groupID>junit</groupID> <artifactID>junit</artifactID> <version>4.12</version> <scope>test</scope> </dependency> By default 'compile' scope is added to all the dependencies. This means all dependencies must be available atleast till compile stage. Some other scope values are 1. Runtime : this means the dependencies is not required for the compilation but only required for the execution. 2. Provided: its much like compile, but it expects that the dependencies are provided at the runtime by the container or the JDK. Etc =====================================================
Use Maven Based Projects in Jenkins Steps 1. Create a free style project 2. If you want to pull your code from a GIT, use SCM and give details such as repo link, branch name, and credentials 3. You may or may not include the Build triggers and Build Environments based on your projects 4. Inside Build stage, from add build step drop-down, choose 'Invoke top level maven targets' 5. Under goals, enter the same goal statement that you use in cmd line or IDE based maven a. Example Goal: clean test 6. Now, execute the Jenkins Job. DONE