使用 Gradle 发布多个自定义 jar

发布于 2025-01-14 12:32:21 字数 1710 浏览 3 评论 0原文

我目前正在将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2025-01-21 12:32:21

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

任务

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

这为我提供了我需要的包以及包含适当文件的罐子。唯一的缺点是与 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

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

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

When building the jar Gradle will create the name with the following pattern: [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]. In each task I used archiveAppendix.set() to make sure I got the proper jar name during the publication. Suppose I set archivesBaseName = "Project" and version = '1.0.1' in my build.gradle. The resulting jar from buildClientJar would be Project-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文