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 arbitrary code snippet to be added to GradleBuild #1079

Closed
jjathman opened this issue Apr 8, 2020 · 5 comments
Closed

Allow arbitrary code snippet to be added to GradleBuild #1079

jjathman opened this issue Apr 8, 2020 · 5 comments
Assignees
Milestone

Comments

@jjathman
Copy link

jjathman commented Apr 8, 2020

In my custom Spring initializr instance I would like to include this block in every build.gradle file that is produced. As far as I could tell there was no way to produce a from block that took a string parameter as well as a closure parameter. It's possible I was missing this though.

distributions {
    main {
        contents {
            from('src/main/resources/application.yml') {
                into "conf"
                rename "(.*)", '$1.j2'
            }
        }
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 8, 2020
@snicoll snicoll changed the title Unable to customize distributions fully GradleBuild does not allow to configure a block with a parameter and a closure Apr 8, 2020
@wilkinsona
Copy link
Contributor

Thanks for your patience.

I'm afraid I don't think I understand the request. The original title of the issue was "Unable to customize distributions fully" but Initializr has no support at all at the moment for configuring a Gradle build's distributions. If you are extending the Groovy and Kotlin DSL writers' capabilities to support the configuration of distributions, can you not implement support for the from block that meets your needs?

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Nov 17, 2020
@jjathman
Copy link
Author

Sorry about the confusion, I was trying to show what I'm attempting to do with my code block in my original post. I want to be able to customize the generated build.gradle file by customizing the distributions block. The problem is due to GradleTask.Builder having an invoke method but that method does not allow for passing a closure to the method to invoke. I'm trying to generate the code from my original post, but it is very difficult to do. The code below was the solution I came up with, but it is definitely a hack.


    private void customizeDistributionContents(GradleBuild build) {
        build.tasks().customize("distributions") { task ->
            task.nested("main") { mainBuilder ->
                mainBuilder.nested("contents") { contentsBuilder ->
                    contentsBuilder.invoke("from(", """ 'src/main/resources/application.yml') {
                        into 'conf'
                        rename '(.*)', '\$1.j2'
                    }
                    """)
                }
            }
        }
    }

An alternative that would be fine with me would to be able to customize the final content of the file and just being able to add this additional block of content. I realize my use case is probably rather unique. The existing GradleBuild.Builder API is really close, but just wasn't quite flexible enough for my needs.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Nov 17, 2020
@wilkinsona
Copy link
Contributor

Thanks for the clarification. Your code above is treating distributions as a task when it's actually an extension. It's purely accidental that you can get as close as you have done.

I don't think we want to provide a complete API for writing arbitrary Groovy or Kotlin DSL code so an escape hatch that allows customisation of the end result may be the best option here.

@jjathman
Copy link
Author

Thanks for the help. I think you are right that just some kind of callback that allows for final customization before writing the file would work just great for me.

@snicoll snicoll changed the title GradleBuild does not allow to configure a block with a parameter and a closure Allow arbitrary code snippet to be added to GradleBuild Nov 10, 2022
@snicoll snicoll added type: enhancement and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Nov 10, 2022
@snicoll snicoll added this to the 0.20.0 milestone Nov 10, 2022
@snicoll
Copy link
Contributor

snicoll commented Nov 10, 2022

I've repurposed this issue to allow arbitrary code snippet to be added at the end of a gradle build.

snicoll added a commit to snicoll/initializr that referenced this issue Nov 10, 2022
@snicoll snicoll self-assigned this Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants