use of org.apache.maven.plugin.MojoExecutionException in project hibernate-orm by hibernate.
the class MavenEnhancePlugin method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
if (!shouldApply()) {
getLog().warn("Skipping Hibernate bytecode enhancement plugin execution since no feature is enabled");
return;
}
if (!dir.startsWith(base)) {
throw new MojoExecutionException("The enhancement directory 'dir' (" + dir + ") is no subdirectory of 'base' (" + base + ")");
}
// Perform a depth first search for sourceSet
File root = new File(this.dir);
if (!root.exists()) {
getLog().info("Skipping Hibernate enhancement plugin execution since there is no classes dir " + dir);
return;
}
walkDir(root);
if (sourceSet.isEmpty()) {
getLog().info("Skipping Hibernate enhancement plugin execution since there are no classes to enhance on " + dir);
return;
}
getLog().info("Starting Hibernate enhancement for classes on " + dir);
final ClassLoader classLoader = toClassLoader(Collections.singletonList(new File(base)));
EnhancementContext enhancementContext = new DefaultEnhancementContext() {
@Override
public ClassLoader getLoadingClassLoader() {
return classLoader;
}
@Override
public boolean doBiDirectionalAssociationManagement(UnloadedField field) {
return enableAssociationManagement;
}
@Override
public boolean doDirtyCheckingInline(UnloadedClass classDescriptor) {
return enableDirtyTracking;
}
@Override
public boolean hasLazyLoadableAttributes(UnloadedClass classDescriptor) {
return enableLazyInitialization;
}
@Override
public boolean isLazyLoadable(UnloadedField field) {
return enableLazyInitialization;
}
@Override
public boolean doExtendedEnhancement(UnloadedClass classDescriptor) {
return enableExtendedEnhancement;
}
};
if (enableExtendedEnhancement) {
getLog().warn("Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk.");
}
final Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(enhancementContext);
for (File file : sourceSet) {
final byte[] enhancedBytecode = doEnhancement(file, enhancer);
if (enhancedBytecode == null) {
continue;
}
writeOutEnhancedClass(enhancedBytecode, file);
getLog().info("Successfully enhanced class [" + file + "]");
}
}
use of org.apache.maven.plugin.MojoExecutionException in project hibernate-orm by hibernate.
the class MavenEnhancePlugin method writeOutEnhancedClass.
private void writeOutEnhancedClass(byte[] enhancedBytecode, File file) throws MojoExecutionException {
try {
if (file.delete()) {
if (!file.createNewFile()) {
buildContext.addMessage(file, 0, 0, "Unable to recreate class file", BuildContext.SEVERITY_ERROR, null);
}
} else {
buildContext.addMessage(file, 0, 0, "Unable to delete class file", BuildContext.SEVERITY_ERROR, null);
}
} catch (IOException e) {
buildContext.addMessage(file, 0, 0, "Problem preparing class file for writing out enhancements", BuildContext.SEVERITY_WARNING, e);
}
OutputStream outputStream = null;
try {
outputStream = buildContext.newFileOutputStream(file);
outputStream.write(enhancedBytecode);
outputStream.flush();
} catch (IOException e) {
String msg = String.format("Error writing to enhanced class [%s] to file [%s]", file.getName(), file.getAbsolutePath());
if (failOnError) {
throw new MojoExecutionException(msg, e);
}
buildContext.addMessage(file, 0, 0, msg, BuildContext.SEVERITY_WARNING, e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException ignore) {
}
}
}
use of org.apache.maven.plugin.MojoExecutionException in project hibernate-orm by hibernate.
the class MavenEnhancePlugin method doEnhancement.
private byte[] doEnhancement(File javaClassFile, Enhancer enhancer) throws MojoExecutionException {
try {
String className = javaClassFile.getAbsolutePath().substring(base.length() + 1, javaClassFile.getAbsolutePath().length() - ".class".length()).replace(File.separatorChar, '.');
ByteArrayOutputStream originalBytes = new ByteArrayOutputStream();
FileInputStream fileInputStream = new FileInputStream(javaClassFile);
try {
byte[] buffer = new byte[1024];
int length;
while ((length = fileInputStream.read(buffer)) != -1) {
originalBytes.write(buffer, 0, length);
}
} finally {
fileInputStream.close();
}
return enhancer.enhance(className, originalBytes.toByteArray());
} catch (Exception e) {
String msg = "Unable to enhance class: " + javaClassFile.getName();
if (failOnError) {
throw new MojoExecutionException(msg, e);
}
buildContext.addMessage(javaClassFile, 0, 0, msg, BuildContext.SEVERITY_WARNING, e);
return null;
}
}
use of org.apache.maven.plugin.MojoExecutionException in project hadoop by apache.
the class VersionInfoMojo method execute.
@Override
public void execute() throws MojoExecutionException {
try {
SCM scm = determineSCM();
project.getProperties().setProperty(buildTimeProperty, getBuildTime());
project.getProperties().setProperty(scmUriProperty, getSCMUri(scm));
project.getProperties().setProperty(scmBranchProperty, getSCMBranch(scm));
project.getProperties().setProperty(scmCommitProperty, getSCMCommit(scm));
project.getProperties().setProperty(md5Property, computeMD5());
} catch (Throwable ex) {
throw new MojoExecutionException(ex.toString(), ex);
}
}
use of org.apache.maven.plugin.MojoExecutionException in project hadoop by apache.
the class CompileMojo method runCMake.
public void runCMake() throws MojoExecutionException {
validatePlatform();
validateSourceParams(source, output);
if (output.mkdirs()) {
getLog().info("mkdirs '" + output + "'");
}
List<String> cmd = new LinkedList<String>();
cmd.add("cmake");
cmd.add(source.getAbsolutePath());
for (Map.Entry<String, String> entry : vars.entrySet()) {
if ((entry.getValue() != null) && (!entry.getValue().equals(""))) {
cmd.add("-D" + entry.getKey() + "=" + entry.getValue());
}
}
cmd.add("-G");
cmd.add("Unix Makefiles");
String prefix = "";
StringBuilder bld = new StringBuilder();
for (String c : cmd) {
bld.append(prefix).append(c);
prefix = " ";
}
getLog().info("Running " + bld.toString());
getLog().info("with extra environment variables " + Exec.envToString(env));
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(output);
pb.redirectErrorStream(true);
Exec.addEnvironment(pb, env);
Process proc = null;
OutputBufferThread outThread = null;
int retCode = -1;
try {
proc = pb.start();
outThread = new OutputBufferThread(proc.getInputStream());
outThread.start();
retCode = proc.waitFor();
if (retCode != 0) {
throw new MojoExecutionException("CMake failed with error code " + retCode);
}
} catch (IOException e) {
throw new MojoExecutionException("Error executing CMake", e);
} catch (InterruptedException e) {
throw new MojoExecutionException("Interrupted while waiting for " + "CMake process", e);
} finally {
if (proc != null) {
proc.destroy();
}
if (outThread != null) {
try {
outThread.interrupt();
outThread.join();
} catch (InterruptedException e) {
getLog().error("Interrupted while joining output thread", e);
}
if (retCode != 0) {
for (String line : outThread.getOutput()) {
getLog().warn(line);
}
}
}
}
}
Aggregations