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?
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.
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.
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.
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.
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
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With