[jboss-svn-commits] JBL Code SVN: r30402 - labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 30 23:37:05 EST 2009


Author: steve.ebersole at jboss.com
Date: 2009-11-30 23:37:05 -0500 (Mon, 30 Nov 2009)
New Revision: 30402

Removed:
   labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java
   labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/process/
Modified:
   labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjection.java
   labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java
   labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/Constant.java
   labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/MethodBodyReturn.java
   labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/TargetMember.java
Log:
The previous fix lead to issue resolving classes.  Apprently this all might be due to the classloader I build getting GC'ed because javassist holds it in a weakref, so try holding a ref to it...

Deleted: labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java	2009-12-01 04:19:21 UTC (rev 30401)
+++ labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/AbstractInjectionMojo.java	2009-12-01 04:37:05 UTC (rev 30402)
@@ -1,84 +0,0 @@
-package org.jboss.maven.plugins.injection;
-
-import java.util.List;
-import java.util.ArrayList;
-import java.net.URL;
-import java.net.MalformedURLException;
-import java.net.URLClassLoader;
-import java.io.File;
-
-import org.apache.maven.plugin.AbstractMojo;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.project.MavenProject;
-import org.apache.maven.artifact.DependencyResolutionRequiredException;
-import org.jboss.maven.plugins.injection.process.InjectionDescriptor;
-import org.jboss.maven.plugins.injection.process.InjectionTarget;
-
-/**
- * Mojo responsible for injecting values into various targets as part of a build process.  One example of this
- * is injecting the project version into a class file (bytcode manip) or a text file or etc so that these various
- * targets do not need to be synch'ed manually.
- *
- * @author Steve Ebersole
- */
-public abstract class AbstractInjectionMojo extends AbstractMojo {
-	/**
-     * The Maven Project Object
-     *
-     * @parameter expression="${project}"
-     * @required
-     */
-    protected MavenProject project;
-
-	protected void prepare() throws MojoExecutionException {
-	}
-
-	protected abstract List<InjectionDescriptor> getInjectionDescriptors() throws MojoExecutionException;
-
-	protected void finishUp() {
-	}
-
-	public void execute() throws MojoExecutionException, MojoFailureException {
-		prepare();
-
-		for ( InjectionDescriptor descriptor : getInjectionDescriptors() ) {
-			final String injectionValue = descriptor.getValue();
-			for ( InjectionTarget target : descriptor.getTargets() ) {
-				target.inject( injectionValue );
-			}
-		}
-
-		finishUp();
-	}
-
-	protected final String resolveExpression(String expression) {
-		return expression;
-	}
-
-	protected final ClassLoader buildProjectCompileClassLoader() throws MojoExecutionException {
-		ArrayList<URL> classPathUrls = new ArrayList<URL>();
-
-		// we should also add the compile classpath elements...
-		for ( String path : projectCompileClasspathElements() ) {
-			try {
-				getLog().info( "Adding project compile classpath element : " + path );
-				classPathUrls.add( new File( path ).toURI().toURL() );
-			}
-			catch ( MalformedURLException e ) {
-				throw new MojoExecutionException( "Unable to build path URL [" + path + "]" );
-			}
-		}
-		return new URLClassLoader( classPathUrls.toArray( new URL[classPathUrls.size()] ), getClass().getClassLoader() );
-	}
-
-	@SuppressWarnings({ "unchecked" })
-	protected final List<String> projectCompileClasspathElements() throws MojoExecutionException {
-		try {
-			return ( List<String> ) project.getCompileClasspathElements();
-		}
-		catch ( DependencyResolutionRequiredException e ) {
-			throw new MojoExecutionException( "Call to Project#getCompileClasspathElements required dependency resolution" );
-		}
-	}
-}

Modified: labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjection.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjection.java	2009-12-01 04:19:21 UTC (rev 30401)
+++ labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjection.java	2009-12-01 04:37:05 UTC (rev 30402)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors.  All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
  *
  * This copyrighted material is made available to anyone wishing to use, modify,
  * copy, or redistribute it subject to the terms and conditions of the GNU

