In the previous tutorial, I showed you some of the basic configurations in the Apache Maven settings.xml file with <localRepository />, <interactiveMode />, <offline />, <pluginGroups />, <proxies /> tags. In this tutorial, we will learn more about some of the more important parts of the Apache Maven configuration file using the <profiles />, <mirrors />, <servers />, <activeProfiles />.
The first is the <profiles> tag.
This tag will contain many <profile> tags that allow us to define many different profiles.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<profiles> <profile> <id>huongdanjava</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>huongdanjava-releases</id> <name>Huong Dan Java Releases</name> <url>http://localhost:8081/repository/huongdanjava-releases/</url> <layout>default</layout> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>huongdanjava-releases</id> <name>Huong Dan Java Releases</name> <url>http://localhost:8081/repository/huongdanjava-releases/</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> |
For each profile, we need to define a unique id to distinguish it from other profiles by using the <id> tag inside this tag. The <profile> tag will also contain some child tags:
- <activation>
This will decide whether this profile is used by default when we run the Apache Maven command. To do this, we need to declare the <activeByDefault> tag inside this <activation> tag, the value of <activeByDefault> will be true or false and this value will determine whether this profile is used or not?
- <repositories>
This tab is used to configure the information about the Maven Repository contains the dependencies that you need to use in your projects. Each Maven Repository will be declared with a <repository> tag inside this <repositories> tag. The information of the Maven Repository will be configured through the <id>, <name>, <url> tags as you see in the example above. <layout> means that we will use the Apache Maven standard format.
Please refer to the tutorial Create Maven Repository in Nexus Repository Manager to learn how to create a new Maven Repository for yourself.
- <pluginRepositories>
This tag is similar to the <repositories> tag, which will contain <pluginRepository> tags that define the Maven Repository that contains the plugins that you need to use in your projects. We also have information on <id>, <name>, <url> that needs to be configured for the <pluginRepository> section.
- <properties>
This tag is not in the example above but if you need to predefine some properties to use in your Maven projects then please declare this tag. It’s the same as when we declare the <properties> tag in the pom.xml file. 🙂
Next, we’ll look at the <mirrors> tag
This tag is used to declare the Maven Repository can replace the Maven Repository we have declared in the <repository> or <pluginRepository> tag.
For example:
1 2 3 4 5 6 7 8 |
<mirrors> <mirror> <id>huongdanjava-releases-mirror</id> <name>Huong Dan Java Releases Mirror</name> <url>http://192.168.2.21:8081/repository/huongdanjava-releases/</url> <mirrorOf>huongdanjava-releases</mirrorOf> </mirror> </mirrors> |
We can declare many <mirror> tags within the <mirrors> tag. The information for each <mirror> is the same as when we declared the <repository> or <pluginRepository> tags, only one more tag to specify which Maven Repository will be replaced with the Maven Repository declared in this <mirror> tag. This is the <mirrorOf> tag.
In the example above, I declare the id of the repository in the example of the <profiles> tag: huongdanjava-releases, will have the mirror of the repository huongdanjava-releases-mirror.
Next, we’ll look at the <servers> tag.
This tab helps us to declare username and password information for the Maven Repository when the use of this Maven Repository needs to be logged.
It will consist of one or more <server> tags, for example:
1 2 3 4 5 6 7 |
<servers> <server> <id>huongdanjava-releases</id> <username>deployer</username> <password>123456</password> </server> </servers> |
We use the <username>, <password> tags to declare the username, password, and <id> tags to declare the id of the Maven Repository we need to use this username and password.
Finally, the <activeProfiles /> tag.
This tab helps us define the profiles we need to use by default.
For example:
1 2 3 |
<activeProfiles> <activeProfile>huongdanjava</activeProfile> </activeProfiles> |
The value of the <activeProfile> tag will be the id of one profile!