Java Makefile:如何动态编译包内的文件

时间:2014-10-17 22:08:20

标签: java makefile

我需要编译大量的文件,这些文件位于2个目录中,我希望make能够自己查找文件。

我在互联网上找到的所有教程都显示了如何在当前目录中执行此操作,而不是在其他目录中执行此操作。 这是我目前的代码:

JFLAGS = -g
JC = javac

# clear default targets

.SUFFIXES: .java .class

# compile files

.java.class:
    $(JC) $(JFLAGS) $*.java

# directories to look for java files

PACKAGES := \
    Core \
    Card \

# Initialize CLASSES with main.java

CLASSES := main.java

# I have no clue how this works, but someone told me to do it.

.PHONY: default clean $(PACKAGES)

# should add all java files to CLASSES

$(PACKAGES):
    $(eval CLASSES := $(CLASSES) $@/*.java)

# calls sources builder and compiler

default: $(PACKAGES) classes

# calls compiler for each file

classes: $(CLASSES:.java=.class)

clean:
    $(foreach DIR,$(PACKAGES),$(eval $(RM $(DIR)/*.class) ) )

正如您所看到的,我尝试通过迭代和规则访问目录,但这些方法都不起作用。

make clean给了我nothing to be done for cleanmake给了我nothing to be done for Core,即使Core填充了java文件并且策略性地放置了.class文件。

我该如何解决这个问题?

我使用的基本makefile可以在http://www.cs.swarthmore.edu/~newhall/unixhelp/javamakefiles.html

找到

1 个答案:

答案 0 :(得分:0)

我使用shell find来查找文件:

JFLAGS = -g
JC = javac
.SUFFIXES: .java .class

.java.class:
    $(JC) $(JFLAGS) $*.java

PACKAGES := \
    . \
    Core \
    Cards \
    Cards/Abstract \

CLASSES := $(shell find $(PACKAGES) -type f -name '*.java')

.PHONY: default clean

default: classes

classes: $(CLASSES:.java=.class)

clean:
    $(RM) $(shell find $(PACKAGES) -type f -name '*.class')