Custom plugin - reading properties for uploadArchives


(Pranav K Varshney) #1

I have defined the following properties in my build.gradle

apply plugin: MyPlugin
  deploy {
 type = "ssh"
 ip = "10.1.1.1"
 login = "me"
 password = "password"
 path = "/var/www/html"
}

My extension

public class DeployExtension {
 String type
 String ip
 String login
 String password
 String path
}

MyPlugin

class MyPlugin implements Plugin<Project> {
    public void apply(Project project) {
  def extension = new DeployExtension()
        project.extensions.deploy = extension
                project.configurations {
  webserver
 }
           project.uploadWebserver {
     repositories.mavenDeployer {
  name = 'sshDeployer' // optional
  repository(url: project.customProp.serverIP) {
    authentication(userName: "me", password: "myPassword")
         }
           }
 }
   }
}

How do I read the properties ip, login, password etc under uploadWebserver? extension.propertyName gives me null.


(Peter Niederwieser) #2

The plugin runs when the build script hits ‘apply plugin: …’. At this point, the code configuring the ‘deploy { … }’ section hasn’t been executed yet. The solution is to defer the execution of any plugin code that needs to access the extension object. There are several ways to do so, each with their own tradeoffs. One way is to wrap the code whose execution is to be deferred with ‘project.gradle.projectsEvaluated { … }’.