Dimitri Kopriwa
Dimitri Kopriwa

Reputation: 14353

How to set java target and source version when using gradle maven plugin?

I am using gradle 3.5 and maven plugin for gradle.

I have a task to generate the pom.xml , the pom generated is wrong due to source and target version of java.

This generated a pom.xml for 1.5 (wrong):

task createPom << {
    pom {
        project {
            groupId 'com.domain.api'
            artifactId 'gs-gradle'
            version '0.1.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")
}

This make the gradle makePom task fail:

task createPom << {
    pom {
        project {
            groupId 'com.domain.api'
            artifactId 'gs-gradle'
            version '0.1.0'
            build {
                plugins {
                    plugin {
                        groupId 'org.apache.maven.plugins'
                        artifactId 'maven-compiler-plugin'
                        version '3.7.0'
                        configuration {
                            source '1.8'
                            target '1.8'
                        }
                    }
                }
            }
            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")
}

This is the output error when adding the build object :

* What went wrong:
Execution failed for task ':createPom'.
> No such property: _SCRIPT_CLASS_NAME_ for class: org.apache.maven.model.Model

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Upvotes: 1

Views: 1190

Answers (1)

Dimitri Kopriwa
Dimitri Kopriwa

Reputation: 14353

This is how I solved the target and source issue :

pom {
    project {
        groupId 'com.domain.api'
        artifactId 'gs-gradle'
        version '0.1.0'
        properties {
            project {
                build {
                    sourceEncoding 'UTF-8'
                }
            }
            maven {
                compiler {
                    source '1.8'
                    target '1.8'
                }
            }
        }

        inceptionYear '2008'
        licenses {
            license {
                name 'The Apache Software License, Version 2.0'
                url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                distribution 'repo'
            }
        }
    }
}

This way I was able to set properties.

If you really need to customize the build, you won't be able to declare it the same way because of the plugin in charge of it. This is how you can do:

pom {
    project {
        groupId 'com.domain.api'
        artifactId 'gs-gradle'
        version '0.1.0'
        properties {
            project {
                build {
                    sourceEncoding 'UTF-8'
                }
            }
            maven {
                compiler {
                    source '1.8'
                    target '1.8'
                }
            }
        }

        inceptionYear '2008'
        licenses {
            license {
                name 'The Apache Software License, Version 2.0'
                url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                distribution 'repo'
            }
        }
    }
}.withXml {
    asNode().appendNode('build').appendNode('plugins').with {
        appendNode('plugin').with {
            appendNode('groupId', 'org.springframework.boot')
            appendNode('artifactId', 'spring-boot-maven-plugin')
            appendNode('version', "${springBootVersionDef}")
            appendNode('executions').appendNode('execution').appendNode('goals').with {
                appendNode('goal', 'repackage')
            }
        }
        appendNode('plugin').with {
            appendNode('groupId', 'org.apache.maven.plugins')
            appendNode('artifactId', 'maven-jar-plugin')
            appendNode('version', "3.0.2")
            appendNode('configuration').appendNode('archive').appendNode('manifest').with {
                appendNode('addClasspath', "true")
                appendNode('classpathPrefix', "lib/")
                appendNode('mainClass', "com.domain.api.Application")
            }
        }
    }
}.writeTo("pom.xml")

Upvotes: 1

Related Questions