Skip to content

Commit

Permalink
#229 - Add skipDuringIncrementalBuild option to enhance goal
Browse files Browse the repository at this point in the history
#228 - Add skip parameter

- Added skip and skipDuringIncrementalBuild options to both the enhance and generate goals
  • Loading branch information
reckart committed Jul 5, 2023
1 parent b2ed11d commit 67d663f
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ public class EnhanceMojo extends AbstractMojo {
@Parameter(defaultValue = "compile", required = true)
private String includeScope;

/**
* Skip plugin execution.
*/
@Parameter(property = "uima-enhance.skip", defaultValue = "false", required = true)
private boolean skip;

/**
* Skip plugin execution only during incremental builds (e.g. triggered from m2e).
*/
@Parameter(defaultValue = "false", required = true)
private boolean skipDuringIncrementalBuilds;

/**
* Start of a line containing a class name in the missing meta data report file
*/
Expand All @@ -185,9 +197,13 @@ public class EnhanceMojo extends AbstractMojo {

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (isSkipped()) {
return;
}

// Get the compiled classes from this project
String[] files = FileUtils.getFilesFromExtension(project.getBuild().getOutputDirectory(),
new String[] { "class" });
new String[] { "class" });
componentLoader = Util.getClassloader(project, getLog(), includeScope);

// Set up class pool with all the project dependencies and the project classes themselves
Expand All @@ -199,7 +215,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// Determine where to write the missing meta data report file
File reportFile = new File(project.getBuild().getDirectory(),
"uimafit-missing-meta-data-report.txt");
"uimafit-missing-meta-data-report.txt");

// Read existing report
if (generateMissingMetaDataReport) {
Expand Down Expand Up @@ -251,7 +267,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
ctClazz = classPool.get(clazzName);
} catch (NotFoundException e) {
throw new MojoExecutionException(
"Class [" + clazzName + "] not found in class pool: " + getRootCauseMessage(e), e);
"Class [" + clazzName + "] not found in class pool: " + getRootCauseMessage(e), e);
}

// Get the source file
Expand Down Expand Up @@ -289,15 +305,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
} catch (IOException e) {
throw new MojoExecutionException(
"Enhanced class [" + clazzName + "] cannot be written: " + getRootCauseMessage(e), e);
"Enhanced class [" + clazzName + "] cannot be written: " + getRootCauseMessage(e),
e);
} catch (CannotCompileException e) {
throw new MojoExecutionException(
"Enhanced class [" + clazzName + "] cannot be compiled: " + getRootCauseMessage(e), e);
"Enhanced class [" + clazzName + "] cannot be compiled: " + getRootCauseMessage(e),
e);
}
}

getLog().info("Enhanced " + countEnhanced + " class" + (countEnhanced != 1 ? "es" : "") + " ("
+ countAlreadyEnhanced + " already enhanced).");
+ countAlreadyEnhanced + " already enhanced).");

if (generateMissingMetaDataReport) {
// Remove any classes from the report that are no longer part of the build
Expand All @@ -310,11 +328,25 @@ public void execute() throws MojoExecutionException, MojoFailureException {

if (failOnMissingMetaData && !reportData.isEmpty()) {
throw new MojoFailureException("Component meta data missing. A report of the missing "
+ "meta data can be found in " + reportFile);
+ "meta data can be found in " + reportFile);
}
}
}

private boolean isSkipped() {
if (skipDuringIncrementalBuilds && buildContext.isIncremental()) {
getLog().info("Enhancement of UIMA component classes skipped in incremental build.");
return true;
}

if (skip) {
getLog().info("Enhancement of UIMA component classes skipped.");
return true;
}

return false;
}

