使用 Gradle 发布多个自定义 jar
我目前正在将 Ant 项目转换为 Gradle,并且有一些输出自定义 jar 的构建目标。我很好奇我是否/如何使用 Gradle 来解决这个问题?我知道我可以使用 ant.importBuild
从 Gradle 运行 Ant 任务,但我想运行每个任务并将生成的 jar+artifacts 发布到远程存储库。
例如,我有这两个 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 中运行这些相同/相似的任务并将它们发布到远程存储库?
I'm currently working on converting an Ant project over to Gradle and there are some build targets that output custom jars. I was curious if/how I'd go about this with Gradle? I know I can run Ant tasks from Gradle using ant.importBuild
, but I'd like to run each task and publish the resulting jar+artifacts to a remote repository.
For example, I have these two Ant target builds:
<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>
and
<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>
Is it possible to run these same/similar tasks in Gradle and publish them to a remote repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够弄清楚,但文档不太清楚如何做到这一点,并且需要大量搜索和拼接各种答案,但这是我能够想出的解决方案:
任务
发布
构建时的 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
。这为我提供了我需要的包以及包含适当文件的罐子。唯一的缺点是与 jar 一起打包的
pom.xml
不包含依赖信息。这将是我要解决的下一个任务。完成后,我将编辑此响应以包含适当的信息。I was able to figure it out but the docs weren't very clear on how to do this and required quite a bit of searching and stitching together various answers, but this is the solution I was able to come up with:
Tasks
Publishing
When building the jar Gradle will create the name with the following pattern:
[archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]
. In each task I usedarchiveAppendix.set()
to make sure I got the proper jar name during the publication. Suppose I setarchivesBaseName = "Project"
andversion = '1.0.1'
in mybuild.gradle
. The resulting jar frombuildClientJar
would beProject-Client-1.0.1.jar
.This gives me the packages I need as well as the Jars containing the appropriate files. The only downside to this is the
pom.xml
that is packaged with the jar contains no dependency information. So that will be my next task to figure out. Once that is done, I'll edit this response to contain the appropriate info.