return targetVersion;
}
// Not in Java 5: @Override
public void execute() throws MojoExecutionException, MojoFailureException {
final Log log = getLog();
if (skip) {
log.info("Skipping forbidden-apis checks.");
return;
}
// In multi-module projects, one may want to configure the plugin in the parent/root POM.
// However, it should not be executed for this type of POMs.
if ("pom".equals(packaging)) {
log.info("Skipping execution for packaging \"" + packaging + "\"");
return;
}
// set default param:
if (includes == null) includes = new String[] {"**/*.class"};
final URL[] urls;
try {
final List<String> cp = getClassPathElements();
urls = new URL[cp.size()];
int i = 0;
for (final String cpElement : cp) {
urls[i++] = new File(cpElement).toURI().toURL();
}
assert i == urls.length;
if (log.isDebugEnabled()) log.debug("Compile Classpath: " + Arrays.toString(urls));
} catch (MalformedURLException e) {
throw new MojoExecutionException("Failed to build classpath: " + e);
}
URLClassLoader urlLoader = null;
final ClassLoader loader = (urls.length > 0) ?
(urlLoader = URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader())) :
ClassLoader.getSystemClassLoader();
try {
final Checker checker = new Checker(loader, internalRuntimeForbidden, failOnMissingClasses, failOnUnresolvableSignatures) {
@Override
protected void logError(String msg) {
log.error(msg);
}
@Override
protected void logWarn(String msg) {
log.warn(msg);
}
@Override
protected void logInfo(String msg) {
log.info(msg);
}
};
if (!checker.isSupportedJDK) {
final String msg = String.format(Locale.ENGLISH,
"Your Java runtime (%s %s) is not supported by the forbiddenapis MOJO. Please run the checks with a supported JDK!",
System.getProperty("java.runtime.name"), System.getProperty("java.runtime.version"));
if (failOnUnsupportedJava) {
throw new MojoExecutionException(msg);
} else {
log.warn(msg);
return;
}
}
log.info("Scanning for classes to check...");
final File classesDirectory = getClassesDirectory();
if (!classesDirectory.exists()) {
log.warn("Classes directory does not exist, forbiddenapis check skipped: " + classesDirectory);
return;
}
final DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(classesDirectory);
ds.setCaseSensitive(true);
ds.setIncludes(includes);
ds.setExcludes(excludes);
ds.addDefaultExcludes();
ds.scan();
final String[] files = ds.getIncludedFiles();
if (files.length == 0) {
log.warn(String.format(Locale.ENGLISH,
"No classes found in '%s' (includes=%s, excludes=%s), forbiddenapis check skipped.",
classesDirectory.toString(), Arrays.toString(includes), Arrays.toString(excludes)));
return;
}
try {
final String sig = (signatures != null) ? signatures.trim() : null;
if (sig != null && sig.length() != 0) {
log.info("Reading inline API signatures...");
checker.parseSignaturesString(sig);
}
if (bundledSignatures != null) {
String targetVersion = getTargetVersion();
if ("".equals(targetVersion)) targetVersion = null;
if (targetVersion == null) {
log.warn("The 'targetVersion' parameter or '${maven.compiler.target}' property is missing. " +
"Trying to read bundled JDK signatures without compiler target. " +
"You have to explicitely specify the version in the resource name.");
}
for (String bs : bundledSignatures) {
log.info("Reading bundled API signatures: " + bs);
checker.parseBundledSignatures(bs, targetVersion);
}
}
if (signaturesFiles != null) for (final File f : signaturesFiles) {
log.info("Reading API signatures: " + f);
checker.parseSignaturesFile(new FileInputStream(f));
}
} catch (IOException ioe) {
throw new MojoExecutionException("IO problem while reading files with API signatures: " + ioe);
} catch (ParseException pe) {
throw new MojoExecutionException("Parsing signatures failed: " + pe.getMessage());
}
if (checker.hasNoSignatures()) {
if (failOnUnresolvableSignatures) {
throw new MojoExecutionException("No API signatures found; use parameters 'signatures', 'bundledSignatures', and/or 'signaturesFiles' to define those!");
} else {
log.info("Skipping execution because no API signatures are available.");
return;
}
}
log.info("Loading classes to check...");
try {
for (String f : files) {
checker.addClassToCheck(new FileInputStream(new File(classesDirectory, f)));
}
} catch (IOException ioe) {
throw new MojoExecutionException("Failed to load one of the given class files: " + ioe);
}
log.info("Scanning for API signatures and dependencies...");
try {
checker.run();
} catch (ForbiddenApiException fae) {
throw new MojoFailureException(fae.getMessage());
}