Maven – Repositories

Maven – Repositories ”; Previous Next What is a Maven Repository? In Maven terminology, a repository is a directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily. Maven repository are of three types. The following illustration will give an idea regarding these three types. local central remote Local Repository Maven local repository is a folder location on your machine. It gets created when you run any maven command for the first time. Maven local repository keeps your project”s all dependencies (library jars, plugin jars etc.). When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build. Maven local repository by default get created by Maven in %USER_HOME% directory. To override the default location, mention another path in Maven settings.xml file available at %M2_HOME%conf directory. <settings xmlns = “http://maven.apache.org/SETTINGS/1.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd”> <localRepository>C:/MyLocalRepository</localRepository> </settings> When you run Maven command, Maven will download dependencies to your custom path. Central Repository Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries. When Maven does not find any dependency in local repository, it starts searching in central repository using following URL − https://repo1.maven.org/maven2/ Key concepts of Central repository are as follows − This repository is managed by Maven community. It is not required to be configured. It requires internet access to be searched. To browse the content of central maven repository, maven community has provided a URL − https://search.maven.org/#browse. Using this library, a developer can search all the available libraries in central repository. Remote Repository Sometimes, Maven does not find a mentioned dependency in central repository as well. It then stops the build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository, which is developer”s own custom repository containing required libraries or other project jars. For example, using below mentioned POM.xml, Maven will download dependency (not available in central repository) from Remote Repositories mentioned in the same pom.xml. <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>com.companyname.common-lib</groupId> <artifactId>common-lib</artifactId> <version>1.0.0</version> </dependency> <dependencies> <repositories> <repository> <id>companyname.lib1</id> <url>http://download.companyname.org/maven2/lib1</url> </repository> <repository> <id>companyname.lib2</id> <url>http://download.companyname.org/maven2/lib2</url> </repository> </repositories> </project> Maven Dependency Search Sequence When we execute Maven build commands, Maven starts looking for dependency libraries in the following sequence − Step 1 − Search dependency in local repository, if not found, move to step 2 else perform the further processing. Step 2 − Search dependency in central repository, if not found and remote repository/repositories is/are mentioned then move to step 4. Else it is downloaded to local repository for future reference. Step 3 − If a remote repository has not been mentioned, Maven simply stops the processing and throws error (Unable to find dependency). Step 4 − Search dependency in remote repository or repositories, if found then it is downloaded to local repository for future reference. Otherwise, Maven stops processing and throws error (Unable to find dependency). Print Page Previous Next Advertisements ”;

Maven – POM

