You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.PHONY规则触发“nothing to be done for”错误的技术咨询

Fixing "Nothing to be done for 'test'" and Dynamic Test Detection in Your Makefile

Let's break down why your current setup is failing, then fix it with a clean, reliable Makefile that behaves exactly as you expect.

What's Going Wrong?

Your issues stem from two core mistakes:

  1. You're targeting actual test files: When you use tests/%.java as a target, Make sees these as existing, unmodified files. It assumes they're already "up-to-date" and skips running the test rule, hence the "Nothing to be done" message.
  2. Static wildcard expansion: The $(wildcard tests/*.java) variable expands once when Make first loads the file. New test files added later won't be picked up unless you force Make to re-parse the file (like running make clean).

Fixed Makefile

Here's the corrected version that resolves both problems:

PROG = Test
# Generate virtual test targets (not actual files) from test Java files
TEST_TARGETS = $(patsubst tests/%.java, tests/%.test, $(wildcard tests/*.java))
.SUFFIXES: .java .class

JCC = javac
JAR = jar

all: $(PROG).jar

# Mark test targets as PHONY to ensure they always run
.PHONY: test clean $(TEST_TARGETS)

test: $(PROG).jar $(TEST_TARGETS)

# Rule for virtual test targets - runs the corresponding Java test file
tests/%.test:
	@echo "#### testing tests/$*.java ####"
	@java -jar $(PROG).jar tests/$*.java
	@echo "> Passed"

$(PROG).jar: Main.class
	$(JAR) cmf $(PROG)MainClass.txt $(PROG).jar $^

Main.class: Main.java
	$(JCC) $^

clean:
	rm -f *.class $(PROG).jar

Key Fixes Explained

  1. Virtual Test Targets:
    We use $(patsubst) to convert each tests/%.java file into a non-existent tests/%.test target. Since these targets don't correspond to real files, Make will always try to run their rules, no matter the state of your test files.

  2. Proper PHONY Usage:
    Only mark test and the virtual $(TEST_TARGETS) as PHONY. Marking actual test files as PHONY breaks Make's dependency checking logic, which was part of your original problem.

  3. Dynamic Test Detection:
    If you add new test files and want Make to pick them up without make clean, run:

    make test -B
    

    The -B flag (short for --always-make) forces Make to re-parse the wildcard variable and run all tests, even if it thinks things are up-to-date.

How to Verify

  • Run make test: It will execute all tests in the tests/ directory every time, no "Nothing to be done" messages.
  • Add a new .java file to tests/, then run make test -B: The new test will be picked up and executed automatically.

内容的提问来源于stack exchange,提问作者collin.clark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:29:50