uploadArchives does not upload artifacts generated from other gradle files - gradle

In my build.gradle, I have war task which generates a war file and couple tasks which generate jar file. Jar generating tasks are defined in other gradle files and are imported in main build.gralde using apply from clause.
Problem is uploadArchives only uploads war file and does not upload jar files which are generated from other files.
What could be the potential cause of problem here?
My build.gradle looks like as follows
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven'
war {
.... // generates war file
}
apply from: 'Createjar1.gradle' // creates jar1
apply from: 'Createjar2.gradle' // creates jar2
apply from: 'Createjar3.gradle' // creates jar3
uploadArchives {
repositories {
mavenDeployer {
repository(url:"${nexusURL}") {
authentication(userName: 'username', password: 'password')
}
snapshotRepository(url: "${nexusURL}") {
authentication(userName: 'username', password: 'password')
}
}
}
}

By default the 'maven' plugin will upload any artifacts contained in the 'archives' configuration. Your WAR is automatically added to that configuration but additional artifacts have to be explicitly defined.
artifacts {
archives jar1Task
archives jar2Task
archives jar3Task
}

Related

Gradle - uploadArchives generated snapshot is different from createJar

With my build.gradle file I am creating a jar file using a createJar task, which I later upload using an upload using an uploadArchives task.
The project is in groovy, structured like:
/src/main/groovy/package.name
/src/test/groovy
It's a test jar file, where the main class is in /src/test/groovy and it uses for processing classes from /src/main/groovy/package.name
The createJar task works and creates the jar that I need correctly.
So far so good.
The problem is when I run uploadArchives, which generates a snapshot.jar and it only contains the classes from /src/main/groovy/package.name
Why are the 2 jar files different ?
How can I make uploadArchives to upload the jar from createJar (or at least include the test classes as well & run it using the main class specified in createJar) ?
In build.gradle I have something like :
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin: 'maven'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
artifacts {
archives file: file('./build/libs/name-'+version+'.jar'), name: 'name', type: 'jar'
}
task createJar(type: Jar) {
classifier 'test'
from sourceSets.test.output
manifest {
attributes 'Implementation-Title': 'Gradle Jar File',
'Implementation-Version': version,
'Main-Class': 'MainClassName'
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://url/snapshotsFolder/") {
authentication(userName: "user", password: "pass")
}
}
}
}
To extend my comment, let me add an example:
jar {
classifier 'test'
from sourceSets.test.output
manifest {
attributes 'Implementation-Title': 'Gradle Jar File', 'Implementation-Version': version, 'Main-Class': 'MainClassName'
}
}
This will configure the task jar of type Jar, which is created by the java plugin.

Gradle is not publishing custom jar artifact

I have a gradle build that must publish a pre-built jar file as an artifact. By some reason it is not being picked up. Here's a simplified version to reproduce it:
folder contents:
settings.gradle
build.gradle
some.jar
settings.gradle:
rootProject.name = 'some'
build.gradle:
apply plugin: 'base'
apply plugin: 'maven-publish'
group = 'foo.bar'
version = '1.0'
task copyPrebuilt(type: Copy) {
from(projectDir) {
include 'some.jar'
rename 'some', "some-$version"
}
into distsDir
}
artifacts {
// should be 'archives'?
// http://stackoverflow.com/questions/39068090/gradle-archives-artifacts-not-populated-to-default-configuration
'default' (file("$distsDir/some-${version}.jar")) {
name 'some'
type 'jar'
builtBy copyPrebuilt
}
}
some-1.0.jar is not copied to ~/.m2/repository/foo/bar/some/1.0 by gradle publishToMavenLocal
any clues?
The easiest way to get something published is to define a publication object:
apply plugin: 'base'
apply plugin: 'maven-publish'
group = 'foo.bar'
version = '1.0'
task copyPrebuilt(type: Copy) {
from(projectDir) {
include 'some.jar'
rename 'some', "some-$version"
}
into buildDir
}
publishToMavenLocal.dependsOn copyPrebuilt
publishing {
publications {
prebuilt(MavenPublication) {
artifact file("$buildDir/some-${version}.jar")
}
}
}
This code makes publishToMavenLocal task install some-1.0.jar to the local maven repository.

Publish (rootproject) pom without (rootproject) publishing artifact / packaging = pom

