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

Access semver version properties in Gradle build file. #8

Closed
LeoColman opened this issue Jul 24, 2019 · 16 comments
Closed

Access semver version properties in Gradle build file. #8

LeoColman opened this issue Jul 24, 2019 · 16 comments
Assignees
Labels
enhancement New feature or request

Comments

@LeoColman
Copy link

Hey!
How can I read the version properties?

Currently I'm loading them manually

val props = Properties().apply {
    load(file("version.properties").inputStream())
}

Is there an easy way to do it?

@ethauvin ethauvin added the question Further information is requested label Jul 24, 2019
@ethauvin ethauvin self-assigned this Jul 24, 2019
@ethauvin
Copy link
Owner

@Kerooker If your project is in Java or Kotlin, you can use the SemVer Annotation Processor to create a version class that is accessible programmatically within your project.

If you are trying to read the properties file in the Gradle build file, then what you suggested works.

I've never really thought about exposing the properties file content. Maybe as project extensions? Is that what you're looking for?

@LeoColman
Copy link
Author

I have the need to embed the version to other gradle tasks, such as Google App Engine's release or for Android artifact creation.

I'm using the file approach, but I think that perhaps there should be a way to do it out-of-the-box. Something like

android {
     version = semver.version
     build = semver.major * 10000 + semver.minor * 100 + semver.patch
}

(This is actually pretty close to my use case)

@LeoColman
Copy link
Author

And then we can remove user's trouble on fetching these to use in build.gradle

@ethauvin
Copy link
Owner

Have you tried?

version = project.version

That should work, if not try putting it in a doFirst block.

As far as your build calculation, I'd have to look into it. Providing read-only properties in Gradle is not very straight forward.

@ethauvin ethauvin added the enhancement New feature or request label Jul 25, 2019
@ethauvin ethauvin changed the title How to load version inside gradle? Access semver version properties in Gradle build file. Jul 25, 2019
@ethauvin
Copy link
Owner

@Kerooker I've released 1.0.3-beta. It implements a set of semver extension properties. Look at the documentation for a complete list.

Your example should now work as-is. Give it a try and let me know if that works for you.

@LeoColman
Copy link
Author

That's exactly what I wanted!

This works as expected

@ethauvin
Copy link
Owner

Implemented by e1891c4

@ethauvin ethauvin reopened this Jul 26, 2019
@ethauvin ethauvin removed the question Further information is requested label Jul 26, 2019
@ethauvin ethauvin reopened this Jul 26, 2019
ethauvin added a commit that referenced this issue Jul 26, 2019
@mochadwi
Copy link

How to make sure to assign the semver.version for android.defaultConfig.versionName? moving it to afterEvaluate making the BuildConfig.VERSION_NAME not exist (which we use internally in our project)

@ethauvin
Copy link
Owner

@mochadwi I'm not quite sure what you're trying to accomplish. Do you have an example or a project that I can look at?

@mochadwi
Copy link

mochadwi commented Oct 15, 2021

android {

   defaultConfig {
       // no. 1
       versionName = semver.version // will assign a default value, e.g: 1.0.0
       project.afterEvaluate {
           // No. 2

           versionName = semver.version // actual version assigned, e.g: 2.1.1

       }

   }
}

In our case we need to assign the versionName in No. 1 to generate BuildConfig.VERSION_NAME, but as mentioned in the docs, we need use afterEvaluate, doLast or doFirst to assign the correct value.

When updating to use no. 2, seems the BuildConfig.VERSION_NAME not generated anymore, as BuildConfig.VERSION_NAME only generated in Configuration Phase (cmiiw)

We're ended up using the readLines files manually to get the correct semver.version

@ethauvin
Copy link
Owner

@mochadwi I have no idea if it'll work, but you could try:

versionName = ${-> semver.version}

It might work, let me know.

@mochadwi
Copy link

Thanks, will let you know ASAP 🙏

@mochadwi
Copy link

mochadwi commented Oct 15, 2021

@mochadwi I have no idea if it'll work, but you could try:

versionName = ${-> semver.version}

It might work, let me know.

is it Lambda argument? needs to find an alternative for build.gradle.kts (kotlin script)

never came across this though :D

@ethauvin
Copy link
Owner

@mochadwi It's a lazy instantiation of a GString. I don't think there's an equivalent in Kotlin, unfortunately.

Have you tried setting the version as a late variable? Might work, but I have my doubts.

@mochadwi
Copy link

Thanks for the reference, will update you later. Currently still AFK 🙏

@mochadwi
Copy link

@mochadwi It's a lazy instantiation of a GString. I don't think there's an equivalent in Kotlin, unfortunately.

Have you tried setting the version as a late variable? Might work, but I have my doubts.

Does the late variable different with the lazy instantiation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants