设置编译器级别对于gradle生成的Maven pom

时间:2012-03-12 18:22:32

标签: maven-2 gradle

我使用gradle作为项目依赖项管理器,但由于我更喜欢​​netbeans并且无法找到与maven的本机集成,因此我将gradle生成的默认pom复制为pom.xml。但是如何设置源和目标级别?

我的build.gradle看起来像

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

targetCompatibility=1.6
sourceCompatibility=1.6

我跑完后

gradle install

并检查build / poms / pom-default.xml,它从不配置源或目标级别,默认为1.3

我缺少的是maven编译器插件配置

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

并且无法找到如何配置pom的特定部分。我找到了他们配置许可证,开发人员等等但不是插件特定的所有示例。

2 个答案:

答案 0 :(得分:3)

花了一些时间来弄明白。就像彼得在上面说的那样你可以添加那个部分。说起来容易做起来难,但至少对我而言。

幸运的是,spring正在使用gradle,所以你有很多真实世界的例子。检查git hub

install {
    repositories.mavenInstaller {
        customizePom(pom, project)
    }
}

def customizePom(pom, gradleProject) {
    pom.whenConfigured { generatedPom ->
        // respect 'optional' and 'provided' dependencies
        gradleProject.optionalDeps.each { dep ->
            generatedPom.dependencies.find { it.artifactId == dep.name }?.optional = true
        }
        gradleProject.providedDeps.each { dep ->
            generatedPom.dependencies.find { it.artifactId == dep.name }?.scope = 'provided'
        }

        // eliminate test-scoped dependencies (no need in maven central poms)
        generatedPom.dependencies.removeAll { dep ->
            dep.scope == 'test'
        }

        // add all items necessary for maven central publication
        generatedPom.project {
            name = gradleProject.description
            description = gradleProject.description
            organization {
                name = 'bajoneando'
            }
            build {
                plugins {
                    plugin {
                        groupId = 'org.apache.maven.plugins'
                        artifactId = 'maven-compiler-plugin'
                        configuration {
                            source = '1.6'
                            target = '1.6'
                        }
                    }
                    plugin {
                        groupId = 'org.apache.maven.plugins'
                        artifactId = 'maven-surefire-plugin'
                        configuration {
                            includes {
                                include = '**/*Tests.java'
                            }
                            excludes {
                                exclude = '**/*Abstract*.java'
                            }
                        }
                    }
                }
                resources {
                    resource {
                        directory = 'src/main/java'
                        includes = ['**/*']
                        excludes = ['**/*.java']
                    }
                    resource {
                        directory = 'src/main/resources'
                        includes = ['**/*']
                    }
                }
                testResources {
                    testResource {
                        directory = 'src/test/java'
                        includes = ['**/*']
                        excludes = ['**/*.java']
                    }
                    testResource {
                        directory = 'src/test/resources'
                        includes = ['**/*']
                    }
                }
            }
            developers {
                developer {
                    id = 'lnramirez'
                    name = 'Luis Ramirez Monterosa'
                    email = '*****@gmail.com'
                }
            }
        }
    }
}

答案 1 :(得分:1)

您可以使用与“许可证和开发人员资料”中的示例相同的方式添加该部分。