Makefile with conditional logic based on external variables

Elvis Ciotti
Oct 13, 2022

--

a variable passed to the makefile target MYVAR=1 make mytask` is visible with `$(MYVAR)` inside mytask

To use it with a conditional if/else, e.g. to skip a target like Maven surefire skipTests, you can use ifeq (reference here).

Full example

# Makefilemytask:
ifeq ($(MYVAR),1)
@echo "yes"
else
@echo "no"
endif

Then launch with:
make mytask to print “no”
MYVAR=1 make mytask to print “yes”

Thanks for reading !

What to do next:
- Clap if useful
- Buy me a coffee
- Follow me for more
- Read my other articles below or from my profile
- Keep in touch on LinkedIn

--

--

Elvis Ciotti
Elvis Ciotti

Written by Elvis Ciotti

Software Contractor — Java, Spring, k8s, AWS, Javascript @ London - hire me at https://elvisciotti.github.io/

No responses yet