Modified: labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java	2009-12-01 04:19:21 UTC (rev 30401)
+++ labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/BytecodeInjectionMojo.java	2009-12-01 04:37:05 UTC (rev 30402)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors.  All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
  *
  * This copyrighted material is made available to anyone wishing to use, modify,
  * copy, or redistribute it subject to the terms and conditions of the GNU
@@ -28,6 +28,9 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.net.URLClassLoader;
 
 import javassist.ClassPool;
 import javassist.CtClass;
@@ -37,8 +40,10 @@
 import javassist.bytecode.ConstantAttribute;
 import javassist.bytecode.FieldInfo;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.jboss.maven.plugins.injection.process.InjectionDescriptor;
-import org.jboss.maven.plugins.injection.process.InjectionTarget;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
 
 /**
  * Used to inject resolved expression values into compiled bytecode.
@@ -48,11 +53,20 @@
  * 
  * @goal bytecode
  * @phase compile
+ * @requiresDependencyResolution
  *
  * @author Steve Ebersole
  */
-public class BytecodeInjectionMojo extends AbstractInjectionMojo {
+public class BytecodeInjectionMojo extends AbstractMojo {
 	/**
+     * The Maven Project Object
+     *
+     * @parameter expression="${project}"
+     * @required
+     */
+    protected MavenProject project;
+
+	/**
 	 * The injections to be performed.
 	 *
 	 * @parameter
@@ -63,41 +77,92 @@
 	private LoaderClassPath loaderClassPath;
 	private ClassPool classPool;
 
-	@Override
-	protected void prepare() throws MojoExecutionException {
-		super.prepare();
-		loaderClassPath = new LoaderClassPath( buildProjectCompileClassLoader() );
-		classPool = new ClassPool();
+	/**
+	 * {@inheritDoc}
+	 */
+	public void execute() throws MojoExecutionException, MojoFailureException {
+		final ClassLoader mavenProjectCompileClassPathClassLoader = buildProjectCompileClassLoader();
+		loaderClassPath = new LoaderClassPath( mavenProjectCompileClassPathClassLoader );
+
+		classPool = new ClassPool( true );
 		classPool.appendClassPath( loaderClassPath );
-		classPool.appendSystemPath();
-	}
 
-	protected List<InjectionDescriptor> getInjectionDescriptors() throws MojoExecutionException {
-		ArrayList<InjectionDescriptor> descriptors = new ArrayList<InjectionDescriptor>();
 		for ( BytecodeInjection injection : bytecodeInjections ) {
-			descriptors.add( generateDescriptor( injection ) );
+			for ( TargetMember member : injection.getTargetMembers() ) {
+				final InjectionTarget injectionTarget;
+				if ( member instanceof Constant ) {
+					injectionTarget = new ConstantInjectionTarget( ( Constant ) member );
+				}
+				else if ( member instanceof MethodBodyReturn ) {
+					injectionTarget = new MethodBodyReturnReplacementTarget( ( MethodBodyReturn ) member );
+				}
+				else {
+					throw new MojoExecutionException( "Unexpected injection member type : " + member );
+				}
+				injectionTarget.inject( injection.getExpression() );
+			}
 		}
-		return descriptors;
+
+		loaderClassPath.close();
 	}
 
-	private InjectionDescriptor generateDescriptor(BytecodeInjection injection) throws MojoExecutionException {
-		InjectionDescriptor descriptor = new InjectionDescriptor( resolveExpression( injection.getExpression() ) );
-		for ( TargetMember member : injection.getTargetMembers() ) {
-			descriptor.getTargets().add( generateBytecodeInjectionTarget( member ) );
+	/**
+	 * Builds a {@link ClassLoader} based on the maven project's compile classpath elements.
+	 *
+	 * @return The {@link ClassLoader} made up of the maven project's compile classpath elements.
+	 *
+	 * @throws MojoExecutionException Indicates an issue processing one of the classpath elements
+	 */
+	private ClassLoader buildProjectCompileClassLoader() throws MojoExecutionException {
+		ArrayList<URL> classPathUrls = new ArrayList<URL>();
+		for ( String path : projectCompileClasspathElements() ) {
+			try {
+				getLog().debug( "Adding project compile classpath element : " + path );
+				classPathUrls.add( new File( path ).toURI().toURL() );
+			}
+			catch ( MalformedURLException e ) {
+				throw new MojoExecutionException( "Unable to build path URL [" + path + "]" );
+			}
 		}
-		return descriptor;
+		return new URLClassLoader( classPathUrls.toArray( new URL[classPathUrls.size()] ), getClass().getClassLoader() );
 	}
 
-	private InjectionTarget generateBytecodeInjectionTarget(TargetMember member) throws MojoExecutionException {
-		if ( member instanceof Constant ) {
-			return new ConstantInjectionTarget( ( Constant ) member );
+	/**
+	 * Essentially a call to {@link MavenProject#getCompileClasspathElements} except that here we
+	 * cast it to the generic type and internally handle {@link DependencyResolutionRequiredException}.
+	 *
+	 * @return The compile classpath elements
+	 *
+	 * @throws MojoExecutionException Indicates a {@link DependencyResolutionRequiredException} was encountered
+	 */
+	@SuppressWarnings({ "unchecked" })
+	private List<String> projectCompileClasspathElements() throws MojoExecutionException {
+		try {
+			return ( List<String> ) project.getCompileClasspathElements();
 		}
-		else if ( member instanceof MethodBodyReturn ) {
-			return new MethodBodyReturnReplacementTarget( ( MethodBodyReturn ) member );
+		catch ( DependencyResolutionRequiredException e ) {
+			throw new MojoExecutionException(
+					"Call to MavenProject#getCompileClasspathElements required dependency resolution"
+			);
 		}
-		throw new MojoExecutionException( "Unexpected injection member type : " + member );
 	}
 
+	/**
+	 * Defines a strategy for applying injections.
+	 *
+	 * @author Steve Ebersole
+	 */
+	public static interface InjectionTarget {
+		/**
+		 * Inject the given value per this target's strategy.
+		 *
+		 * @param value The value to inject.
+		 *
+		 * @throws MojoExecutionException Indicates a problem performing the injection.
+		 */
+		public void inject(String value) throws MojoExecutionException;
+	}
+
 	private abstract class BaseInjectionTarget implements InjectionTarget {
 		private final TargetMember targetMember;
 

Modified: labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/Constant.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/Constant.java	2009-12-01 04:19:21 UTC (rev 30401)
+++ labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/Constant.java	2009-12-01 04:37:05 UTC (rev 30402)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors.  All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
  *
  * This copyrighted material is made available to anyone wishing to use, modify,
  * copy, or redistribute it subject to the terms and conditions of the GNU

Modified: labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/MethodBodyReturn.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/MethodBodyReturn.java	2009-12-01 04:19:21 UTC (rev 30401)
+++ labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/MethodBodyReturn.java	2009-12-01 04:37:05 UTC (rev 30402)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors.  All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
  *
  * This copyrighted material is made available to anyone wishing to use, modify,
  * copy, or redistribute it subject to the terms and conditions of the GNU

Modified: labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/TargetMember.java
===================================================================
--- labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/TargetMember.java	2009-12-01 04:19:21 UTC (rev 30401)
+++ labs/jbossbuild/maven-plugins/trunk/maven-injection-plugin/src/main/java/org/jboss/maven/plugins/injection/TargetMember.java	2009-12-01 04:37:05 UTC (rev 30402)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors.  All third-party contributions are
- * distributed under license by Red Hat Middleware LLC.
+ * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by
+ * third-party contributors as indicated by either @author tags or express
+ * copyright attribution statements applied by the authors.  All
+ * third-party contributions are distributed under license by Red Hat Inc.
  *
  * This copyrighted material is made available to anyone wishing to use, modify,
  * copy, or redistribute it subject to the terms and conditions of the GNU



More information about the jboss-svn-commits mailing list