What is POM, Parent, Dependencies?(Part-2)
POM.xml
POM.xml file is what maven refers to for the project information and build configuration information.
Project information typically includes Project version, description, artifacts, etc., where as build configuration includes dependencies(JARs), goals, plugins, repositories, etc., So, a general POM file contains all these details. Since, this post is continuation of the maven series we have started earlier. I am referring to the POM(below fig.) file that we generated using spring boot archetype in the earlier post. For understanding the project creation using archetype refer to Post1 (http://javarephrased.blogspot.in/2016/02/what-is-maven-what-is-archetype-and.html) on maven.
As you see, we didn't mention all the details (discussed earlier) in the POM file. Maven do not even force you to provide all the details. To create a Simple POM.xml for a project, modelVersion(1), artifactId(2), groupId(3), version(4) in the below fig. are enough to do the Job.
Because, maven is smart enough to substitute the details we did not provide with some default values and can run the POM file. And details like goals, repositories and plugins gets automatically included from Super POM.xml (https://maven.apache.org/ref/3.0.4/maven-model-builder/super-pom.html)(Click to see) through inheritance.
Parent tag usage is limited to 1 per POM file. If you want your POM file to refer to the dependencies from multiple POM files, use import scope instead. The Usage of import tag will be covered in next posts.
parent
(9) -Maven supports inheritance. This tag is used for inheriting properties of spring-boot-starter-parent POM.xml (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-starters/spring-boot-starter-parent/pom.xml)(for eg.) project to our project's POM.xml. So, the internal dependencies of spring-boot-starter-parent project(JARS, Plugins, etc.,) can be accessed in our project and even we can access the project's properties defined in spring-boot-starter-parent POM.xml.Parent tag usage is limited to 1 per POM file. If you want your POM file to refer to the dependencies from multiple POM files, use import scope instead. The Usage of import tag will be covered in next posts.
![]() |
Generated POM.xml from Spring-boot ArchetypeDependencies
(6) Every project refers some external JARs. Lets see how we managed these JARs in our projects before the evolution of maven.
With maven, all we need to do is create a dependency tag with groupId and artifactId under the dependencies tag of the POM file. How it works?
Maven first checks for this JAR in local repository (<USER_HOME>/.m2). If found, it includes the library and referenced libraries of JAR files to your project's build path. Otherwise, it downloads the artifacts from the Maven central repository and copies it to local repository and then includes it into project's build path. It gets the referenced libraries information by looking at the JAR file's POM.xml file which is available in central repository (http://repo1.maven.org/maven2/org/springframework/boot/spring-boot/1.0.2.RELEASE/spring-boot-1.0.2.RELEASE.pom).
Maven Central repository(available at http://repo1.maven.org/maven2/) maintains all the popular artifacts(JARS) eg. Spring, hibernate, etc. At the background, The Maven central repository registers to the other open source repositories like GitHub (https://github.com/spring-projects) where all the artifacts for spring (for example) are maintained. Since it has a tie up with open source repositories, any version updates from the open source repositories will be pushed to the maven central repository. So, we don't need to bother about version updates.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Also, it offers the scope at which the artifact(JAR) should be applicable. In the above snippet, Junit JAR is limited to the scope of test soruces classpath and is not applicable for your main sources classpath. So far, We have covered, how to generate the project from an archetype, how to include external JARs into your project's build path in the first 2 posts of Maven. Now, we have the infrastructure ready for the project. Lets discuss the next step, how we can build and install the project in the coming posts. |
Comments
Post a Comment