6

I have a Gradle plugin with a task:

@TaskAction
def nyTask() {

  def myFlags = project.pluginConfig.myFlags
  if myFlags == null {
    myFlags = "-arg1 absolutePathToFile/file1.txt -arg2 absolutePathToFile/file2.txt"  
    // *** this is the part I don't know ***
  }

  project.exec { 
     executable aCommand
     args myFlags.split()
  }
} 

myFlags is defined in the plugin's extension. When using the plugin I can configure it like:

myPlugin {
  myFlags = "-arg1 absolutePathToFile/file1.txt -arg2 absolutePathToFile/file2.txt"
}

I have some default files in the plugins src/main/resources/folder, i.e.,

src/main/resources/file1.txt
src/main/resources/file2.txt

If myFlags == null then I want to use the files from the resource folder.

The problem is that I do not know how to get the correct path to these files.

How do I get the absolute path to the file1.txt and file2.txt in the plugin's resource folder?

Michael
  • 32,527
  • 49
  • 210
  • 370
  • It seems that this is what you're looking for: http://stackoverflow.com/questions/941754/how-to-get-a-path-to-a-resource-in-a-java-jar-file – Opal Jun 22 '15 at 06:40

1 Answers1

4

Absolute path can be accessed via main sourceSet defined in java plugin.

The only problem is that in general there may more than one source dir for resources, so in the next example I demonstrate how to get the absolute path of the first resources directory and in your case you need to decide how to handle more then one resources dir:

  if myFlags == null {
      def resourcesDir = sourceSets.main.resources.srcDirs.first().absolutePath
      myFlags = "-arg1 ${resourcesDir}/file1.txt -arg2 ${resourcesDir}/file2.txt"
  }