Maven – POM ”; Previous Next POM stands for Project Object Model. It is fundamental unit of work in Maven. It is an XML file that resides in the base directory of the project as pom.xml. The POM contains information about the project and various configuration detail used by Maven to build the project(s). POM also contains the goals and plugins. While executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, and then executes the goal. Some of the configuration that can be specified in the POM are following − project dependencies plugins goals build profiles project version developers mailing list Before creating a POM, we should first decide the project group (groupId), its name (artifactId) and its version as these attributes help in uniquely identifying the project in repository. POM Example <project xmlns = “http://maven.apache.org/POM/4.0.0″ xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.project-group</groupId> <artifactId>project</artifactId> <version>1.0</version> </project> It should be noted that there should be a single POM file for each project. All POM files require the project element and three mandatory fields: groupId, artifactId, version. Projects notation in repository is groupId:artifactId:version. Minimal requirements for a POM − Sr.No. Node & Description 1 Project root This is project root tag. You need to specify the basic schema settings such as apache schema and w3.org specification. 2 Model version Model version should be 4.0.0. 3 groupId This is an Id of project”s group. This is generally unique amongst an organization or a project. For example, a banking group com.company.bank has all bank related projects. 4 artifactId This is an Id of the project. This is generally name of the project. For example, consumer-banking. Along with the groupId, the artifactId defines the artifact”s location within the repository. 5 version This is the version of the project. Along with the groupId, It is used within an artifact”s repository to separate versions from each other. For example − com.company.bank:consumer-banking:1.0 com.company.bank:consumer-banking:1.1. Super POM The Super POM is Maven’s default POM. All POMs inherit from a parent or default (despite explicitly defined or not). This base POM is known as the Super POM, and contains values inherited by default. Maven use the effective POM (configuration from super pom plus project configuration) to execute relevant goal. It helps developers to specify minimum configuration detail in his/her pom.xml. Although configurations can be overridden easily. An easy way to look at the default configurations of the super POM is by running the following command: mvn help:effective-pom Create a pom.xml in any directory on your computer.Use the content of above mentioned example pom. In example below, We”ve created a pom.xml in C:MVNproject folder. Now open command console, go the folder containing pom.xml and execute the following mvn command. C:MVNproject>mvn help:effective-pom Maven will start processing and display the effective-pom. C:MVN>mvn help:effective-pom [INFO] Scanning for projects… [INFO] [INFO] —————< com.companyname.project-group:project >—————- [INFO] Building project 1.0 [INFO] ——————————–[ jar ]——————————— [INFO] [INFO] — maven-help-plugin:3.2.0:effective-pom (default-cli) @ project — [INFO] Effective POMs, after inheritance, interpolation, and profiles are applied: [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 2.261 s [INFO] Finished at: 2021-12-10T19:54:53+05:30 [INFO] ———————————————————————— C:MVN> Effective POM displayed as result in console, after inheritance, interpolation, and profiles are applied. <?xml version=”1.0″ encoding=”Cp1252″?> <!– ====================================================================== –> <!– –> <!– Generated by Maven Help Plugin on 2021-12-10T19:54:52+05:30 –> <!– See: http://maven.apache.org/plugins/maven-help-plugin/ –> <!– –> <!– ====================================================================== –> <!– ====================================================================== –> <!– –> <!– Effective POM for project –> <!– ”com.companyname.project-group:project:jar:1.0” –> <!– –> <!– ====================================================================== –> <project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.project-group</groupId> <artifactId>project</artifactId> <version>1.0</version> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </pluginRepository> </pluginRepositories> <build> <sourceDirectory>C:MVNsrcmainjava</sourceDirectory> <scriptSourceDirectory>C:MVNsrcmainscripts</scriptSourceDirectory> <testSourceDirectory>C:MVNsrctestjava</testSourceDirectory> <outputDirectory>C:MVNtargetclasses</outputDirectory> <testOutputDirectory>C:MVNtargettest-classes</testOutputDirectory> <resources> <resource> <directory>C:MVNsrcmainresources</directory> </resource> </resources> <testResources> <testResource> <directory>C:MVNsrctestresources</directory> </testResource> </testResources> <directory>C:MVNtarget</directory> <finalName>project-1.0</finalName> <pluginManagement> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>default-clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>default-testResources</id> <phase>process-test-resources</phase> <goals> <goal>testResources</goal> </goals> </execution> <execution> <id>default-resources</id> <phase>process-resources</phase> <goals> <goal>resources</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>default-testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <executions> <execution> <id>default-test</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-install</id> <phase>install</phase> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>default-deploy</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.3</version> <executions> <execution> <id>default-site</id> <phase>site</phase> <goals> <goal>site</goal> </goals> <configuration> <outputDirectory>C:MVNtargetsite</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> <execution> <id>default-deploy</id> <phase>site-deploy</phase> <goals> <goal>deploy</goal> </goals> <configuration> <outputDirectory>C:MVNtargetsite</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> </executions> <configuration> <outputDirectory>C:MVNtargetsite</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </plugin> </plugins> </build> <reporting> <outputDirectory>C:MVNtargetsite</outputDirectory> </reporting> </project> In above pom.xml, you can see the default project source folders structure, output directory, plug-ins required, repositories, reporting directory, which Maven will be using while executing the desired goals. Maven pom.xml is also not required to be written manually. Maven provides numerous archetype plugins to create projects, which in order, create the project structure and pom.xml Print Page Previous Next Advertisements ”;