英文:
Using java variable in build.gradle
问题
You can use a java variable in your build.gradle version field like this:
group = 'project_name'
version = project.ext.myJavaVariable
sourceCompatibility = '1.8'
Make sure to define myJavaVariable
in your Java code, and it will be accessible in your build.gradle script.
英文:
Is there any way to use a java variable in my build.gradle version field so I don't need to manually change the build script everytime I make a new build?
So something like this
group = 'project_name'
version = <insert variable from java file>
sourceCompatibility = '1.8'
答案1
得分: 1
使用Java源文件来管理项目的版本是不合理的。您应该让构建系统(在这种情况下是Gradle)来管理版本。当您在Gradle中更新版本时,应该设置构建方式,以便新版本能够在整个项目中传播。
在构建脚本中,这很容易实现;您只需在需要的地方引用version
属性即可。这个概念与在应用程序中传递数据没有任何不同。请记住,在Gradle中,构建脚本是用Groovy或Kotlin编写的,正常的编程逻辑也适用(构建脚本实际上是一个实现了org.gradle.api.Project
和org.gradle.api.Script
的对象)。
更难的部分是确保您的应用程序可以在不必同时更新构建脚本和源代码版本的情况下查看版本。然而,有许多方法可以实现这一点,例如:
- 创建一个属性文件作为资源,然后使用
processResources
Gradle任务将项目的版本注入到该属性文件中。然后在运行时读取资源以获取应用程序版本。 - 创建一个自定义Gradle任务,生成一个属性文件作为资源。然后在运行时读取资源以获取应用程序版本。
- 创建一个自定义Gradle任务,生成表示应用程序版本的Java源文件(例如
AppVersion.java
)。 - 使用
jar
Gradle任务设置清单的Implementation-Version
属性。然后在运行时读取此属性以获取应用程序的版本。 - 如果使用Java 9+和模块,则可以让
compileJava
Gradle任务设置模块版本。然后在运行时读取模块版本。
您还可以考虑将版本保存在gradle.properties
文件中,并从那里读取到构建脚本中(Gradle提供了相应的API)。这样,当您更新版本时,构建脚本不必重新编译(至少在使用Kotlin DSL的情况下是这样的)。
英文:
Using a Java source file to manage the version of your project is backwards. You should let your build system—in this case, Gradle—manage the version. When you update the version in Gradle you should have setup the build in such a way that the new version is propagated throughout the project.
Within the build script this is easily accomplished; you simply have to reference the version
property everywhere you need it. The concept isn't any different than passing data around in your application. Remember that, in Gradle, the build script is written in Groovy or Kotlin and normal programming logic applies (the build script is essentially an object which implements both org.gradle.api.Project
and org.gradle.api.Script
).
The more difficult part is making sure your application can see the version without having to update the version in both the build script and the source code. There are, however, a number of ways to accomplish this, such as:
- Create a properties file as a resource and use the
processResources
Gradle task to inject the project's version into said properties file. Then read the resource at runtime to get the application version. - Create a custom Gradle task which generates a properties file as a resource. Then read the resource at runtime to get the application version.
- Create a custom Gradle task which generates the Java source file (e.g.
AppVersion.java
) that represents your application's version. - Use the
jar
Gradle task to set theImplementation-Version
attribute of the manifest. Then read this attribute at runtime to get the application's version. - If using Java 9+ and modules then you can have the
compileJava
Gradle task set the module version. Then you can read the module version at runtime.
You might also want to consider keeping the version in the gradle.properties
file and reading it from there into your build script (Gradle provides an API for this). That way when you update the version the build script doesn't have to be recompiled (at least that's the case with the Kotlin DSL).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论