# How to edit write-only property as readable and writable

**URL:** <https://discuss.gradle.org/t/how-to-edit-write-only-property-as-readable-and-writable/11655>\
**Category:** Plugin Portal\
**Created:** [September 15, 2015, 6:37am UTC](https://discuss.gradle.org/t/how-to-edit-write-only-property-as-readable-and-writable/11655 "2015-09-15T06:37:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gucludd](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@gucludd](https://discuss.gradle.org/u/gucludd)\
**Post date:** [September 15, 2015, 6:37am UTC](https://discuss.gradle.org/t/how-to-edit-write-only-property-as-readable-and-writable/11655/1 "2015-09-15T06:37:28Z")

</div>

I have a custom plugin implementation. I need to pass task properties prop1 and prop2 into task class exampleClass (extending DefaultClass) with gradle command.

gradle -Pprop1={val} -Pprop2={val2} exampleTask

Class exampleClass, defined as Java class, is declared in build.gradle as

task(type: org.ttg.examleClass){  
//some actionns  
}

**My question: How can I pass those -P parameters into default class?** I tried hasProperty({prop1}) and it returned true. However, property({prop1}) resulted prop1 as write-only exception.

I will appreciate any kind of help.

---

<div class="post-metadata">

**Author:** ![Shinya\_Mochida](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/shinya_mochida/32/2393_2.png) [@Shinya\_Mochida](https://discuss.gradle.org/u/Shinya_Mochida)\
**Post date:** [September 15, 2015, 10:01am UTC](https://discuss.gradle.org/t/how-to-edit-write-only-property-as-readable-and-writable/11655/2 "2015-09-15T10:01:57Z")

</div>

Where do you use a method `property(String)`?

If you use a method `property(String)` in `task exampleTask{}` block, it is the method of `Task#property(String)`. The method `Task#property(String)` tries to find property from task object, and it’s not property given by command line(`-Pprop1=xxx`).

If you use a method `property(String)` outside of `task exampleTask{}` block, it is the method of `Project#property(String)`. It will return property given by command line(`-Pprop1=xxxx`).

The detail information is available at Gradle’s dsl document.

- [Project’s property method](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:property(java.lang.String))
- [Task’s property method](https://docs.gradle.org/current/dsl/org.gradle.api.Task.html#org.gradle.api.Task:property(java.lang.String))

---

<div class="post-metadata">

**Author:** ![gucludd](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@gucludd](https://discuss.gradle.org/u/gucludd)\
**Post date:** [September 15, 2015, 2:03pm UTC](https://discuss.gradle.org/t/how-to-edit-write-only-property-as-readable-and-writable/11655/3 "2015-09-15T14:03:50Z")

</div>

Thanks for your response @Shinya_Mochida. I use it inside of exampleTask class.

Could you please expand what you mean by properly given? I use -P{property1}={property1Val}

hasProperty({property1}) returned true. What triggers to make it as write only property for task class?

---

<div class="post-metadata">

**Author:** ![Shinya\_Mochida](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/shinya_mochida/32/2393_2.png) [@Shinya\_Mochida](https://discuss.gradle.org/u/Shinya_Mochida)\
**Post date:** [September 15, 2015, 3:24pm UTC](https://discuss.gradle.org/t/how-to-edit-write-only-property-as-readable-and-writable/11655/4 "2015-09-15T15:24:34Z")

</div>

Because you didn’t write your script, class definition and stacktraces, I guessed. And I guessed you might have created class as follows…

```java
public class ExampleClass extend DefaultTask {
    private String prop1;
    @TaskAction public void doWork() {
        System.out.println ("prop1 -> [" + prop1 + "]");
    }
    // setter/getter
}

```

And I guess you might have created script as follows…

```groovy
task exampleTask(type: ExampleTask) {
  def existsProp = hasProperty('prop1') // -> Task#hasProperty and it will return true because class ExampleTask has field prop1
  prop1 = property('prop1') // -> this will set null to ExampleTask#prop1, because this property method is Task#property method and returns null because ExampleTask#prop1 has no default value.
}

```

In the class and scripts above, while you run Gradle with a parameter `-Pprop1=xxxx`, the task cannot retrieve the parameter value.

In my understanding, you want to give a parameter to task from command, don’t you?

If so, you have to change script as follows.

```groovy
task exampleTask(type: ExampleTask) {
    prop1 = project.hasProperty('prop1') ? project.property('prop1') : 'default value'
}

```

If I have mistaken your question and your intent, please let me know, and show me your class definition and build script.

---

<div class="post-metadata">

**Author:** ![gucludd](https://avatars.discourse-cdn.com/v4/letter/g/c6cbf5/32.png) [@gucludd](https://discuss.gradle.org/u/gucludd)\
**Post date:** [September 15, 2015, 4:26pm UTC](https://discuss.gradle.org/t/how-to-edit-write-only-property-as-readable-and-writable/11655/5 "2015-09-15T16:26:26Z")

</div>

Thanks again @Shinya_Mochida. It worked! I didn’t originally define task in a way you suggested.
