4

In my parent pom I have

<build>
 <plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
            <execution>
                <phase>validate</phase>
                <goals>
                <goal>create</goal>
                </goals>
            </execution>
        </executions>
            <configuration>
                <doCheck>true</doCheck>
                <doUpdate>true</doUpdate>
            </configuration>
        </plugin>

In a child module run a java Main class with the above default properties: buildName and scmBranch:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
    <execution>
        <id>my-execution</id>
        <phase>package</phase>
        <goals>
            <goal>exec</goal>
            </goals>
        </execution>
        </executions>
            <configuration>
            <executable>java</executable>
                <arguments>
                    <argument>-classpath</argument>
                        <classpath />
                        <argument>${MyMainClass}</argument>
                        <argument>test-${scmBranch}-${buildNumber}</argument>
                </arguments>
            </configuration>

But the variables are never substituted/expanded in my main class. Any ideas?

From the documentation:

required: false
type: java.lang.String
expression: ${maven.buildNumber.buildNumberPropertyName}
default: buildNumber

You can rename the buildNumber property name to another 
 property name if desired.

As I understand the buildNumber is a property provided when you include the buildnumber-maven-plugin

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
u123
  • 15,603
  • 58
  • 186
  • 303
  • Where are the properties scmBranch and buildNumber defined? Could you paste that code too? – Diego Pino Dec 20 '12 at 22:10
  • As I understand they are provided when you use the plugin – u123 Dec 20 '12 at 22:17
  • Do you have the `` block defined in your pom? That is required for the build number plugin to know how to talk to your source control system. Also, I recommend you set a value for ``, so you can easily see a bad value when something goes wrong, like `didn't work!`. Also, can you please paste the output of maven? The build number plugin should be logging some information about what it is doing when invoked. – cambecc Dec 21 '12 at 03:33
  • Can you remove the definition of the life cycle phase and use the default life cycle phase of the plugin and recheck? How have you calle mvn ? **mvn clean package** ? – khmarbaise Dec 21 '12 at 08:33
  • Removing from life cycle does nothing and yes I build with mvn clean package – u123 Dec 21 '12 at 14:57
  • Defining a dummy scm seems to be the way to go: http://stackoverflow.com/questions/9115765/is-it-possible-to-use-maven-buildnumber-plugin-to-generate-build-number-without but how do I make it install into .m2 using the build number? – u123 Dec 26 '12 at 11:19

1 Answers1

2

In your parent pom, you have not defined the pluginManagement tag. Without it, your child module does not inherit the plugins you have defined, they are used by the parent pom only. This is what you need to add to the parent pom:

<build>
    <!-- plugins used by this POM and all inheriting POMs -->
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <doCheck>true</doCheck>
                    <doUpdate>true</doUpdate>
                </configuration>
            </plugin>
            <!-- other plugins -->
        </plugins>
    </pluginManagement>
</build>

Now your child module has access to the buildnumber-maven-plugin.

Tarek
  • 3,080
  • 5
  • 38
  • 54
  • 1
    Tried this proposal, but it did not work. ${buildNumber} is not propagated, stays "null". – Wrench Nov 07 '16 at 16:14
  • 1
    Hi, do you declare buildnumber-maven-plugin in your child's ? You'd only need to redeclare groupId and artifactId, the rest is inherited. – Tarek Nov 08 '16 at 17:50
  • 1
    After pulling, reorganising, messing etc with my pom's, I must take my previous statement back. Your example works great and I'm now enjoying commit id in all child modules jar's in build.properties. – Wrench Nov 08 '16 at 18:03
  • 1
    Great! Feel free to edit my answer with any missing information. Thanks! – Tarek Nov 10 '16 at 19:30