Ordinarily, this is exactly what you want. That the build steps are the same no matter who runs the build and where.
But there are times when different environments need slightly different builds.
For example, we may want to run the cargo plugin only in our local build.
In our production build, we would not want to automatically deploy to the server.
We do this by creating Profiles.
A profile, in effect, is nothing but a name for section of the build.
Different sections of the build can be enclosed in different profiles.
Any build section enclosed in a profile will be executed only when that profile is active.
For example, let us enclose our cargo plugin in a profile, like this:
<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>
<profiles>
<profile>
<id>local-profile</id>
<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>
</profile>
</profiles>
</project>
We have defined a profile "local-profile" and enclosed the cargo plugin in it.
Our cargo plugin will be executed only when the local-profile is active.
By default, profiles are inactive.
Update your /src/main/java/webapp/index.jsp, like this:
<html>
<body>
<h2>Hello World v4!</h2>
</body>
</html>
R-click on HelloWorldWARArch project and select Run As-->Maven install.
If you look at the messages in the console, you will see that the cargo plugin was not executed.
If you point your browser to:
http://localhost:8080/HelloWorldWARArch/
you will see "Hello World v3!", further proof that the cargo plugin did not execute.
How to activate the profile?
There are several ways to activate profiles.
We will activate the profile from our settings.xml, 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>
<activeProfile>local-profile</activeProfile>
</activeProfiles>
</settings>
Now if you run Maven install, you will see the cargo plugin execute.
Refresh your browser, and you will see:
"HelloWorld v4!"
You can create different profiles for different environments (development/testing/production) and execute the correct build for each environment.
No comments:
Post a Comment