/**
* Add the EnhancedClassFile annotation.
*/
Expand All @@ -323,7 +355,7 @@ private void markAsEnhanced(CtClass aCtClazz) {
ConstPool constPool = classFile.getConstPool();

AnnotationsAttribute annoAttr = (AnnotationsAttribute) classFile
.getAttribute(AnnotationsAttribute.visibleTag);
.getAttribute(AnnotationsAttribute.visibleTag);

// Create annotation attribute if it does not exist
if (annoAttr == null) {
Expand All @@ -344,12 +376,12 @@ private void markAsEnhanced(CtClass aCtClazz) {
* Enhance resource meta data
*/
private void enhanceResourceMetaData(JavaSource aAST, Class<?> aClazz, CtClass aCtClazz,
Multimap<String, String> aReportData) {
Multimap<String, String> aReportData) {
ClassFile classFile = aCtClazz.getClassFile();
ConstPool constPool = classFile.getConstPool();

AnnotationsAttribute annoAttr = (AnnotationsAttribute) classFile
.getAttribute(AnnotationsAttribute.visibleTag);
.getAttribute(AnnotationsAttribute.visibleTag);

// Create annotation attribute if it does not exist
if (annoAttr == null) {
Expand All @@ -362,25 +394,25 @@ private void enhanceResourceMetaData(JavaSource aAST, Class<?> aClazz, CtClass a
a = new Annotation(ResourceMetaData.class.getName(), constPool);
// Add a name, otherwise there will be none in the generated descriptor.
a.addMemberValue("name",
new StringMemberValue(ResourceMetaDataFactory.getDefaultName(aClazz), constPool));
new StringMemberValue(ResourceMetaDataFactory.getDefaultName(aClazz), constPool));
}

// Update description from JavaDoc
String doc = Util.getComponentDocumentation(aAST, aClazz.getName());
enhanceMemberValue(a, "description", doc, overrideComponentDescription,
ResourceMetaDataFactory.getDefaultDescription(aClazz), constPool, aReportData, aClazz);
ResourceMetaDataFactory.getDefaultDescription(aClazz), constPool, aReportData, aClazz);

// Update version
enhanceMemberValue(a, "version", componentVersion, overrideComponentVersion,
ResourceMetaDataFactory.getDefaultVersion(aClazz), constPool, aReportData, aClazz);
ResourceMetaDataFactory.getDefaultVersion(aClazz), constPool, aReportData, aClazz);

// Update vendor
enhanceMemberValue(a, "vendor", componentVendor, overrideComponentVendor,
ResourceMetaDataFactory.getDefaultVendor(aClazz), constPool, aReportData, aClazz);
ResourceMetaDataFactory.getDefaultVendor(aClazz), constPool, aReportData, aClazz);

// Update copyright
enhanceMemberValue(a, "copyright", componentCopyright, overrideComponentCopyright,
ResourceMetaDataFactory.getDefaultCopyright(aClazz), constPool, aReportData, aClazz);
ResourceMetaDataFactory.getDefaultCopyright(aClazz), constPool, aReportData, aClazz);

