mvn repository setting in setting.xml and pom.xml - unix1998/technical_notes GitHub Wiki
Maven artifact repository URLs can be configured in both the settings.xml and pom.xml files. However, they serve different purposes and are used in different contexts.
The settings.xml file is located in the Maven installation directory (MAVEN_HOME/conf/settings.xml) or in the user's home directory (~/.m2/settings.xml). This file is used to configure global settings for Maven, including repositories, credentials, and proxies.
Here is an example of how to configure repositories in settings.xml:
<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">
<mirrors>
<mirror>
<id>central</id>
<mirrorOf>central</mirrorOf>
<url>http://central.maven.org/maven2/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>example-profile</id>
<repositories>
<repository>
<id>example-repo</id>
<url>http://repository.example.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>example-profile</activeProfile>
</activeProfiles>
</settings>The pom.xml file is the project-specific configuration file. You can define repositories in the pom.xml to ensure that they are used specifically for that project.
Here is an example of how to configure repositories in 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.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>example-repo</id>
<url>http://repository.example.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>example-plugin-repo</id>
<url>http://repository.example.com/plugins</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>-
Global vs. Project-Specific:
settings.xmlis used for global configurations across all Maven projects for a user or installation, whereaspom.xmlis specific to a single project. -
Profiles:
settings.xmlcan activate profiles globally, whilepom.xmlcan define and activate profiles specific to the project. -
Credentials: It is common to store repository credentials in
settings.xmlto keep them separate from the project configuration.
-
Credentials: Store sensitive information like repository credentials in
settings.xmlto keep yourpom.xmlclean and secure. -
Global Repositories: Configure common repositories that are used across multiple projects in
settings.xml. -
Project-Specific Repositories: Use
pom.xmlfor repositories specific to a single project or that should override the global settings.
By configuring repositories appropriately in either settings.xml or pom.xml, you can ensure that Maven resolves dependencies and plugins correctly based on your project's requirements and your environment setup.