Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow @option to be used with configuration extensions #11456

Closed
jeremylong opened this issue Nov 24, 2019 · 6 comments
Closed

Allow @option to be used with configuration extensions #11456

jeremylong opened this issue Nov 24, 2019 · 6 comments
Labels
a:feature A new functionality stale

Comments

@jeremylong
Copy link

As recommended in the gradle documentation - most plugins use extensions to add configuration options (doc ref: Making the Plugin Configurable).

The @Option attribute is extremely useful as well. However, the @Option attribute is only available on tasks - it would be very helpful if the @option attribute could be used within an extension.

Desired Behavior

It would be great if we could add @Option using the following:

class GreetingPluginExtension {
    @Option("message", "The greeting message")
    String message = 'Hello from GreetingPlugin'
}

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        // Add the 'greeting' extension object
        def extension = project.extensions.create('greeting', GreetingPluginExtension)
        // Add a task that uses configuration from the extension object
        project.task('hello') {
            doLast {
                println extension.message
            }
        }
    }
}

apply plugin: GreetingPlugin

Output of gradle -q hello --message "Wassup from greeting plugin"

> gradle -q hello --message "Wassup from greeting plugin"
Wassup from greeting plugin
@jeremylong jeremylong added a:feature A new functionality from:contributor labels Nov 24, 2019
@lacasseio
Copy link
Contributor

It's an interesting concept. I'm not sure the @Option API where meant to be use with the model in any way. It is possible to workaround this by using System (-D<prop>=<value>) or Project (-P<prop>=<value>) properties. If I remember correctly, I think that using -Pgreeting.message="Wassup from greeting plugin" would actually work without any custom code given the extension name isn't colliding with any properties on Project object. However, there is no way to discover what is currently configurable. It would probably be a good idea to may these feature more discoverable if we go down that line. Maybe giving a description to the Property object so we can list them and the register extensions when requesting help on the project, kind of like the model task of the Software Model.

@jeremylong
Copy link
Author

The -P<prop>=value does not appear to work with extensions - am I missing something?

Output of gradle -q hello -Pgreeting.message="Wassup from greeting plugin"

> gradle -q hello -Pgreeting.message="Wassup from greeting plugin"
Hello from GreetingPlugin

@lacasseio
Copy link
Contributor

It looks like I miss read the code. It's mostly a feature that would be coming from DynamicObject. However, it's an internal API and doesn't seems to treat extensions as properties to be accessed by the project properties. You are left with wiring everything yourself.

greeting.message = hasProperty('message') ? getProperty('message') : 'Hello from GreetingPlugin'

The question we are wondering is why would you want to configure the extension via the command line? What kind of use case are you looking at solving?

@jeremylong
Copy link
Author

This comes from a ticket posted to dependency-check-gradle#161. The plugin performs software composition analysis and can be configured to fail the build if a vulnerability is identified in one of the dependencies and has a CVSS score above a configured level. Their use case would be being able to adjust the CVSS threshold (i.e. the build may be configured to have it fail on a CVSS of 7.0 - but a developer may want to locally complete a successful build without having to modify the build.gradle while they finish solving their current task without having to fix the vulnerable component immediately).

In other cases one may want to skip the analysis - but allowing the task developer to keep all the configuration in one place (the extension). If we could put an @option (or similar) on a configuration task we could more easily allow disabling a task.

@stale
Copy link

stale bot commented Dec 3, 2020

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. If you're interested in how we try to keep the backlog in a healthy state, please read our blog post on how we refine our backlog. If you feel this is something you could contribute, please have a look at our Contributor Guide. Thank you for your contribution.

@stale stale bot added the stale label Dec 3, 2020
@stale
Copy link

stale bot commented Dec 24, 2020

This issue has been automatically closed due to inactivity. If you can reproduce this on a recent version of Gradle or if you have a good use case for this feature, please feel free to reopen the issue with steps to reproduce, a quick explanation of your use case or a high-quality pull request.

@stale stale bot closed this as completed Dec 24, 2020
@wolfs wolfs closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:feature A new functionality stale
Projects
None yet
Development

No branches or pull requests

3 participants