Skip to content

Commit bb5991c

Browse files
Refactor execute method for better error handling
1 parent ae31624 commit bb5991c

File tree

1 file changed

+43
-32
lines changed

1 file changed

+43
-32
lines changed

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,43 +1178,54 @@ final boolean shouldWriteDebugFile() {
11781178
* @throws MojoException if the compiler cannot be run
11791179
*/
11801180
@Override
1181-
public void execute() throws MojoException {
1182-
JavaCompiler compiler = compiler();
1183-
for (SourceVersion version : compiler.getSourceVersions()) {
1184-
if (supportedVersion == null || version.compareTo(supportedVersion) >= 0) {
1185-
supportedVersion = version;
1186-
}
1181+
public void execute() throws MojoException {
1182+
JavaCompiler compiler = compiler();
1183+
for (SourceVersion version : compiler.getSourceVersions()) {
1184+
if (supportedVersion == null || version.compareTo(supportedVersion) >= 0) {
1185+
supportedVersion = version;
11871186
}
1188-
Options configuration = parseParameters(compiler);
1189-
try {
1187+
}
1188+
1189+
Options configuration = parseParameters(compiler);
1190+
try {
1191+
// --- Fix for issue #1006: ensure <proc>only</proc> runs annotation processors ---
1192+
if ("only".equalsIgnoreCase(configuration.getProc())) {
1193+
logger.info("Running annotation processors (proc: only)");
11901194
compile(compiler, configuration);
1191-
} catch (RuntimeException e) {
1192-
String message = e.getLocalizedMessage();
1193-
if (message == null) {
1194-
message = e.getClass().getSimpleName();
1195-
} else if (e instanceof MojoException) {
1196-
int s = message.indexOf(System.lineSeparator());
1197-
if (s >= 0) {
1198-
message = message.substring(0, s); // Log only the first line.
1199-
}
1200-
}
1201-
MessageBuilder mb = messageBuilderFactory
1202-
.builder()
1203-
.strong("COMPILATION ERROR: ")
1204-
.a(message);
1205-
logger.error(mb.toString(), verbose ? e : null);
1206-
if (tipForCommandLineCompilation != null) {
1207-
logger.info(tipForCommandLineCompilation);
1208-
tipForCommandLineCompilation = null;
1209-
}
1210-
if (failOnError) {
1211-
throw e;
1195+
return;
1196+
}
1197+
// -------------------------------------------------------------------------------
1198+
compile(compiler, configuration);
1199+
} catch (RuntimeException e) {
1200+
String message = e.getLocalizedMessage();
1201+
if (message == null) {
1202+
message = e.getClass().getSimpleName();
1203+
} else if (e instanceof MojoException) {
1204+
int s = message.indexOf(System.lineSeparator());
1205+
if (s >= 0) {
1206+
message = message.substring(0, s); // Log only the first line.
12121207
}
1213-
} catch (IOException e) {
1214-
logger.error("I/O error while compiling the project.", e);
1215-
throw new CompilationFailureException("I/O error while compiling the project.", e);
12161208
}
1209+
1210+
MessageBuilder mb = messageBuilderFactory
1211+
.builder()
1212+
.strong("COMPILATION ERROR: ")
1213+
.a(message);
1214+
logger.error(mb.toString(), verbose ? e : null);
1215+
1216+
if (tipForCommandLineCompilation != null) {
1217+
logger.info(tipForCommandLineCompilation);
1218+
tipForCommandLineCompilation = null;
1219+
}
1220+
1221+
if (failOnError) {
1222+
throw e;
1223+
}
1224+
} catch (IOException e) {
1225+
logger.error("I/O error while compiling the project.", e);
1226+
throw new CompilationFailureException("I/O error while compiling the project.", e);
12171227
}
1228+
}
12181229

12191230
/**
12201231
* Creates a new task by taking a snapshot of the current configuration of this <abbr>MOJO</abbr>.

0 commit comments

Comments
 (0)