Search in sources :

Example 6 with MojoExecutionException

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 + "]");
    }
}
Also used : UnloadedClass(org.hibernate.bytecode.enhance.spi.UnloadedClass) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) Enhancer(org.hibernate.bytecode.enhance.spi.Enhancer) URLClassLoader(java.net.URLClassLoader) File(java.io.File) UnloadedField(org.hibernate.bytecode.enhance.spi.UnloadedField) DefaultEnhancementContext(org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext) EnhancementContext(org.hibernate.bytecode.enhance.spi.EnhancementContext)

Example 7 with MojoExecutionException

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) {
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 8 with MojoExecutionException

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;
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 9 with MojoExecutionException

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);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 10 with MojoExecutionException

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);
                }
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) OutputBufferThread(org.apache.hadoop.maven.plugin.util.Exec.OutputBufferThread) Map(java.util.Map)

Aggregations

MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1221 IOException (java.io.IOException)656 File (java.io.File)572 MojoFailureException (org.apache.maven.plugin.MojoFailureException)275 Artifact (org.apache.maven.artifact.Artifact)162 ArrayList (java.util.ArrayList)151 FileInputStream (java.io.FileInputStream)77 MavenProject (org.apache.maven.project.MavenProject)76 HashMap (java.util.HashMap)68 Properties (java.util.Properties)63 FileOutputStream (java.io.FileOutputStream)61 Map (java.util.Map)60 URL (java.net.URL)59 MalformedURLException (java.net.MalformedURLException)57 ArchiverException (org.codehaus.plexus.archiver.ArchiverException)52 FileWriter (java.io.FileWriter)50 List (java.util.List)49 URLClassLoader (java.net.URLClassLoader)45 LinkedHashSet (java.util.LinkedHashSet)40 InputStream (java.io.InputStream)38