Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 618
Manuel Jordan Avatar asked Jun 17 '16 07:06

Manuel Jordan


People also ask

How create POM XML from gradle?

You can name the task createPom to anyTaskName as you like. Then just run gradle clean or grale build or simply gradle createPom . This will generate it as pom. xml in the root of the project.

Can gradle project have pom XML?

gradle which you can now use to run all your builds using gradle commands. Please note that gradle init automatically detects the pom. xml and creates a gradle build with the java and maven plugin loaded. It means that existing Maven dependencies are automatically converted and added to your gradle build file.

Can I convert a gradle project to Maven?

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories. The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

How do I create an executable jar in gradle?

Method 1 (no need for application or other plugins)Copy library JARs in libs sub-directory of where you put your result JAR. Make sure your library JAR files do not contain space in their file name. Note that Java requires us to use relative URLs for the Class-Path attribute.


2 Answers

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

like image 168
Alex78191 Avatar answered Oct 31 '22 00:10

Alex78191


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")
}
like image 44
Brandon McKenzie Avatar answered Oct 31 '22 01:10

Brandon McKenzie