Access src/main/java from within plugin task


(ThomasE) #1

project1

  • provides project1.jar
  • provides common functionality for multiple projects
  • has main()

project2

  • compile depends on project1.jar
  • provides gradle plugin “SomeThing.plugin” via project2.jar
  • “SomeThing.plugin” has task “doSomeThing”
  • does not have main()

project3

  • compile depends on project1.jar

  • does not have main()

    apply plugin: 'foo.bar.SomeThing.plugin’
    buildscript {
    repositories {
    maven {
    url ‘<url_to_our_repository>’
    }
    jcenter()
    }
    dependencies {
    classpath group: ‘foo.bar’, name: ‘project2’, version: ‘0.0.1’
    }
    }


All projects are built with apply plugin: 'java'

Thanks to @Chris_Dore & @sterling (Cannot apply custom plugin) this setup works and I can see the call of the plugin functionality (as provided by project2) in the “doSomeThing” task while building project3.

Now I need to inspect the classes defined in src/main/java of project3 in the task “doSomeThing”. For that, the code in project2 (which is called by task “doSomething”) uses a class called ClassFinder which is defined in src/main/java of project1. That call works and I can see output of ClassFinder printed to STDOUT as debug during the execution of task “doSomething” in the build process of project3.

However, ClassFinder cannot find the classes defined in src/main/java of project3. I assume that is due to how ClassFinder tries to locate them. When ClassFinder calls System.getProperty("java.class.path") all it gets is C:\Users\username\.gradle\wrapper\dists\gradle-3.4-bin\aeufj4znodijbvwfbsq3044r0\gradle-3.4\lib\gradle-launcher-3.4.jar but no other paths or JARs. Note, ClassFinder does know how to find classes in JARs as well as in directories - it just doesn’t receive any other paths to process.

  1. Is there a way to provide the clases from src/main/java to ClassFinder ? Something like declaring the directory as “classpath” for task “doSomeThing” or provide project3.jar itself after it has been compiled ?
  2. Is there an alternative/addition to System.getProperty("java.class.path") which also finds src/main/java of the project currently being built ?

Thanks & Regards

Edit: I found https://stackoverflow.com/questions/31384584/how-to-use-application-classpath-in-gradle-task but have not been able to figure out how to appyl it in my setup (the task “doSomething” is defined in java, not groovy)