Using spaces instead of tabs to indent is preferred because it offers consistency of layout across all editors/viewers.
But if you still want it, you can always make your own custom check for checkstyle or a custom maven plugin /ant task.
Logic shouldnt be difficult to implement either - all you have to check whether leading space on any line is greater than the tab length.
Edit: including an ant example.
Its two weeks now since you posted and you're still not happy, and I had some free time :)
So I cooked up a little ant custom task solution for you.
The Ant task
public class SpaceDetectorTask extends Task {
public static final String REGEX = "^[ ]+";
public static final Pattern p = Pattern.compile(REGEX);
private FileSet fileSet;
private boolean failOnDetection;
// Usual getters/setters
public void addFileSet(FileSet fileSet) {
this.fileSet = fileSet;
}
public void execute() {
DirectoryScanner ds = fileSet.getDirectoryScanner();
String[] files = ds.getIncludedFiles();
for (int x = 0; x <= files.length -1; x++) {
process(ds.getBasedir(), files[x]);
}
}
public void process(File dir, String file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File(dir, file)));
String line;
int linecount = 0;
System.out.println("File: " + file);
boolean ignore = false;
while((line = reader.readLine()) != null) {
linecount++;
// exclude comment blocks
if (line.contains("/**") || line.contains("*/")) {
ignore = !ignore;
continue;
}
if (!ignore) {
if (p.matcher(line).find()) {
int spcCount = line.length() - (line.replaceAll(REGEX, "")).length();
if (spcCount >= 4) { // break whenever 4 leading spaces are detected. Configure as you need.
String msg = "File: "+ file + " is using spaces as indentation.";
if (failOnDetection) {
throw new BuildException(msg);
} else {
getProject().log(msg);
}
}
}
reader.close();
}
}
} catch (IOException e) {
if (failOnDetection) {
throw new BuildException(e);
} else {
getProject().log("File: " + file + "\n" + e.getMessage());
}
}
}
In ant build.xml
- Compile the task first
Declare it
<taskdef name="detect-spaces"
classname="com.blah.blah.build.tools.SpaceDetectorTask">
<classpath>
<pathelement path="${dir.classes}"/>
<fileset dir="C:/apache-ant-1.7.1/lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</taskdef>
use it
<target name="rules.spaces">
<detect-spaces
failOnDetection="true">
<fileset dir="${dir.src.java}">
<include name="**/*.java"/>
</fileset>
</detect-spaces>
</target>
Writing up a maven/checkstyle plugin shoulnt be difficult either.