如何让 gradle 在项目的根目录下为 maven 用户生成一个有效的 pom.xml 文件?

新手上路,请多包涵

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

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

此刻,我唯一对 pom.xml 的引用是在 build.gradle 文件的这一部分(这是,经过很少的修改,可以在 这里 找到):

 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 并且看起来相当好的)?

更重要的是,是否有可能从 pom.project 中提取出 uploadArchives 同时仍然能够引用它?

build.gradle 文件的完整链接: 此处

原文由 fge 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 973
2 个回答

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

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。 Of all the things in the pom definition, you should really provide groupId , artifactId and version , other thins like licenses are not that重要的。

您还可以查看 此示例 以了解具有某些依赖项的项目定义,并尝试运行它以查看其生成的内容。

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

原文由 jmruc 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是我的 build.gradle

 apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'

repositories {
mavenLocal()
mavenCentral()
}

dependencies {

    compile 'org.springframework:spring-core:4.0.5.RELEASE'
    compile 'org.springframework:spring-webmvc:4.0.5.RELEASE'

    compile 'org.slf4j:slf4j-api:1.7.5'
    runtime 'org.slf4j:slf4j-log4j12:1.7.5'

    testCompile 'org.springframework:spring-test:4.0.5.RELEASE'

    testCompile 'junit:junit:4.11'
    testCompile "org.mockito:mockito-core:1.9.5"
    testCompile "org.hamcrest:hamcrest-library:1.3"

    testCompile 'javax.servlet:javax.servlet-api:3.0.1'
}
test {
  testLogging {
    // Show that tests are run in the command-line output
    events 'started', 'passed'
  }
}
task wrapper(type: Wrapper) { gradleVersion = '1.12' }
task createPom  {
    pom {
        project {
            groupId 'sg.test.spring.web.guide'
            artifactId 'sg-web-initial'
            version '1.0.0-SNAPSHOT'

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

您可以根据需要将任务命名为 createPomanyTaskName 。然后只需运行 gradle cleangrale build 或简单地运行 gradle createPom

这将在项目的根目录中将其生成为 pom.xml。尽管您可以将 writeTo("pom.xml") 替换为 writeTo("<anyDir>/newpom.xml")

生成的 pom.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sg.test.spring.web.guide</groupId>
    <artifactId>sg-web-initial</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <inceptionYear>2008</inceptionYear>
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.5.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.0.5.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

原文由 sanjeev 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题