The default settings.xml is located in the <maven_home>/conf
It looks like this (after removing the comments):
<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/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
Anything you specify in the settings.xml applies to all Maven projects.
As you can see, the default settings.xml does not specify anything.
But if you were to update it by specifying servers, proxies, mirrors, repositories, etc - it would be picked up by all maven projects.
If you update the settings.xml in <maven_home>/conf, then all users of maven will be affected by your changes.
A better way to do things is to create a settings.xml in your <home directory>/.m2. If you do this, Maven merges your settings.xml with the settings.xml in <maven_home>/conf. Your settings taking precedence.
I will not describe each and every setting in settings.xml, but just show one as an example.
In our previous post, we put the username and password to Tomcat in the pom.xml.
This is not a good idea because the pom.xml gets distributed with the project and it will expose your credentials.
So we will specify our server credentials in settings.xml, which stays in our home directory.
In your <home directory>/.m2, create a settings.xml file, like this:
<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/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers>
<server>
<id>localhost</id>
<configuration>
<cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url>
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password>s3cret</cargo.remote.password>
</configuration>
</server>
</servers>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
Here we have specified a server called "localhost" and given it the url and credentials of our local tomcat server.
Now update your pom.xml to use the server in the settings.xml, like this:
(we have updated the properties in the bottom section of 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.confucius</groupId>
<artifactId>HelloWorldWARArch</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Webapp demonstrating cargo plugin</name>
<url>http://www.confucius.org</url>
<pluginRepositories>
<pluginRepository>
<id>codehaus-releases</id>
<url>http://nexus.codehaus.org/releases/</url>
</pluginRepository>
</pluginRepositories>
<build>
<finalName>HelloWorldWARArch</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>undeploy</goal>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<type>remote</type>
</container>
<deployer>
<type>remote</type>
<deployables>
<deployable>
<groupId>org.confucius</groupId>
<artifactId>HelloWorldWARArch</artifactId>
<type>war</type>
</deployable>
</deployables>
</deployer>
<configuration>
<type>runtime</type>
<properties>
<cargo.server.settings>localhost</cargo.server.settings>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
Update your /src/main/webapp/index.jsp, like this:
<html>
<body>
<h2>Hello World v3!</h2>
</body>
</html>
In your Eclipse Navigatory, R-click on HelloWorldWARArch project and select Run As-->Maven install
Now if you point your browser to:
http://localhost:8080/HelloWorldWARArch/
you will see "Hello World v3!"
This shows that the pom.xml picked up the server settings from settings.xml
You can now safely distribute this project without exposing the server credentials.
Also, you do not need to specify the credentials in each and every project. All projects will pick up the configuration from settings.xml.
No comments:
Post a Comment