I'm migrating one of our projects from maven to gradle: it's a gradle multi-project & all subprojects are publishing artifacts to artifactory. So far so good.
The legacy (maven-based) build environment however also expects the root project to publish a pom file with the "packaging" node equal to "pom" (common maven behaviour, so it seems)
So now, I'm trying to have this generated by Gradle, but only find ways to customize an automatically generated pom for each artifact, I can't find a way to generate/upload a pom without publishing an actual artifact.
Workaround for now is to have the root project use the java plugin, generate/install an empty jar and manipulate the generated pom to conform to maven expectations (packaging=pom), but that's a hack.
Is there a way to have this root pom file generated with gradle ?
Example project:
settings.gradle
rootProject.name = 'MultiProject'
include 'child01', 'child02'
rootProject.children.each { it.name = rootProject.name + "-" + it.name }
build.gradle
subprojects {
apply plugin: 'java'
}
allprojects {
apply plugin: 'maven'
group = 'my_group'
version = '0.0.1-SNAPSHOT'
}
EDIT (current workaround), addition to build.gradle
// workaround to generate pom
apply plugin: 'java'
configurations {
pomCreation
}
task createPom {
ext.newPomFile = "${buildDir}/blabla.pom"
doLast {
pom {
project {
packaging 'pom'
}
}.writeTo(newPomFile)
}
}
install.dependsOn(createPom)
artifacts {
pomCreation file(createPom.newPomFile)
}
I would use the gradle maven-publish plugin for that. With that plugin you can define your specific pom and don't have to upload other artifacts. Here an example:
publishing {
publications {
maven(MavenPublication) {
pom.withXml{
def xml = asNode()
xml.children().last() + {
delegate.dependencies {
delegate.dependency {
delegate.groupId 'org.springframework'
delegate.artifactId 'spring-context'
delegate.version( '3.2.8.RELEASE' )
}
}
}
}
}
}

How to refactor uploadArchives for some subprojects at root build.gradle?

I have a multi project, and I want to upload some of the subprojects artifact to the maven repository.
For now, I wrote the following code into the main build.gradle:
task sourcesJar(type: Jar, dependsOn: classes) { ... }
project(':subProjName1') {
apply plugin: 'java'
apply plugin: 'maven'
configurations {
subProjName1Archives
}
uploadSubProjName1Archives {
repositories {
mavenDeployer {
repository(url: "file:///$rootDir/mvn-repo/")
}
}
}
artifacts {
subProjName1Archives jar
subProjName1Archives sourcesJar
}
}
project(':subProjName2') { ... }
...
project(':subProjNameN') { ... }
And do following for upload archives:
gradlew.bat uploadSubProjName1Archives
gradlew.bat uploadSubProjName2Archives
...
gradlew.bat uploadSubProjNameNArchives
It's doing what I want, but how can I generalize it into one task in the main build.gradle?
If you put the above code into a subprojects { .. } block in the root build script, you can invoke all tasks at once with gradle uploadMyConfiguration. (Only) if you have a concrete need for a single task (e.g. because another task depends on all artifacts being uploaded), you can add a further lifecycle task:
task uploadAll {
dependsOn { subprojects.uploadMyConfiguration }
}
PS: Unless you have a good reason not to, you can reuse the existing archives configuration and uploadArchives task. The archives configuration already contains the Jar produced by the jar task, so you just have to add the sources Jar.

How to upload an existing collection of 3rd-party Jars to a Maven server in Gradle?

How can I upload a collection of existing Jars to a Maven repository? The Jars are built from an ANT Task imported to Gradle, and used as a dependency to my task... The Jars don't have version tag, so they should ALL receive the same version number when they are uploaded...
apply plugin: 'java'
apply plugin: 'maven'
version = "6.1.1"
group = "com.oahu"
ant.importBuild "$projectDir/tools/ant/package.xml"
uploadArchives(dependsOn: ["oahu-jar", "client-sdk-jar", "common-jar"]) << {
// the dependencies will generate "oahu.jar", "oahu_client_sdk.jar", "common.jar"
// UPLOAD THE DEPENDENCIES LISTED ABOVE LOCATED AT the subdirectory "build/"
description = "Uploads the generated jar ${archivesBaseName}-${version}.jar to ${cn_mvn_serverUrl}"
repositories.mavenDeployer {
repository(url: "${cn_mvn_releaseUrl}") {
authentication(userName: "${cn_mvn_username}", password: "${cn_mvn_password}")
}
}
}
The tasks "oahu-jar", "client-sdk-jar", "common-jar" are the ones imported from ANT... I have the Maven repositories configuration already working from another project... But the Maven plugin uploads the Jar generated by the Jar task from the Java plugin... Considering the imported ANT tasks generates:
build.gradle
src
build
|-"oahu.jar"
|-"oahu_client_sdk.jar"
|-"common.jar"
The result of this should be the upload of those Jars with the given version...
"oahu-6.1.1.jar", "oahu_client_sdk-6.1.1.jar", "common-6.1.1.jar"... all uploaded to the Maven repository...
Add sourceSets? Configuration? Artifacts?
currently that is not explicitly supported by gradle so you have to do some scripting for that. Based on your snippet above, I've created a sample snippet, that should be easy to adapt:
apply plugin:'java'
apply plugin:'maven'
import org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact
version = "6.1.1"
group = "com.oahu"
ant.importBuild "$projectDir/tools/ant/package.xml"
// a list of the ant tasks that create a jar
// I assumed the following convention:
// ant task named "SampleAntJar-jar" creates the jar "build/SampleAntJar.jar"
def antJarTasks = ["SampleAntJar-jar", "SecondSampleAntJar-jar"]
artifacts{
//for each ant task add a defaultpublishArtifact to the archives configuration
antJarTasks.each{ taskName ->
def artifactName = taskName - '-jar'
archives new DefaultPublishArtifact(artifactName, "jar", "jar", null, new
Date(), new File("$buildDir", "${artifactName}.jar"))
}
}
uploadArchives(){
dependsOn: antJarTasks
repositories {
mavenDeployer {
repository(url: "file://{'/Users/Rene/.m2/repository/'}")
antJarTasks.each{ antJarTask ->
antJarName = antJarTask - "-jar"
addFilter(antJarName) {artifact, file ->
artifact.name == antJarName
}
pom(antJarName).artifactId = antJarName
}
}
}
}
regards,
René

Resources