3333import java .net .URLClassLoader ;
3434import java .util .ArrayList ;
3535
36+ import org .slf4j .Logger ;
37+ import org .slf4j .LoggerFactory ;
38+
3639import javassist .ClassPool ;
3740import javassist .CtClass ;
3841import javassist .CtField ;
4346import javassist .bytecode .ClassFile ;
4447import javassist .bytecode .ConstantAttribute ;
4548import javassist .bytecode .FieldInfo ;
46- import org .gradle .api .Action ;
47- import org .gradle .api .Project ;
48- import org .gradle .api .Task ;
49- import org .gradle .api .tasks .SourceSet ;
50- import org .gradle .api .tasks .SourceSetContainer ;
51-
52- import org .slf4j .Logger ;
53- import org .slf4j .LoggerFactory ;
49+ import org .gradle .api .DefaultTask ;
50+ import org .gradle .api .file .ConfigurableFileCollection ;
51+ import org .gradle .api .provider .ListProperty ;
52+ import org .gradle .api .provider .Property ;
53+ import org .gradle .api .tasks .Input ;
54+ import org .gradle .api .tasks .InputFiles ;
55+ import org .gradle .api .tasks .TaskAction ;
5456
5557/**
56- * Responsible for coordinating the actual injection of project version. Runs as a Gradle Action (doLast typically
57- * to the main CompileJava task)
58+ * Responsible for coordinating the actual injection of project version.
5859 *
5960 * @author Steve Ebersole
6061 */
61- public class InjectionAction implements Action <Task > {
62- private static final Logger log = LoggerFactory .getLogger ( InjectionAction .class );
62+ public abstract class InjectionTask extends DefaultTask {
63+ static final String TASK_NAME = "injectVersion" ;
64+ private static final Logger log = LoggerFactory .getLogger ( InjectionTask .class );
6365
6466 private LoaderClassPath loaderClassPath ;
6567 private ClassPool classPool ;
6668
67- private final InjectionSpec injectionSpec ;
69+ @ InputFiles
70+ abstract ConfigurableFileCollection getClasspath ();
6871
69- public InjectionAction (InjectionSpec injectionSpec ) {
70- this .injectionSpec = injectionSpec ;
71- }
72+ @ Input
73+ public abstract Property <String > getVersion ();
74+
75+ @ Input
76+ public abstract ListProperty <TargetMember > getInjectionTargets ();
7277
73- @ Override
74- public void execute (Task task ) {
75- final ClassLoader runtimeScopeClassLoader = buildRuntimeScopeClassLoader ( task . getProject () );
78+ @ TaskAction
79+ public void execute () {
80+ final ClassLoader runtimeScopeClassLoader = buildRuntimeScopeClassLoader ();
7681 loaderClassPath = new LoaderClassPath ( runtimeScopeClassLoader );
7782 classPool = new ClassPool ( true );
7883 classPool .appendClassPath ( loaderClassPath );
7984
80- performInjections ( task . getProject () );
85+ performInjections ();
8186 }
8287
83- private ClassLoader buildRuntimeScopeClassLoader (Project project ) {
88+ private ClassLoader buildRuntimeScopeClassLoader () {
8489 final ArrayList <URL > classPathUrls = new ArrayList <>();
85- final SourceSet mainSourceSet = project
86- .getExtensions ()
87- .getByType ( SourceSetContainer .class )
88- .getByName ( SourceSet .MAIN_SOURCE_SET_NAME );
89- for ( File file : mainSourceSet .getRuntimeClasspath () ) {
90+ getClasspath ().forEach ( file -> {
9091 try {
9192 classPathUrls .add ( file .toURI ().toURL () );
9293 }
9394 catch (MalformedURLException e ) {
9495 throw new InjectionException ( "Could not determine artifact URL [" + file .getPath () + "]" , e );
9596 }
96- }
97+ } );
9798 return new URLClassLoader ( classPathUrls .toArray ( URL []::new ), getClass ().getClassLoader () );
9899 }
99100
100- private void performInjections (Project project ) {
101- final String projectVersion = project . getVersion ().toString ();
101+ private void performInjections () {
102+ final String projectVersion = getVersion ().get ();
102103
103- for ( TargetMember targetMember : injectionSpec . getTargetMembers ().get () ) {
104+ for ( TargetMember targetMember : getInjectionTargets ().get () ) {
104105 resolveInjectionTarget ( targetMember ).inject ( projectVersion );
105106 }
106107 }
@@ -113,7 +114,7 @@ private InjectionTarget resolveInjectionTarget(TargetMember targetMember) {
113114 CtField field = ctClass .getField ( targetMember .getMemberName () );
114115 return new FieldInjectionTarget ( targetMember , ctClass , field );
115116 }
116- catch ( NotFoundException ignore ) {
117+ catch ( NotFoundException ignore ) {
117118 }
118119
119120 // see if it is a method...
@@ -133,7 +134,7 @@ private InjectionTarget resolveInjectionTarget(TargetMember targetMember) {
133134 // finally throw an exception
134135 throw new InjectionException ( "Unknown member [" + targetMember .getQualifiedName () + "]" );
135136 }
136- catch ( Throwable e ) {
137+ catch (Throwable e ) {
137138 throw new InjectionException ( "Unable to resolve class [" + targetMember .getClassName () + "]" , e );
138139 }
139140 }
@@ -146,14 +147,13 @@ private interface InjectionTarget {
146147 * Inject the given value per this target's strategy.
147148 *
148149 * @param value The value to inject.
149- *
150150 * @throws org.hibernate.build.gradle.inject.InjectionException Indicates a problem performing the injection.
151151 */
152152 void inject (String value );
153153 }
154154
155155 private abstract class BaseInjectionTarget implements InjectionTarget {
156- @ SuppressWarnings ( { "UnusedDeclaration" })
156+ @ SuppressWarnings ({ "UnusedDeclaration" })
157157 private final TargetMember targetMember ;
158158 private final CtClass ctClass ;
159159 private final File classFileLocation ;
@@ -164,7 +164,7 @@ protected BaseInjectionTarget(TargetMember targetMember, CtClass ctClass) {
164164 try {
165165 classFileLocation = new File ( loaderClassPath .find ( targetMember .getClassName () ).toURI () );
166166 }
167- catch ( Throwable e ) {
167+ catch (Throwable e ) {
168168 throw new InjectionException ( "Unable to resolve class file path" , e );
169169 }
170170 }
@@ -205,7 +205,7 @@ private class FieldInjectionTarget extends BaseInjectionTarget {
205205 private FieldInjectionTarget (TargetMember targetMember , CtClass ctClass , CtField ctField ) {
206206 super ( targetMember , ctClass );
207207 this .ctField = ctField ;
208- if ( ! Modifier .isStatic ( ctField .getModifiers () ) ) {
208+ if ( !Modifier .isStatic ( ctField .getModifiers () ) ) {
209209 throw new InjectionException ( "Field is not static [" + targetMember .getQualifiedName () + "]" );
210210 }
211211 }
@@ -237,7 +237,7 @@ protected void doInjection(String value) {
237237 try {
238238 ctMethod .setBody ( "{return \" " + value + "\" ;}" );
239239 }
240- catch ( Throwable t ) {
240+ catch (Throwable t ) {
241241 throw new InjectionException ( "Unable to replace method body" , t );
242242 }
243243 }
0 commit comments