-
Select File -> Project.
-
Expand Maven.
-
Select Maven Project.
-
Click next.
-
Check: Create a simple project.
-
Check: Use default workspace location.
-
Click on next.
-
Enter the Group Id.
-
Enter the Artifact Id.
-
Click on finish.
The maven project is ready. The directory structure is created for the maven project. The minimal pom.xml file is created.
• 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.
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.
Right click on the project.
Expand Run As
Select Maven build.
Enter Goals: clean
Click on Run.
The target folder is now empty.
-
Right click on the project.
-
Expand Run As.
-
Select Maven build.
-
Enter Goals: compile
-
Click on Run.
-
Right click on the project.
-
Expand Run As.
-
Select Maven build.
-
Enter Goals: test-compile
-
Click on Run.
We can go to the target folder to see the class files that are created.
-
Right click on the project.
-
Expand Run As.
-
Select Maven build.
-
Enter Goals: test
-
Click on Run.
-
Right click on the project.
-
Expand Run As.
-
Select Maven build.
-
Enter Goals: install
-
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:
-
Right click on the project.
-
Expand Run As.
-
Select Maven build.
-
Enter Goals: package
-
Click on Run.
The jar file will be created but it will not be installed in the local repository.
-
Open pom.xml file
-
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.
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.
Suppose we want to exclude the dependency which is transitive.
-
Go to the pom.xml file of the dependency.
-
Copy the dependency.
-
Go to the pom.xml file.
-
Go inside the dependency tag. (Dependency to which it is transitive to)
-
Enter the following:
<exclusions> <exclusion> <paste the transitive dependency except the version tag> </exclusion> </exclusion>
-
Save the pom.xml file.
Now the transitive dependency will be excluded.
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>
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,
-
[] includes the value provided in the version tag.
-
() excludes the value provided in the version tag.
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.
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.
-
Click on file -> project.
-
Expand Maven.
-
Click on Maven project.
-
Click next.
-
Check: Create a simple project.
-
Check: Use default workspace location.
-
Click on next.
-
Enter the Group Id.
-
Enter the Artifact Id.
-
Change the packaging to pom.
-
Click on Finish.
The Multi-Module maven project is created. The directory structure is also created.
The directory structure is as follows:
- Src
a. Site
- 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.
-
Select File -> New.
-
Expand Maven.
-
Select maven Module.
-
Click Next.
-
Check: Create a simple project.
-
Give a Module name.
-
Enter the parent.
-
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.
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.
-
mvn clean install -> This command will clean the target folder and will create the jar and install it in the local repository.
-
mvn clean install -DskipTests -> This command will skip the Tests.
-
mvn -X clean install -> The maven runs in debug mode. More log file contents are printed.
-
mvn help:effective-pom -> This command shows the pom.xml file with the super pom file.
-
mvn dependency:tree -> The entire dependency tree for the project is printed.
-
mvn dependency:sources -> This command shows the source code for jar files.
-
mvn –help -> Shows all the commands that are available in maven.
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.