// Replace annotation
annoAttr.addAnnotation(a);
Expand All @@ -402,12 +434,11 @@ private void enhanceResourceMetaData(JavaSource aAST, Class<?> aClazz, CtClass a
* @param aOverride
* set value even if it is already set
* @param aDefault
* default value set by uimaFIT - if the member has this value, it is considered
* unset
* default value set by uimaFIT - if the member has this value, it is considered unset
*/
private void enhanceMemberValue(Annotation aAnnotation, String aName, String aNewValue,
boolean aOverride, String aDefault, ConstPool aConstPool,
Multimap<String, String> aReportData, Class<?> aClazz) {
boolean aOverride, String aDefault, ConstPool aConstPool,
Multimap<String, String> aReportData, Class<?> aClazz) {
String value = getStringMemberValue(aAnnotation, aName);
boolean isEmpty = value.length() == 0;
boolean isDefault = value.equals(aDefault);
Expand Down Expand Up @@ -438,12 +469,12 @@ private String getStringMemberValue(Annotation aAnnotation, String aValue) {
* Enhance descriptions in configuration parameters.
*/
private void enhanceConfigurationParameter(JavaSource aAST, Class<?> aClazz, CtClass aCtClazz,
Multimap<String, String> aReportData) throws MojoExecutionException {
Multimap<String, String> aReportData) throws MojoExecutionException {
// Get the parameter name constants
Map<String, String> parameterNameFields = getParameterConstants(aClazz,
parameterNameConstantPrefixes);
parameterNameConstantPrefixes);
Map<String, String> resourceNameFields = getParameterConstants(aClazz,
externalResourceNameConstantPrefixes);
externalResourceNameConstantPrefixes);

// Fetch configuration parameters from the @ConfigurationParameter annotations in the
// compiled class. We only need the fields in the class itself. Superclasses should be
Expand All @@ -460,7 +491,7 @@ private void enhanceConfigurationParameter(JavaSource aAST, Class<?> aClazz, CtC
pname = ConfigurationParameterFactory.createPrimitiveParameter(field).getName();
// Extract JavaDoc for this resource from the source file
pdesc = Util.getParameterDocumentation(aAST, field.getName(),
parameterNameFields.get(pname));
parameterNameFields.get(pname));
}

// Is this an external resource?
Expand All @@ -470,7 +501,7 @@ else if (ExternalResourceFactory.isExternalResourceField(field)) {
pname = ExternalResourceFactory.createResourceDependency(field).getKey();
// Extract JavaDoc for this resource from the source file
pdesc = Util.getParameterDocumentation(aAST, field.getName(),
resourceNameFields.get(pname));
resourceNameFields.get(pname));
} else {
continue;
}
Expand All @@ -486,7 +517,7 @@ else if (ExternalResourceFactory.isExternalResourceField(field)) {
try {
CtField ctField = aCtClazz.getField(field.getName());
AnnotationsAttribute annoAttr = (AnnotationsAttribute) ctField.getFieldInfo()
.getAttribute(AnnotationsAttribute.visibleTag);
.getAttribute(AnnotationsAttribute.visibleTag);

// Locate and update annotation
if (annoAttr != null) {
Expand All @@ -495,14 +526,14 @@ else if (ExternalResourceFactory.isExternalResourceField(field)) {
// Update existing annotation
for (Annotation a : annotations) {
if (a.getTypeName()
.equals(org.apache.uima.fit.descriptor.ConfigurationParameter.class.getName())
|| a.getTypeName()
.equals(org.apache.uima.fit.descriptor.ExternalResource.class.getName())
|| a.getTypeName().equals("org.uimafit.descriptor.ConfigurationParameter")
|| a.getTypeName().equals("org.uimafit.descriptor.ExternalResource")) {
.equals(org.apache.uima.fit.descriptor.ConfigurationParameter.class.getName())
|| a.getTypeName()
.equals(org.apache.uima.fit.descriptor.ExternalResource.class.getName())
|| a.getTypeName().equals("org.uimafit.descriptor.ConfigurationParameter")
|| a.getTypeName().equals("org.uimafit.descriptor.ExternalResource")) {
if (a.getMemberValue("description") == null) {
a.addMemberValue("description",
new StringMemberValue(pdesc, aCtClazz.getClassFile().getConstPool()));
new StringMemberValue(pdesc, aCtClazz.getClassFile().getConstPool()));
getLog().debug("Enhanced description of " + type + " [" + pname + "]");
// Replace updated annotation
annoAttr.addAnnotation(a);
Expand All @@ -520,7 +551,7 @@ else if (ExternalResourceFactory.isExternalResourceField(field)) {
ctField.getFieldInfo().addAttribute(annoAttr);
} catch (NotFoundException e) {
throw new MojoExecutionException("Field [" + field.getName() + "] not found in byte code: "
+ ExceptionUtils.getRootCauseMessage(e), e);
+ ExceptionUtils.getRootCauseMessage(e), e);
}
}
}
Expand Down Expand Up @@ -552,7 +583,7 @@ private Map<String, String> getParameterConstants(Class<?> aClazz, String[] aPre
result.put(parameterName, f.getName());
} catch (IllegalAccessException e) {
getLog().warn("Unable to access name constant field [" + f.getName() + "]: "
+ ExceptionUtils.getRootCauseMessage(e), e);
+ ExceptionUtils.getRootCauseMessage(e), e);
}
}
return result;
Expand All @@ -563,7 +594,7 @@ private JavaSource parseSource(String aSourceFile) throws MojoExecutionException
return Util.parseSource(aSourceFile, encoding);
} catch (IOException e) {
throw new MojoExecutionException("Unable to parse source file [" + aSourceFile + "]: "
+ ExceptionUtils.getRootCauseMessage(e), e);
+ ExceptionUtils.getRootCauseMessage(e), e);
}
}

Expand All @@ -589,7 +620,7 @@ private String getSourceFile(String aClassName) {
* Write a report on any meta data missing from components.
*/
private void writeMissingMetaDataReport(File aReportFile, Multimap<String, String> aReportData)
throws MojoExecutionException {
throws MojoExecutionException {
String[] classes = aReportData.keySet().toArray(new String[aReportData.keySet().size()]);
Arrays.sort(classes);

Expand All @@ -616,7 +647,7 @@ private void writeMissingMetaDataReport(File aReportFile, Multimap<String, Strin
}
} catch (IOException e) {
throw new MojoExecutionException("Unable to write missing meta data report to [" + aReportFile
+ "]" + ExceptionUtils.getRootCauseMessage(e), e);
+ "]" + ExceptionUtils.getRootCauseMessage(e), e);
} finally {
IOUtils.closeQuietly(out);
}
Expand All @@ -626,7 +657,7 @@ private void writeMissingMetaDataReport(File aReportFile, Multimap<String, Strin
* Read the missing meta data report from a previous run.
*/
private void readMissingMetaDataReport(File aReportFile, Multimap<String, String> aReportData)
throws MojoExecutionException {
throws MojoExecutionException {
if (!aReportFile.exists()) {
// Ignore if the file is missing
return;
Expand Down Expand Up @@ -657,7 +688,8 @@ private void readMissingMetaDataReport(File aReportFile, Multimap<String, String
}
} catch (IOException e) {
throw new MojoExecutionException(
"Unable to read missing meta data report: " + ExceptionUtils.getRootCauseMessage(e), e);
"Unable to read missing meta data report: " + ExceptionUtils.getRootCauseMessage(e),
e);
} finally {
LineIterator.closeQuietly(i);
}
Expand Down
Loading

0 comments on commit 67d663f

Please sign in to comment.