【问题标题】:Setting Compiler Level For gradle generated Maven pom为 gradle 生成的 Maven pom 设置编译器级别
【发布时间】:2012-03-29 04:18:18
【问题描述】:

我使用 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.xml 的特定部分。我找到了他们配置许可证、开发人员资料等的所有示例,但没有找到插件规范。

【问题讨论】:

    标签: maven-2 gradle


    【解决方案1】:

    我花了一段时间才弄明白。就像彼得上面说的那样,您可以添加该部分。说起来容易做起来难,至少对我来说。

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

    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'
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以添加该部分,方法与示例中添加“许可和开发人员的内容”的方式相同。

      【讨论】:

      • 我试过了,当我尝试将build.plugins.plugin 添加到project 时,它会显示&gt; No such property: _SCRIPT_CLASS_NAME_ for class: org.apache.maven.model.Model
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      相关资源
      最近更新 更多