This message was deleted.
# community-support
s
This message was deleted.
Specifically the section on default tasks.
j
Thank you!
I have checked that adding my default tasks implies losing the previous ones (
help
) even after trying to add the previous
defaultTasks
+
myNewTask
. Is it possible to keep them?
c
keep the `help`task? why, when you want the default to presumably be some meaningful action? Folks can still execute
./gradlew help
j
I don't want to keep it, the problem is I don't want to remove other tasks that the user can add because I want to add a default task with my plugin
so if I am losing help task, that means I am losing all previous default tasks
c
hmmm. it’s a bit intrusive to set a default task in a plugin, as the plugin is unaware of the overall context of the build etc. If you have tasks to execute they can be added as dependencies on lifecycle tasks, e.g.
Copy code
tasks.named("build") {
  dependsOn(myTask)
}
j
yeah, I know that but basically my plugin is setting the project version and it is generating a file with the version, so I even want to call the task that will generate that version file without running build
but I will have to just run the task instead, thank you anyway 🙂
c
you can always do
Copy code
tasks.configureEach {
  dependsOn(myTask)
}
…to run your task before any others. Presumably the version is only required if other tasks are run to consume it. This is bit fragile - if tasks are consuming the version, they should have a inferred dependency on the task that generates the version.
j
Yeah... I have to refactor my plugin because it was working in configuration phase by setting the version there, but with configuration cache everything is broken because the version depends on the git tags, and after adding new commits the version should change, but the config is reused so, the version doesn't change. Not sure if a task should set the project version before the rest of the tasks do that. But looks weird to me to set the project version from a task instead of in configuration phase
c
perhaps it could be a shared build service (to determine and provide the version), with
service.map { it.resolveVersion() }
(a provider) provided to tasks to pull in the version.
j
But plugins like maven publish just pick the version and call to
toString
so I am not sure how to fix this issue with a provider
c
i see. perhaps check how axion is performing what appears to be the equivalent operation.
j
that plugin is compatible with config cache?
c
Not atm. Working with them on that.
e
Copy code
version = object {
    override fun toString() = computeVersion()
}
might work
c
yep!
j
that was what I was thinking about, creating my own object which wraps a
Property<String>
and
toString
calls to
prop.get()
. Is should be the same that your statement @ephemient?
e
you might want to ensure
.get()
is called only once instead of re-fetched (if it's the result of a provider, it'll get evaluated every time), and ensure that
.set
isn't called after (
.finalizeValue
will do both), but yes
j
I have tried with the object and I got
unspecified
when publishing
c
that’s generally due to the toString() returning
unspecified
. A simple test would be
Copy code
version = object {
    override fun toString() = "MyVersion"
}
…to confirm that structure, then add the dynamic resolution of version.
j
yeah, my tests are passing, maybe I failed in the sandbox project and I am not applying the project correctly or in the correct module, one second, today has been a looooong day 🥲
c
no worries.
j
yeah, it is working, my bad!
thank you all 🙂
c
awesome!
j
I want to revive this again. Currently, to indicate project version needs to be changed, for example indicating it is a final version, so the stage Gradle property is changed, but as this this property is changing, configuration cache is discarded. I am not sure if there is a solution to this problem, because if a task apply a configuration to a project, the task would be accessing to the project property and the cache should break. At the same time, indicating that the version of a project has to change the stage/scope is a config change, so I see it should be normal that de config cache should be discarded, but I would like to know if you think that there is a chance to avoid discarding the cache or I am in a chicken egg problem with no solution.
@ephemient as I mentioned in this thread, looks like I am not being able to get the version reevaluated even I want that behavior in the task. Looks like I am having the issue you mentioned:
you might want to ensure
.get()
is called only once instead of re-fetched (if it's the result of a provider, it'll get evaluated every time), and ensure that
.set
isn't called after (
.finalizeValue
will do both), but yes
At same time I am not only passing the calculated version, a tag prefix too, which is available in the my plugin extension.
Copy code
public class LazyVersion
private constructor(
    projectTagPrefix: Provider<String>,
    calculatedVersion: Provider<String>,
) {

    public val tagPrefix: Provider<String> = projectTagPrefix

    public val version: Provider<String> = calculatedVersion

    public val semver: Version?
        get() = Version.safe(version.get()).getOrNull()

    public val semverWithTagPrefix: String?
        get() = semver?.let { semanticVersion -> "${tagPrefix.get()}$semanticVersion" }

    override fun toString(): String = "${tagPrefix.get()}${version.get()}"

    public companion object {
        internal operator fun invoke(project: Project): LazyVersion =
            with(project) {
                LazyVersion(
                    projectTagPrefix = semverExtension.tagPrefix,
                    calculatedVersion =
                        provider {
                            val projectTagPrefix = semverExtension.tagPrefix.get()
                            val isSamePrefix = tagPrefixProperty == projectTagPrefix
                            val version =
                                git.calculatedVersion(
                                    tagPrefix = projectTagPrefix,
                                    stageProperty = stageProperty.takeIf { isSamePrefix },
                                    scopeProperty = scopeProperty.takeIf { isSamePrefix },
                                    isCreatingSemverTag =
                                        isCreatingSemverTag.takeIf { isSamePrefix } ?: false,
                                    mockDate = mockDateProperty,
                                    checkClean = checkCleanProperty,
                                )
                            checkVersionIsHigherOrSame(
                                version,
                                git.lastVersionInCurrentBranch(semverExtension.tagPrefix.get())
                            )
                            version
                        }
                )
            }
    }
}