All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avaje.ebean.enhance.maven.MavenEnhanceTask Maven / Gradle / Ivy

There is a newer version: 8.1.1
Show newest version
package com.avaje.ebean.enhance.maven;

import com.avaje.ebean.enhance.agent.AgentManifestReader;
import com.avaje.ebean.enhance.agent.Transformer;
import com.avaje.ebean.enhance.ant.OfflineFileTransform;
import com.avaje.ebean.enhance.ant.TransformationListener;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

/**
 * A Maven Plugin that can enhance entity beans etc for use by Ebean.
 * 

* You can use this plugin as part of your build process to enhance entity beans * etc. *

*

* The parameters are: *

    *
  • classSource This is the root directory where the .class files are * found. If this is left out then this defaults to * ${project.build.outputDirectory}.
  • *
  • classDestination This is the root directory where the .class files * are written to. If this is left out then this defaults to the * classSource.
  • *
  • packages A comma delimited list of packages that is searched for * classes that need to be enhanced. If the package ends with ** or * then all * subpackages are also searched.
  • *
  • transformArgs Arguments passed to the transformer. Typically a * debug level in the form of debug=1 etc.
  • *
*

* *
{@code
 *
 *       
 *         org.avaje.ebeanorm
 *         avaje-ebeanorm-mavenenhancer
 *         4.6.1
 *         
 *           
 *             main
 *             process-classes
 *             
 *               
 *               
 *               
 *             
 *           
 *             enhance
 *           
 *         
 *       
 *     
 *
 * }
* *

To invoke explicitly:

{@code * * mvn avaje-ebeanormenhancer:enhance * * }
* * @author Paul Mendelson, Vaughn Butt, Rob Bygrave * @since 2.5, Mar, 2009 */ @Mojo(name = "enhance", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) public class MavenEnhanceTask extends AbstractMojo { /** * The class path used to read related classes. */ @Parameter(property = "project.compileClasspathElements", required = true, readonly = true) List compileClasspathElements; /** * The directory holding the class files we want to transform. */ @Parameter(property = "project.build.outputDirectory") String classSource; /** * the classpath used to search for e.g. inherited classes */ @Parameter(name = "classpath") private String classpath; /** * Set the arguments passed to the transformer. */ @Parameter(name = "transformArgs") String transformArgs; /** * Set the package name to search for classes to transform. *

* If the package name ends in "/**" then this recursively transforms all sub * packages as well. *

*

* This is optional. When not set the agent may visit more classes to see if they need * enhancement but will still enhance effectively and pretty quickly (ignoring standard * jdk classes and lots of common libraries and language sdk's). *

*/ @Parameter(name = "packages") String packages; /** * Set to true to fail the maven build if exceptions occurred during enhancement. */ @Parameter(name = "failOnExceptions") boolean failOnExceptions; public void execute() throws MojoExecutionException { final Log log = getLog(); if (classSource == null) { classSource = "target/classes"; } File f = new File(""); log.info("Current Directory: " + f.getAbsolutePath()); StringBuilder extraClassPath = new StringBuilder(); extraClassPath.append(classSource); if (classpath != null && !classpath.isEmpty()) { if (!extraClassPath.toString().endsWith(";")) { extraClassPath.append(";"); } extraClassPath.append(classpath); } // find the packages that we should process for enhancement AgentManifestReader manifestReader = new AgentManifestReader(); // maybe configured via manifest (preferred) manifestReader.read(new File(classSource+"/META-INF/ebean.mf")); // maybe explicitly configured manifestReader.addRaw(packages); // these are the local packages to enhance (local to this artifact) Set localPackages = manifestReader.getPackages(); ClassLoader classLoader = buildClassLoader(); Transformer transformer = new Transformer(extraClassPath.toString(), transformArgs); log.info("classSource=" + classSource + " transformArgs=" + nullToEmpty(transformArgs) + " packages=" + localPackages); OfflineFileTransform ft = new OfflineFileTransform(transformer, classLoader, classSource); ft.setListener(new TransformationListener() { public void logEvent(String msg) { log.info(msg); } public void logError(String msg) { log.error(msg); } }); ft.process(localPackages); Map> unexpectedExceptions = transformer.getUnexpectedExceptions(); if (failOnExceptions && !unexpectedExceptions.isEmpty()) { throw new MojoExecutionException("Exceptions occurred during EBean enhancements, see the log above for the exact problems."); } } /** * Return a null string as empty (for pretty output on valid null parameters). */ private String nullToEmpty(String val) { return (val == null) ? "" : val; } private ClassLoader buildClassLoader() { URL[] urls = buildClassPath(); return URLClassLoader.newInstance(urls, Thread.currentThread().getContextClassLoader()); } /** * Return the class path using project compileClasspathElements. */ private URL[] buildClassPath() { try { List urls = new ArrayList<>(compileClasspathElements.size()); Log log = getLog(); for (String element : compileClasspathElements) { if (log.isDebugEnabled()) { log.debug("ClasspathElement: " + element); } urls.add(new File(element).toURI().toURL()); } return urls.toArray(new URL[urls.size()]); } catch (MalformedURLException e) { throw new RuntimeException(e); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy