0

I've got a maven project that uses the buildnumber-maven-plugin. If I run mvn validate I see it's working:

[INFO] --- buildnumber-maven-plugin:1.3:create (default) @ myproject ---
[INFO] Executing: /bin/sh -c cd /Users/rob/Workspace/myproject && git rev-parse --verify HEAD
[INFO] Storing buildNumber: 5d315d8d1a43c3289fbf114c379fa1a3d3787044 at timestamp: 1477059166424

But if I run mvn resources:resources the filtered file does not pick it up:

[INFO] --- maven-resources-plugin:2.6:resources (default-cli) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource

The pom.xml has:

<build>
    ...
    <resources>         
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>version.txt</include>
            </includes>
        </resource>

version.txt has:

${buildNumber}

But after maven runs, no filtering:

> cat target/classes/version.txt
${buildNumber}

The build number config in pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.3</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals><goal>create</goal></goals>
        </execution>

I don't know enough Maven. Shouldn't running the resources "goal" also get the buildNumber property?

Rob N
  • 15,024
  • 17
  • 92
  • 165
  • Well if you run `mvn resources:resources`, the phase `validate` is not called, since you're directly invoking a plugin goal. So it's normal that the `buildnumber-maven-plugin` isn't invoked. Why are you doing that in the first place, instead of executing something like `mvn clean package`? – Tunaki Oct 21 '16 at 14:37
  • According to this accepted answer: http://stackoverflow.com/a/16205812/327572 "If you specify a goal when you execute Maven then it will still run all phases up to the phase for that goal." Is that wrong? I'm doing it because I'm trying to get an IntelliJ's incremental "Make project" to do some of what maven does on a full build. You can select the plugin:goal to run after the IDE's "make". – Rob N Oct 21 '16 at 15:37
  • That is indeed wrong, if you specify a goal, Maven will not execute the phases before a possible default phase this goal is bound to. It will just execute that goal. You can easily check that with the logs. Run `mvn resources:resources`, you will not see anything executed apart from exactly that goal. [This answer](http://stackoverflow.com/a/32759400/1743880) is better. – Tunaki Oct 21 '16 at 15:50
  • Another way to convince yourself: run `mvn clean jar:jar`. You'll get an empty JAR (or an exception depending on the jar plugin version). Meaning no classes were compiled. – Tunaki Oct 21 '16 at 16:03

1 Answers1

1

There is a difference in the commands that you execute:

mvn validate executes the maven phase "valdate": meaning all phases that come before (in this case none)

mvn resources:resources is a shortcut for executing the goal "resources" on the resources plugin. Actually its a shortcut for executing: org.apache.maven.plugins:maven-resources-plugin:3.0.1:resources. These short names are resolved by maven and very typical for plugins in the Apache namespace.

As you can see on the maven life-cycle page the goal you may look for is: "mvn process-resources". That phase has a default plugin binding to "resources:resources" which will run the resource plugin. Since you execute a phase all phases before that will be run too, including the build number plugin.

The ":" indicates the difference for the maven command line.

wemu
  • 7,952
  • 4
  • 30
  • 59
  • So is this answer wrong then? http://stackoverflow.com/a/16205812/327572? It says a goal runs previous phases too. – Rob N Oct 21 '16 at 15:44
  • well maybe not. I think the answer refers to writing your own plugin and mapping it to a phase when writing it. But I'm not sure if you can make maven execute a lifecycle from a goal other than making it a requirement for the plugin. There is another answer in the question you linked I like much better than the one that was accepted. Usually you specify the phase to execute. Plugins consist of goals. It easy to mix them up, mvn compile will execute the life-cycle while mvn compiler:compile will just execute the goal on that plugin. If the plugin requires a project it will fail. – wemu Oct 22 '16 at 16:17
  • I don't understand why this is the accepted answer. It is wrong and does not work. The resources:resources target does indeed execute after the buildNumber plugin and filtering should pick up that replacement. It does not. – user1283068 Feb 04 '20 at 13:11
  • only if you execute a maven phase and not the goal directly. if you do that the build number plugin will not run as there is no lifecycle beeing executed, only the resources plugin mojo. as stated running "mvn process-resources" should run both the buildnumber plugin and the resources plugin, as its a phase not a goal. – wemu Feb 04 '20 at 15:00