How do I display the value of a settings.xml element?
Author: Deron Eriksson
Description: This tutorial describes how to display the value of a settings.xml element.
Tutorial created using:
Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)
With mavenSW, a settings.xml element can be accessed as a property, where the property name begins with "settings.". Thus, a settings.xml element such as "localRepository" can be accessed via ${settings.localRepository}. The maven-antrun-plugin:run goal can be used to run AntSW tasks. We can use the "echo" Ant task to display a value of a property. The following fragment of a pom.xml file binds the antrun:goal to the validate build lifecycle phase. It uses the "echo" Ant task to display the value of the settings.xml localRepository element. Fragment of pom.xml to display value of settings.xml localRepository element<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Displaying value of settings.xml element</echo> <echo>[settings.localRepository] ${settings.localRepository}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> Performing a "mvn validate" on my "mytest" project generates the following console output: Console output of 'mvn validate' on 'mytest' project[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building mytest [INFO] task-segment: [validate] [INFO] ------------------------------------------------------------------------ [INFO] [antrun:run {execution: default}] [INFO] Executing tasks [echo] Displaying value of settings.xml element [echo] [settings.localRepository] /dev/m2repo [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: < 1 second [INFO] Finished at: Mon Feb 11 13:58:37 PST 2008 [INFO] Final Memory: 2M/4M [INFO] ------------------------------------------------------------------------ As you can see, the value of the settings.xml localRepository element is properly displayed. The "mytest" project's full pom.xml file is shown here. 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>com.maventest</groupId> <artifactId>mytest</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>mytest</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.3</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>Displaying value of settings.xml element</echo> <echo>[settings.localRepository] ${settings.localRepository}</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |