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

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

问题描述

虽然现在只有两天,但我肯定会在所有Java项目中使用gradle,并从所有项目的根目录中删除pom.xml。



<但是,我希望保持与maven兼容,因为我希望gradle任务能够在用户需要时在项目的根目录下生成合适的pom.xml。



现在,我所拥有的对pom.xml的唯一引用是在build.gradle文件的这一部分中(这是非常少的修改,找到了什么这里):

  uploadArchives {
存储库{
mavenDeployer {
beforeDeployment {
MavenDeployment部署 - > signing.signPom(部署);
}

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

pom.project {
name$ {name};
包装捆绑;
描述$ {description};
url$ {projectURL};

scm {
url$ {gitroscm};
连接$ {gitroscm};
developerConnection$ {gitrwscm};
}

许可证{
许可证{
名称较低通用公共许可证,版本3或更高版本;
urlhttp://www.gnu.org/licenses/lgpl.html;
分配回购;
}
}

开发者{
开发者{
idwhocares;
名称whocares;
电子邮件whocares;
}
}
}
}
}
}

如何从这个深度嵌套的构造中将 pom.project 解压缩到一个可以生成pom.xml的任务中,生成的pom.xml文件位于 build / poms / pom-default.xml 中,看起来相当不错)?

更重要的是,是否可以从 uploadArchives 中提取 pom.project ,但仍然可以引用它?



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

解决方案

您可以使用 gradle maven插件。这会在您的项目中添加 pom 约定方法,您可以在任务中使用该方法生成 pom.xml 文件,就像

  apply plugin:'maven'

任务createPom<< {
pom {
project {
groupId'org.example'
artifactId'test'
version'1.0.0'

inceptionYear '2008'
许可证{
许可证{
name'Apache软件许可证2.0版'
url'http://www.apache.org/licenses/LICENSE-2.0 .txt'
distribution'repo'
}
}
}
} .writeTo(pom.xml)
}

然后调用 gradle createPom 来生成pom.xml项目根。在pom定义中的所有内容中,您应该提供 groupId artifactId 版本,其他诸如许可证之类的东西并不重要。



您也可以看看这个例子为具有一些依赖性的项目定义,并尝试运行它以查看它产生的内容。


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.

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.

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";
                    }
                }
            }
        }
    }
}

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)?

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

Full link to the build.gradle file: here.

解决方案

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

apply plugin: 'maven'

task createPom << {
    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")
}

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.

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

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