Gradle - uploadArchives generated snapshot is different from createJar - gradle

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.

Related

gradle-release build and publish non-snapshot artifacts

I have a multi-module project. And, I want to be able do just do gradle release and get all artifacts of all modules released.
One of the problems is that when I include the plugins, I get
Task with name 'build' not found in root project '
So, following some advice on the internet I created a build task and added a bunch of dependencies on it:
plugins {
id "net.researchgate.release" version "2.6.0"
}
defaultTasks 'clean', 'assemble'
def repoUrl = System.env.REPO ?: "https://company.jfrog.io/company/maven"
allprojects {
repositories {
mavenCentral()
jcenter()
maven {
url 'https://dl.bintray.com/palantir/releases'
}
maven {
credentials {
username System.env.REPO_USER
password System.env.REPO_PASS
}
url repoUrl
name 'company'
}
}
}
task build{}
subprojects { thisProject ->
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'checkstyle'
apply plugin: 'maven-publish'
apply plugin: 'findbugs'
build.dependsOn "${thisProject}:build"
publishing {
repositories {
maven {
credentials {
username System.env.REPO_USER
password System.env.REPO_PASS
}
url repoUrl
name 'company'
}
}
publications {
"-$thisProject.name-"(MavenPublication) {
from components.java
}
}
}
sourceCompatibility = 1.8 // java 8
targetCompatibility = 1.8
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
}
tasks.each {println(it)}
afterReleaseBuild.dependsOn tasks.publish
when I run gradle tasks --all I get
task ':afterReleaseBuild'
task ':beforeReleaseBuild'
task ':build'
task ':checkCommitNeeded'
task ':checkSnapshotDependencies'
task ':checkUpdateNeeded'
task ':commitNewVersion'
task ':confirmReleaseVersion'
task ':createReleaseTag'
task ':createScmAdapter'
task ':initScmAdapter'
task ':preTagCommit'
task ':release'
task ':runBuildTasks'
task ':unSnapshotVersion'
task ':updateVersion'
"release" works. That is to say it bumps versions, tags, strips the "-SNAPSHOT" off, etc. But, there is a step missing here (and it could totally be my ignorance), but upon stripping the "-SNAPSHOT" from the version I need it to build the artifacts and publish them. This is like the maven release process but without the artifact upload. I am currently using gradle publish and not maven upload.
Caveats: Very new to gradle but not java
Can someone tell me what I am missing?
_
You just can try to remove build task from the relesae plugin configuration.
buildTasks = [] works fine for me.
release {
...
tagTemplate = '${version}'
versionPropertyFile = 'gradle.properties'
buildTasks = []
...
}

How to collect javadoc jars into a zip file

For a multi-project Gradle project, foo, bar and baz. I'm trying to create a task which creates a zip file with both libraries and javadoc, i.e. foo.jar, AND foo-javadoc.jar..
./build.gradle
./settings.gradle
./foo/build.gradle
./bar/build.gradle
./baz/build.gradle
settings.gradle
include ":foo"
include ":bar"
include ":baz"
Top level build
allprojects {
apply plugin: 'java'
task generateJavadoc (type : Javadoc) {
source = sourceSets.main.allJava
classpath = sourceSets.main.compileClasspath
failOnError = false
}
task javadocJar(type: Jar, dependsOn: generateJavadoc) {
classifier = 'javadoc'
from generateJavadoc.destinationDir
}
artifacts {
archives javadocJar
}
}
task buildZip(type: Zip, dependsOn: build) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from (project(':foo').configurations.runtime) {
into 'jars'
}
from (project (':foo').configurations.archives.all) {
into 'jars'
}
}
When I invoke this with gradle clean buildZip a zip file is created, but without the any -javadoc JARs I was expecting... The JavaDoc jars are generated into the project build directories, e.g. foo/build/lib/foo-javadoc.jar I've tried multiple combinations of from project (':foo').artifacts etc.
This is possible using the following. Note javadocJar is the name of the task defined in the allprojects block
from (rootProject.allprojects.javadocJar.outputs) {
into 'javadoc'
}

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.

uploadArchives does not upload artifacts generated from other gradle files

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
}

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.

Resources