Jane Wilkie
Jane Wilkie

Reputation: 1743

Using a variable in Maven from the super pom

I am giving myself a crash course in Maven and stumbled across a great plugin called buildnumber: http://www.mojohaus.org/buildnumber-maven-plugin/create-mojo.html

I have setup a VERY rudimentary, beginners project and in the pom.xml file I am successfully having the ${buildNumber} to interpolate.

I have the pom.xml below (I apologize for the length of it).

<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.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <scm>
    <connection>scm:git:ssh://[email protected]/XXXX/bb101repo.git</connection>
    <developerConnection>scm:git:ssh://[email protected]/XXX/bb101repo.git</developerConnection>
    <url>https://bitbucket.org/XXXX/bb101repo.git</url>
  </scm>
  <build>
    <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
            <execution>
                <phase>validate</phase>
                <goals>
                    <goal>create</goal>
                </goals>
            </execution>
        </executions>
            <configuration>
              <shortRevisionLength>5</shortRevisionLength>
<!--                doCheck : Check for locally modified files. If true build will fail if local modifications have not been commited -->
<!--                doUpdate : Update the local copy of your repo. If true the plugin will update your local files with the remote modifications before building -->
              <doCheck>true</doCheck>
              <doUpdate>false</doUpdate>
            </configuration>
    </plugin>
    </plugins>

  <finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>

  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

When I run mvn package, it works as intended.

I have another file in my project (under src) called info.xml and it's below.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>FMS Help</display-name>
  <version>${buildNumber}</version>
</web-app>

How do I get the ${buildNumber} to unpack in my tag under target?

I'm guessing it's a really simple solution, but I'm stumped. Again, I'm a complete n00b at Maven and any pointer in the right direction would be appreciated. JW

Upvotes: 0

Views: 414

Answers (1)

Chris
Chris

Reputation: 23179

You need to tell Maven to apply filtering to your resources. Check out the resources plugin documentation here: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

To avoid applying filtering to all your source files, you'd either need to set up the proper include/exclude paths or move the info.xml file to the resources directory.

Upvotes: 1

Related Questions