in uimafit-maven-plugin/src/main/java/org/apache/uima/fit/maven/GenerateDescriptorsMojo.java [129:252]
public void execute() throws MojoExecutionException {
if (isSkipped()) {
return;
}
// add the generated sources to the build
if (!outputDirectory.exists()) {
outputDirectory.mkdirs();
buildContext.refresh(outputDirectory);
}
// Get the compiled classes from this project
String[] files = FileUtils.getFilesFromExtension(project.getBuild().getOutputDirectory(),
new String[] { "class" });
componentLoader = Util.getClassloader(project, getLog(), includeScope);
// List of components that is later written to META-INF/org.apache.uima.fit/components.txt
StringBuilder componentsManifest = new StringBuilder();
int countGenerated = 0;
for (String file : files) {
String base = file.substring(0, file.length() - 6);
String clazzPath = base.substring(project.getBuild().getOutputDirectory().length() + 1);
String clazzName = clazzPath.replace(File.separator, ".");
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
Class clazz = componentLoader.loadClass(clazzName);
// Make the componentLoader available to uimaFIT e.g. to resolve imports
Thread.currentThread().setContextClassLoader(componentLoader);
// Do not generate descriptors for abstract classes, they cannot be instantiated.
if (Modifier.isAbstract(clazz.getModifiers())) {
continue;
}
ResourceCreationSpecifier desc = null;
ProcessingResourceMetaData metadata = null;
switch (Util.getType(componentLoader, clazz)) {
case ANALYSIS_ENGINE:
AnalysisEngineDescription aeDesc = createEngineDescription(clazz);
metadata = aeDesc.getAnalysisEngineMetaData();
desc = aeDesc;
break;
case COLLECTION_READER:
CollectionReaderDescription crDesc = createReaderDescription(clazz);
metadata = crDesc.getCollectionReaderMetaData();
desc = crDesc;
default:
// Do nothing
}
if (desc != null) {
switch (addTypeSystemDescriptions) {
case EMBEDDED:
embedTypeSystems(metadata);
break;
case NONE: // fall-through
default:
// Do nothing
}
File out = new File(outputDirectory, clazzPath + ".xml");
out.getParentFile().mkdirs();
toXML(desc, out.getPath());
countGenerated++;
// Remember component
componentsManifest.append("classpath*:").append(clazzPath + ".xml").append('\n');
}
} catch (SAXException e) {
handleError("Cannot serialize descriptor for [" + clazzName + "]", e);
} catch (IOException e) {
handleError("Cannot write descriptor for [" + clazzName + "]", e);
} catch (ClassNotFoundException e) {
handleError("Cannot analyze class [" + clazzName + "]", e);
} catch (ResourceInitializationException e) {
handleError("Cannot generate descriptor for [" + clazzName + "]", e);
} finally {
Thread.currentThread().setContextClassLoader(classLoader);
}
}
getLog().info(
"Generated " + countGenerated + " descriptor" + (countGenerated != 1 ? "s." : "."));
// Write META-INF/org.apache.uima.fit/components.txt unless skipped and unless there are no
// components
if (!skipComponentsManifest && componentsManifest.length() > 0) {
File path = new File(outputDirectory, "META-INF/org.apache.uima.fit/components.txt");
FileUtils.mkdir(path.getParent());
try {
FileUtils.fileWrite(path.getPath(), encoding, componentsManifest.toString());
} catch (IOException e) {
handleError("Cannot write components manifest to [" + path + "]"
+ ExceptionUtils.getRootCauseMessage(e), e);
}
}
if (addOutputDirectoryAsResourceDirectory && countGenerated > 0) {
Path absoluteDescriptorOutputPath = outputDirectory.toPath().toAbsolutePath();
Path absoluteBuildOutputDirectory = Paths.get(project.getBuild().getOutputDirectory())
.toAbsolutePath();
Path absoluteBuildTestOutputDirectory = Paths.get(project.getBuild().getTestOutputDirectory())
.toAbsolutePath();
// Add the output folder as a new resource folder if any descriptors were generated and
// only
// if the descriptors were generated directly into the build output folder. The latter
// can
// be the case if the mojo is executed in a late build phase where the resources plugin
// doesn't run anymore.
if (!absoluteBuildOutputDirectory.equals(absoluteDescriptorOutputPath)
&& !absoluteBuildTestOutputDirectory.equals(absoluteDescriptorOutputPath)) {
Resource resource = new Resource();
resource.setDirectory(outputDirectory.getPath());
resource.setFiltering(false);
resource.addInclude("**/*.xml");
project.addResource(resource);
}
}
}