phitreaj
phitreaj

Reputation: 3

makefile for java, recompile only when files changed

I am learning how to write makefiles and ran into a problem. I have a makefile for a java application but every time i execute the target jar, the whole code gets recompiled, even if nothing changed since the last call. I read often, that it is an advantage of make to only recompile, if dependencies of a target (e.g. 'jar') have changed, but it is not the case here. What can solve the problem?

FOLDER=some/path

jar: A.class B.class 
    jar cf output.jar  $(FOLDER)/bin/a/A.class $(FOLDER)/bin/b/B.class 

A.class: 
    javac $(FOLDER)/a/A.java -d $(FOLDER)/bin -implicit:none

B.class:
    javac $(FOLDER)/b/B.java -d $(FOLDER)/bin -implicit:none-implicit:none

Upvotes: 0

Views: 999

Answers (1)

Shmil The Cat
Shmil The Cat

Reputation: 4668

Use explicit paths to your class files and don't forget the pre-requisite within the rule (the class depends on the Java source):

FOLDER=some/path

jar: $(FOLDER)/bin/a/A.class $(FOLDER)/bin/b/B.class
    jar cf output.jar  $(FOLDER)/bin/a/A.class $(FOLDER)/bin/b/B.class 

$(FOLDER)/bin/a/A.class: $(FOLDER)/a/A.java
    javac $(FOLDER)/a/A.java -d $(FOLDER)/bin -implicit:none

$(FOLDER)/bin/b/B.class: $(FOLDER)/b/B.java
    javac $(FOLDER)/b/B.java -d $(FOLDER)/bin -implicit:none-implicit:none

Upvotes: 1

Related Questions