如何让 gradle 在项目的根目录为 maven 用户生成一个有效的 pom.xml 文件? [英] How to make gradle generate a valid pom.xml file at the root of a project for maven users?

查看:42
本文介绍了如何让 gradle 在项目的根目录为 maven 用户生成一个有效的 pom.xml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然现在只有两天,但我绝对喜欢在我所有的 Java 项目中使用 gradle,并从我所有项目的根目录中删除 pom.xml.

While only for two days now, I am definitely sold on using gradle for all of my Java projects, and drop pom.xml from the root of all my projects.

但是,我希望保持 maven 兼容,从某种意义上说,如果用户需要,我希望 gradle 任务能够在项目的根目录生成合适的 pom.xml.

However, I would like to remain maven-compatible, in the sense that I would like for a gradle task to be able to generate a suitable pom.xml at the root of the project should the user want it.

此时,对 pom.xml 的唯一引用是在 build.gradle 文件的这一部分(这是经过很少修改的内容,此处):

At this moment, the only reference to a pom.xml I have is in this section of the build.gradle file (this is, with very few modifications, what is found here):

uploadArchives {
    repositories {
        mavenDeployer {
            beforeDeployment {
                MavenDeployment deployment -> signing.signPom(deployment);
            }

            repository(url: sonatypeRepoURI) {
                authentication(userName: sonatypeUsername,
                    password: sonatypePassword);
            }

            pom.project {
                name "${name}";
                packaging "bundle";
                description "${description}";
                url "${projectURL}";

                scm {
                    url "${gitroscm}";
                    connection "${gitroscm}";
                    developerConnection "${gitrwscm}";
                }

                licenses {
                    license {
                        name "Lesser General Public License, version 3 or greater";
                        url "http://www.gnu.org/licenses/lgpl.html";
                        distribution "repo";
                    }
                }

                developers {
                    developer {
                        id "whocares";
                        name "whocares";
                        email "whocares";
                    }
                }
            }
        }
    }
}

我如何从这个非常深的嵌套构造中提取 pom.project 到一个可以生成 pom.xml 的任务中(默认情况下,生成的 pom.xml 在 build/poms/pom-default.xml 看起来还不错)?

How would I extract the pom.project out of this very deeply nested construct into a task which could generate a pom.xml (by default, the generated pom.xml is in build/poms/pom-default.xml and looks quite good)?

更重要的是,是否有可能将 pom.projectuploadArchives 中提取出来,同时仍然可以引用它?

More importantly, is it possible to extract that pom.project out of uploadArchives while still being able to refer to it?

build.gradle 文件的完整链接:这里.>

Full link to the build.gradle file: here.

推荐答案

您可以使用 gradle maven 插件.这会将 pom 约定方法添加到您的项目中,您可以在任务中使用它来生成 pom.xml 文件,例如

You can use the gradle maven plugin. This adds the pom convention method to your project, which you can use in a task to generate a pom.xml file, like

task writeNewPom {
    doLast {
        pom {
            project {
                groupId 'org.example'
                artifactId 'test'
                version '1.0.0'
                inceptionYear '2008'
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution 'repo'
                    }
                }
            }
        }.writeTo("pom.xml")
    }
}

然后调用gradle createPom在项目根目录生成pom.xml.在 pom 定义中的所有内容中,您应该真正提供 groupIdartifactIdversion,其他诸如 licenses 没那么重要.

Then you call gradle createPom to generate the pom.xml in the project root. Of all the things in the pom definition, you should really provide groupId, artifactId and version, other thins like licenses are not that important.

您还可以查看此示例 以获得具有某些依赖项的项目定义,然后尝试运行它看看它产生了什么.

You can also look at this example for a project definition with some dependencies, and try running it to see what it produces.

添加了一些新关键字,弃用了一些技术.请检查

Some of the new keywords were added and some techniques were deprecated. Please check

这篇关于如何让 gradle 在项目的根目录为 maven 用户生成一个有效的 pom.xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