Manuel Jordan
Manuel Jordan

Reputation: 107

Generate pom.xml in jar with gradle

I want to generate with IntelliJ/Gradle a jar with a pom.xml inside in order to use it in another project. I tried the following code for build.gradle:

group 'com.test'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'maven-publish'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}
jar {
    into("META-INF/maven/$project.group/$project.name") {
        from generatePomFileForMavenJavaPublication
        rename ".*", "pom.xml"
    }
}
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile group: 'postgresql', name: 'postgresql', version: '9.1-901.jdbc4'
}

But I'm getting following error:

'jar' cannot be applied to '(groovy.lang.closure <org.gradle.api.tasks.bundling.AbstractArchiveTask> )'

And gradle says:

Could not find property 'generatePomFileForMavenJavaPublication' on
task ':jar'.

Or does someone know another method?

Upvotes: 9

Views: 10480

Answers (3)

Alex78191
Alex78191

Reputation: 3086

The task generatePomFileForMavenJavaPublication is created late during the configuration phase. You will have to defer the creation of the reference to the task by assigning the task as Closure return type in the from statement.

apply plugin: 'maven-publish'
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

jar {
    into("META-INF/maven/$project.group/$project.name") {
        from { generatePomFileForMavenJavaPublication }
        rename ".*", "pom.xml"
    }
}

from Github

Upvotes: 4

Manuel Jordan
Manuel Jordan

Reputation: 107

I just found out that the pom.xml doesn't have to be inside the jar.

It works when I publish to Maven Local (and not Maven Central):

group 'ch.test'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'maven-publish'

sourceCompatibility = 1.5

repositories {
    mavenLocal()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

        }
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile group: 'postgresql', name: 'postgresql', version: '9.1-901.jdbc4'
}

Upvotes: -1

Brandon McKenzie
Brandon McKenzie

Reputation: 1685

from wants a file or path. What you're feeding it, generatePomFileForMavenJavaPublication is a task, which is most definitely not a file or path, so that's not going to work.

What you can do is make generatePomFileForMavenJavaPublication a prerequisite for jar and pick up the pom it creates in your build directory.

Something like the following should accomplish this:

jar {
    dependsOn generatePomFileForMavenJavaPublication
    from("$builddir/pom.xml") //Or whatever path it goes to in the generate task.
    into("META-INF/maven/$project.group/$project.name")
}

Upvotes: 1

Related Questions