gpt4 book ai didi

java - Gradle:访问构建信息

转载 作者:搜寻专家 更新时间:2023-11-01 03:38:09 24 4
gpt4 key购买 nike

我希望从使用 gradle 构建项目的 java 项目中访问构建信息。我需要访问的信息是 teamcity 生成的构建号、构建 vcs 号等。maven 可以轻松访问这些信息,使用 maven-replacer-plugin 可以在属性文件中替换这些信息。首先,gradle 中是否有替代方案来实现相同的目标?然后,怎么做? :)

我尝试了共享的方法 here ,但我总是将 null 作为内部版本号。

在maven中,我在项目中放置了一个属性文件(project.properties)来保存键值对,键是那些项目信息,如

project.groupId, project.artifactId, project.version, maven.build.timestamp

我希望使用 gradle 访问相同的构建信息。. pom 如下所示:

                    <!-- Write project version number to file -->
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.3.8</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<!-- replace the token in this file -->
<include>target/classes/project.properties</include>
</includes>
<replacements>
<replacement>
<token>PROJECT_GROUP</token>
<value>${project.groupId}</value>
</replacement>
<replacement>
<token>PROJECT_ARTIFACT</token>
<value>${project.artifactId}</value>
</replacement>
<replacement>
<token>PROJECT_VERSION</token>
<value>${project.version}</value>
</replacement>
<replacement>
<token>PROJECT_BUILD_DATE</token>
<value>${maven.build.timestamp}</value>
</replacement>
<replacement>
<token>BUILD_NUMBER</token>
<value>${build.number}</value>
</replacement>
</replacements>
</configuration>
</plugin>

maven replacer 插件所做的是替换 properties 对象中的值,可以在 java 应用程序中使用。

我需要使用 gradle 访问相同的属性。

pom 中的 Team City 配置如下:

<ciManagement>
<system>Team City</system>
<url>http://teamcity.mycompany.com/teamcity/project.html?projectId=bt007
</url>
</ciManagement>

build.gradle 文件中,我有一个定义如下的任务:

task getProjectGroup << {
ext.projectGroup = System.getProperty("project.group")
println "Project Group: $projectGroup"
}

执行此任务时我得到以下输出:

:service:getProjectGroup
Project Group: null

BUILD SUCCESSFUL

最佳答案

我在我的项目中做了一些非常相似的事情——将构建信息打包到一个属性文件中,该文件可以从 Java 应用程序的类路径中读取。

以下是我在 Gradle 脚本中完成此操作的方法:

tasks.withType(Jar).all { Jar jar ->
jar.doFirst {
String jarType = jar.extension
if(jar.classifier) jarType = jar.classifier
String fileName = "project.properties"
ant.propertyfile(file: "${jar.temporaryDir}/${fileName}") {
entry(key: "PROJECT_GROUP", value: project.group)
entry(key: "PROJECT_ARTIFACT", value: project.archivesBaseName)
entry(key: "PROJECT_VERSION", value: project.version)
entry(key: "PROJECT_BUILD_DATE", value: new Date())
entry(key: "BUILD_NUMBER", value: hasProperty("teamcity") ? teamcity["build.number"] : "local")
}
String intoPath = "your/package/name/here/${project.name}/${jarType}"
jar.from(jar.temporaryDir) {
include fileName
if(jar instanceof War) intoPath = "WEB-INF/classes/${intoPath}"
into(intoPath)
}
println "\tCreated ${intoPath}/${fileName}"
}
}

在这里,我为每个 Jar 任务(包括 War)添加功能,为该存档创建一个属性文件并将其包含在 下的类路径中你的/package/name/here/${project.name}/${jarType}/project.properties.

这就是 Gradle 的美妙之处。它使像这样的定制变得非常简单——不需要插件。

然后,为了读取我的应用程序中的属性,我注入(inject)或硬编码文件的预期路径并加载属性,如下所示:

public Properties lookupClassPathResource(String pathToResource) {
Properties p = null;
org.springframework.core.io.Resource r = new org.springframework.core.io.ClassPathResource(pathToResource);
if(r.exists()) {
try {
p = org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(r);
} catch (IOException e) {
//log or wrap/rethrow exception
}
}
return p;
}

所有人都玩得很开心!

编辑:显然,OP 中链接的问题/答案(关于访问 build.number)有点过时了。您可以使用 teamcity 属性,而不是执行 System.getProperty("build.number"),TeamCity 通过 Gradle 初始化脚本隐式添加到您的项目中。上面的代码已被修改以反射(reflect)这一点。另见 this question其他例子。

关于java - Gradle:访问构建信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23679292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com