使用渐变发布多个自定义Jar



我目前正在将Ant项目转换为Gradle,有一些构建目标可以输出自定义jar。我很好奇我会不会/怎么和格雷德尔做这件事?我知道我可以使用ant.importBuild从Gradle运行Ant任务,但我想运行每个任务,并将生成的jar+工件发布到远程存储库。

例如,我有两个Ant目标构建:

<target name="build-client-jar" depends="compile-source"
description="Builds the client application JAR.">
<jar destfile="${build.dir}/${project.client.jar}"
compress="true"
index="true">
<manifest>
<attribute name="Class-Path"
value="${project.jar}"/>
<attribute name="Built-By" value="${user.name}"/>
</manifest>
<fileset dir="${classes.dir}">
<include name="com/example/project/client/**" />
<include name="*.xml" />
</fileset>
</jar>
</target>

<target name="build-model-jar" depends="compile-source"
description="Builds the model application JAR.">
<jar destfile="${build.dir}/Aps-Model.jar"
compress="true"
index="true">
<manifest>
<attribute name="Class-Path"
value="${project.jar}"/>
<attribute name="Built-By" value="${user.name}"/>>
</manifest>
<fileset dir="${classes.dir}">
<include name="com/example/project/model/**" />
</fileset>
</jar>
</target>

是否可以在Gradle中运行这些相同/相似的任务,并将它们发布到远程存储库?

我能够弄清楚,但文档不太清楚如何做到这一点,需要大量的搜索和拼接各种答案,但这是我能够想出的解决方案:

任务

task buildClientJar(type: Jar) {
archiveAppendix.set("client")
manifest {
attributes("Class-Path": "Project-Client.jar")
attributes(common, "common")
}
include(["com/project/path/client/**", "serviceBean.xml"])
from configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
with jar
}
task buildModelJar(type: Jar) {
archiveAppendix.set("model")
manifest {
attributes("Class-Path": "Project-Model.jar")
attributes(common, "common")
}
include(["com/project/path/model/**"])
from configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
with jar
}

发布

publishing {
publications {
client(MavenPublication) {
artifact buildClientJar
artifactId "Project-Client"
}
model(MavenPublication){
artifact buildModelJar
artifactId "Project-Model"
}
}
repositories {[repo implementation]}
}

在构建jar时,Gradle将使用以下模式创建名称:[archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]。在每个任务中,我都使用archiveAppendix.set()来确保在发布过程中获得正确的jar名称。假设我在build.gradle中设置了archivesBaseName = "Project"version = '1.0.1'。从buildClientJar得到的jar将是Project-Client-1.0.1.jar

这给了我所需的包以及包含适当文件的Jars。唯一的缺点是与jar一起打包的pom.xml不包含依赖性信息。所以这将是我的下一个任务。完成后,我将编辑此响应以包含适当的信息。

最新更新