Log log = getLog();
File tmpServicesDir = new File(new File(tmpDirectory, "META-INF"), "services");
File buildServicesDir = new File(new File(buildOutputDirectory, "META-INF"), "services");
if (!tmpServicesDir.mkdirs()) {
throw new MojoExecutionException("Error while creating the directory: " +
tmpServicesDir.getPath());
}
log.debug("Initializing class scanner ...");
ClassScanner scanner = new ClassScanner(buildOutputDirectory);
for (Artifact artifact : filterArtifacts(project.getArtifacts(),
new ScopeArtifactFilter(Artifact.SCOPE_COMPILE))) {
scanner.addToClasspath(artifact.getFile());
}
List<ServiceLocator> serviceLocators =
new ArrayList<ServiceLocator>(serviceClassNames.length);
for (String serviceClassName : serviceClassNames) {
// If the user provided its own service file, skip generation
File file = new File(buildServicesDir, serviceClassName);
if (file.exists()) {
log.debug(file + " exists; don't scan for " + serviceClassName +
" implementation");
} else {
ServiceLocator sl = new ServiceLocator(serviceClassName);
serviceLocators.add(sl);
scanner.addVisitor(sl);
}
}
try {
scanner.scan();
} catch (ClassScannerException e) {
throw new MojoExecutionException("Failed to scan classes for services", e);
}
for (ServiceLocator sl : serviceLocators) {
File file = new File(tmpServicesDir, sl.getServiceClassName());
if (!sl.getImplementations().isEmpty()) {
String destFileName = "META-INF/services/" + sl.getServiceClassName();
log.info("Generating " + destFileName);
try {
Writer out = new OutputStreamWriter(new FileOutputStream(file));
try {
for (String impl : sl.getImplementations()) {
log.debug(" " + impl);
out.write(impl);
out.write("\n");
}
} finally {
out.close();
}
} catch (IOException e) {
throw new MojoExecutionException("Unable to create temporary file " + file, e);
}
archiver.addFile(file, destFileName);
}
}
}