0

I'm trying to write a make recipe to change the jar name from the package version to the git commit.

COMMIT := $(shell git log -1 --format=%h)
MVN_VERSION := $(shell mvn -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive exec:exec)

jar:
    mvn package assembly:single

rename:
    @echo $(MVN_VERSION)
    @echo $(COMMIT)
    mv target/myjar-$(MVN_VERSION).jar target/myjar-$(COMMIT).jar

I know that the respective shell commands work (maven from this answer) on their own in bash, but when I run, nothing gets stored in the variable.

> make rename

4cbee79
mv target/myjar-.jar target/myjar-4cbee79.jar
mv: rename target/myjar-.jar to target/myjar-4cbee79.jar: No such file or directory
gregmacfarlane
  • 2,121
  • 3
  • 24
  • 53
  • 1
    I would change the version of the project into a git sha which can more or less easily achieved by using http://maven.apache.org/maven-ci-friendly.html (Needs at least Maven 3.5.X) and furthermore why are you calling `assembly:single` instead of configuring assembly plugin in the way that you can simply call `mvn package` ? – khmarbaise Mar 09 '18 at 19:27
  • The short answer is that I'm new to Maven. Thanks for the tips, they helped greatly. – gregmacfarlane Mar 09 '18 at 19:46
  • [Tips for writing shell scripts within makefiles:](https://stackoverflow.com/a/29085684/86967) – Brent Bradburn Mar 09 '18 at 19:48
  • In a Maven project, makefiles are very rarely the correct solution. You figure out how to change the configuration of the appropriate plugin, if possible so you can stay in the Maven life cycle. – Thorbjørn Ravn Andersen Mar 10 '18 at 00:10

1 Answers1

2

You have to escape all dollar signs that you want make to pass through to the shell, by doubling them. Otherwise make expands them as make variables:

MVN_VERSION := $(shell mvn -q -Dexec.executable="echo" -Dexec.args='$${project.version}' --non-recursive exec:exec)

Without that you're passing the option -Dexec.args='' (empty string) because make expanded the make variable project.version, which was never set.

MadScientist
  • 92,819
  • 9
  • 109
  • 136