How to get given task's all variables

I want to use java code to get Task’s all (task local + parent scope) variables in Camunda. how to do that ? I tried using HistoricVariableInstanceQuery or HistoricDetailQuery but not getting results.

Historic tables are for finished activities. You should use runtime tables.
I think the easiest way is using TaskService: https://docs.camunda.org/javadoc/camunda-bpm-platform/7.12/org/camunda/bpm/engine/TaskService.html#getVariables-java.lang.String-
You can get TaskService from ProcessEngine or autowire it when using Spring boot.

Thanks for quick reply. I also want to get completed task’s all variables. How do I do that?

@Suhas_Kulkarni @neplatnyudaj refer the below post.