I have the following build.gradle and it works for the individual sub project (or all of them if I copy paste it):
def group = 'com.my.pgk'
def artifact = project.name
def version = '1.0.0'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
uploadArchives {
repositories {
mavenDeployer {
def finalPath = 'file://' + LocalMavenRepoPath.toString()
println group
println finalPath
repository url: finalPath
pom.groupId = group
pom.artifactId = artifact
pom.version = version
pom.packaging = 'jar'
}
}
}
jar {
archiveName = artifact + "-" + version + ".jar"
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
archiveName = artifact + "-v" + version + "-src.jar"
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
archiveName = artifact + "-v" + version + "-doc.jar"
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
It is doing exactly what I want it to...
Trying to move this to the root project build.gradle doesn't because the variables defined in the subprojects are not updated when uploadArchives is called.
How can I work around this?
You need the subprojects to be configured first so that gradle is aware of the subproject variables before the root build.gradle is configured. You can force a bottom-up configuration by using:
evaluationDependsOnChildren()
In a project with a single root and two subprojects, you can create a common gradle script in the root project and apply it to each of the subprojects.
Root Project:
build.gradle
//nothing to see here
Root Project: common.gradle
if (!project.hasProperty("commonVar")) {
ext.commonVar = "unset"
}
task printCommonVar() {
println commonVar
}
Root Project: settings.gradle
rootProject.name = 'GradleGroupProject'
// Find the directories containing a "build.gradle" file in the root directory
// of the project. That is, every directory containing a "build.gradle" will
// be automatically the subproject of this project.
def subDirs = rootDir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (!file.isDirectory()) {
return false
}
if (file.name == 'buildSrc') {
return false
}
return new File(file, 'build.gradle').isFile()
}
});
subDirs.each { File dir ->
include dir.name
}
Subproject 1: build.gradle
ext.commonVar = "subproject1"
apply from: rootProject.file('common.gradle')
Subproject 2: build.gradle
ext.commonVar = "subproject2"
apply from: rootProject.file('common.gradle')
The ordering between ext.commonVar = "subproject2" and apply from: rootProject.file('common.gradle') is important.
Related
import groovy.json.*
subprojects {
afterEvaluate {
if (eventsJsonProjectsName.contains(project.name)) {
def Projectgroup = ''
for (i in group)
if (i == '.') Projectgroup += '/'
else Projectgroup += i
File file = new File(projectDir, "${project.name}.json")
file.createNewFile()
def data = JsonOutput.toJson(
ApplicationName: project.name,
ApplicationVersion: project.version,
Modality: "IGT",
ArtifactoryPath: artifactory_publish_contextUrl + '/' +
artifactory_publish_repoKey + '/' + Projectgroup + '/' +
project.name + '-' + project.version + '/' +
project.name + '-' + project.version + '.jar'
)
file.text = data
}
}
}
enter image description here
I actually working on multiple projects and each sub project have its own build and this code actually creates json for each builds and store as a .json file.
And I actually these files to Jfrog for each project
afterEvaluate {
subprojects {
if (eventsJsonProjectsName.contains(project.name)) {
repositories {
maven {
url "${artifactory_publish_contextUrl}/ASP/CI-CD-Test"
}
}
apply plugin: "com.jfrog.artifactory"
apply plugin: "java"
apply plugin: "maven-publish"
dependencies {
testImplementation "junit:junit:4.7"
}
publishing {
publications {
mavenJava(MavenPublication) {
// from components.java
artifact file("${project.name}.json")
}
}
}
artifactory {
contextUrl = "${artifactory_publish_contextUrl}
publish {
repository {
repoKey = "${artifactory_publish_repoKey}"
username = "${artifactory_publish_user}"
password = "${artifactory_publish_password}"
}
}
}
}
}
}
enter image description here
I actually use this block of code for publishing it to Jfrog. But it is not reflecting in Jfrog. I want to know what is wrong with this.
I am stuck with it for a long time now.please help me with this.
I want upload the JSON file to artifactory each time build runs
Given the following three projects (foo, bar, and app), where app depends on bar depends on foo, I'd like to be able to simply install bar in project app and have library foo automatically installed as well. This does work correctly with the following scripts:
Project 'foo' build.gradle
plugins {
id "maven-publish"
}
repositories {
mavenLocal()
}
group = "com.test2.foo"
version = "0.0.1"
def distZip = file("${buildDir}/dist.zip")
def distFile = file("${buildDir}/Foo.txt")
tasks.register("createDistFile") {
outputs.file(distFile)
doLast {
distFile.delete()
distFile.parentFile.mkdirs()
distFile.text = "foo output 1"
}
}
tasks.register("assembleZip", Zip) {
dependsOn "createDistFile"
from distFile
archiveName = distZip.name
destinationDir distZip.parentFile
}
publishing {
publications {
main(MavenPublication) {
artifact(distZip) {
builtBy tasks.named("assembleZip")
}
}
}
repositories {
mavenLocal()
}
}
Project 'bar' build.gradle
plugins {
id "maven-publish"
id "base"
}
repositories {
mavenLocal()
}
group = "com.test2.bar"
version = "0.0.1"
def distZip = file("${buildDir}/dist.zip")
def distFile = file("${buildDir}/Bar.txt")
tasks.register("createDistFile") {
outputs.file(distFile)
doLast {
distFile.delete()
distFile.parentFile.mkdirs()
distFile.text = "bar output 2"
}
}
tasks.register("assembleZip", Zip) {
dependsOn "createDistFile"
from distFile
archiveName = distZip.name
destinationDir distZip.parentFile
}
artifacts {
"default" distZip, {
builtBy tasks.named("assembleZip")
}
}
publishing {
publications {
main(MavenPublication) {
artifact(distZip) {
builtBy tasks.named("assembleZip")
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', "com.test2.foo")
dependencyNode.appendNode('artifactId', "foo")
dependencyNode.appendNode('version', "0.0.1")
}
}
}
}
repositories {
mavenLocal()
}
}
Project 'app' build.gradle
repositories {
mavenLocal()
}
configurations {
api
}
dependencies {
api "com.test2.bar:bar:0.0.1"
// This should be automatically installed when bar is installed
// api "com.test2.foo:foo:0.0.1"
}
tasks.register("installLibs", Copy) {
dependsOn configurations.api
destinationDir = file("${buildDir}/libs")
configurations.api.each { config ->
from zipTree(config)
}
doFirst {
destinationDir.deleteDir()
}
}
The Problem
When using the above scripts, this can be verified by running installLibs in project app and verifying that both Bar.txt and Foo.txt are installed into the build/libs directory.
However, if I change the settings.gradle file in project 'app' to be this:
rootProject.name = 'app'
includeBuild '[PATH TO BAR PROJECT]'
Then if I run ./gradle installLibs again for the app project, I find that Foo.txt is no longer installed. Is there a way to manually specify this build metadata for project bar, so then when it is used with includeBuild that its transtive dependencies get included?
What could be missing in this build.gradle file that prevents the publishToMavenLocal not to copy files to the .m2 folder?
The script does have the 'maven-publish' plugin.
buildscript {
ext.kotlinVersion = '1.3.10'
ext.dokkaVersion = '0.9.17'
repositories { mavenLocal() }
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokkaVersion"
}
}
plugins {
id 'com.github.hierynomus.license' version '0.15.0'
id 'io.codearte.nexus-staging' version '0.11.0'
id 'maven-publish'
}
group = 'org.jetbrains.xodus'
version = hasProperty('xodusVersion') ? project.xodusVersion : ''
def isSnapshot = version.endsWith('SNAPSHOT')
def isDailyBuild = hasProperty('dailyBuild') ? project.dailyBuild : false
def mavenPublishUrl = hasProperty('mavenPublishUrl') ? project.mavenPublishUrl : ''
def mavenPublishUsername = hasProperty('mavenPublishUsername') ? project.mavenPublishUsername : ''
def mavenPublishPassword = hasProperty('mavenPublishPassword') ? project.mavenPublishPassword : ''
def signingKeyId = hasProperty('signingKeyId') ? project.signingKeyId : ''
def signingPassword = hasProperty('signingPassword') ? project.signingPassword : ''
def signingSecretKeyRingFile = hasProperty('signingSecretKeyRingFile') ? project.signingSecretKeyRingFile : '../key.gpg'
static def shouldDeploy(project) {
return project.version.length() > 0 && !(project.name in ['benchmarks', 'samples'])
}
task wrapper(type: Wrapper) {
gradleVersion = '3.5.1'
}
defaultTasks 'assemble'
// Use nexus-staging-plugin to workaround https://issues.sonatype.org/browse/OSSRH-5454
nexusStaging {
username = mavenPublishUsername
password = mavenPublishPassword
delayBetweenRetriesInMillis = 30000
stagingProfileId = "89ee7caa6631c4"
}
subprojects {
apply plugin: 'license'
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'org.jetbrains.dokka'
sourceCompatibility = 1.7
compileJava.options.encoding = 'UTF-8'
group = rootProject.group
version = rootProject.version
archivesBaseName = rootProject.name + '-' + project.name
license {
header rootProject.file('license/copyright.ftl')
strictCheck true
ext.inceptionYear = 2010
ext.year = Calendar.getInstance().get(Calendar.YEAR)
ext.owner = 'JetBrains s.r.o.'
include "**/*.kt"
include "**/*.java"
mapping {
kt = 'JAVADOC_STYLE'
}
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
}
// tests for most of sub-projects run with database encryption turned on
if (!(project.name in ['benchmarks', 'compress', 'crypto', 'openAPI', 'samples', 'utils'])) {
test {
systemProperty 'exodus.cipherId', 'jetbrains.exodus.crypto.streamciphers.ChaChaStreamCipherProvider'
systemProperty 'exodus.cipherKey', '000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f'
systemProperty 'exodus.cipherBasicIV', '314159262718281828'
// uncomment the following line to run tests in-memory
//systemProperty 'exodus.log.readerWriterProvider', 'jetbrains.exodus.io.inMemory.MemoryDataReaderWriterProvider'
}
dependencies {
testCompile project(':crypto')
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
jar {
manifest {
attributes 'Implementation-Title': archivesBaseName, 'Implementation-Version': version
}
}
test {
minHeapSize = '1g'
maxHeapSize = '1g'
//jvmArgs = ['-ea', '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=2808']
//testLogging.showStandardStreams = true
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
duplicatesStrategy 'exclude'
includeEmptyDirs false
from javadoc.destinationDir
}
javadoc.failOnError = false
// work around for Java 8 javadoc which is too strict
if (JavaVersion.current().isJava8Compatible()) {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
task sourceJar(type: Jar) {
classifier = 'sources'
duplicatesStrategy 'exclude'
includeEmptyDirs false
from project.sourceSets.main.java
from project.sourceSets.main.kotlin
}
// configuring projects with Kotlin sources
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
compile 'io.github.microutils:kotlin-logging:1.5.4'
}
compileKotlin {
kotlinOptions {
languageVersion = '1.2'
apiVersion = '1.2'
}
}
compileTestKotlin {
kotlinOptions {
languageVersion = '1.2'
apiVersion = '1.2'
}
}
dokka {
jdkVersion = 7
packageOptions {
reportUndocumented = false
}
}
task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) {
outputFormat = 'javadoc'
outputDirectory = "$buildDir/javadoc"
}
javadocJar {
dependsOn dokkaJavadoc
from dokkaJavadoc.outputDirectory
}
artifacts {
archives jar, javadocJar, sourceJar
}
if (!isSnapshot && signingKeyId.length() > 0) {
ext.'signing.keyId' = signingKeyId
ext.'signing.password' = signingPassword
ext.'signing.secretKeyRingFile' = signingSecretKeyRingFile
}
afterEvaluate { project ->
if (shouldDeploy(project)) {
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
if (isDailyBuild) {
repository(url: "https://api.bintray.com/maven/jetbrains/xodus/" + archivesBaseName + "/;publish=1") {
authentication(userName: mavenPublishUsername, password: mavenPublishPassword)
}
} else {
repository(url: mavenPublishUrl) {
authentication(userName: mavenPublishUsername, password: mavenPublishPassword)
}
}
pom.project {
name 'Xodus'
description 'Xodus is pure Java transactional schema-less embedded database'
packaging 'jar'
url 'https://github.com/JetBrains/xodus'
scm {
url 'https://github.com/JetBrains/xodus'
connection 'scm:git:https://github.com/JetBrains/xodus.git'
developerConnection 'scm:git:https://github.com/JetBrains/xodus.git'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/license/LICENSE-2.0.txt'
distribution 'repo'
}
}
developers {
developer {
id 'JetBrains'
name 'JetBrains Team'
organization 'JetBrains s.r.o'
organizationUrl 'http://www.jetbrains.com'
}
}
}
}
}
}
signing {
required { !isSnapshot && signingKeyId.length() > 0 && gradle.taskGraph.hasTask('uploadArchives') }
sign configurations.archives
}
}
}
}
Your build does not configure any publications, hence the publishToMavenLocal task is a noop:
$ ./gradlew --console=verbose publishToMavenLocal
> Task :publishToMavenLocal UP-TO-DATE
BUILD SUCCESSFUL in 0s
When you configure a publication as in this example …
task buildMyArtifact(type: Zip) {
// TODO using the build script as a sample file here; use whatever makes
// sense for your project
from 'build.gradle'
archiveBaseName = 'whateverNameInBuildDir'
destinationDirectory = buildDir
}
publishing {
publications {
myPublication(MavenPublication) {
artifact buildMyArtifact
groupId 'com.example'
artifactId 'my-module'
version '1.0.0'
}
}
}
… then the publishToMavenLocal task installs the publication into the local Maven repository:
$ ./gradlew --console=verbose publishToMavenLocal
> Task :buildMyArtifact
> Task :generatePomFileForMyPublicationPublication
> Task :publishMyPublicationPublicationToMavenLocal
> Task :publishToMavenLocal
BUILD SUCCESSFUL in 1s
3 actionable tasks: 3 executed
$ tree ~/.m2/repository/com/example/my-module/
/home/chriki/.m2/repository/com/example/my-module/
├── 1.0.0
│ ├── my-module-1.0.0.pom
│ └── my-module-1.0.0.zip
└── maven-metadata-local.xml
1 directory, 3 files
I have previously successfully set up bintray and artifactory accounts to publish snapshot versions to the OSS JFrog Artifactory repository, but after setting up a GitHub/Bintray/Artifactory organisation under the same user, I am unable to publish snapshots.
When attempting to run
./gradlew artifactoryPublish -Dsnapshot=true -DbintrayUser=myBintrayUser -DbintrayKey=myBintrayApiKey -DbuildNumber=#
I get the following error:
java.io.IOException: Failed to deploy file. Status code: 401 Response message: Artifactory returned the following errors:
Unauthorized Status code: 401
I've tried using both bintray users (my personal and the organisation) but get the same response. I've also tried regenerating a new API key at https://bintray.com/profile/edit, but has not worked (and now also seems to be out of sync with the key at https://oss.jfrog.org/artifactory/webapp/#/profile) which I can't edit.
The build.gradle file is:
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
}
plugins {
id 'java-library'
id 'maven'
id 'maven-publish'
// Automatic SEMVER
// ./gradlew release
id 'net.vivin.gradle-semantic-build-versioning' version '4.0.0' apply false
// SNAPSHOT publishing to oss-jfrog-artifactory
// ./gradlew artifactoryPublish -Dsnapshot=true -DbintrayUser=<YOUR_USER_NAME> -DbintrayKey=<YOUR_API_KEY> -DbuildNumber=NNN
id 'com.jfrog.artifactory' version '4.6.2'
// RELEASE publishing to bintray
// ./gradlew bintrayUpload -DbintrayUser=<YOUR_USER_NAME> -DbintrayKey=<YOUR_API_KEY>
id 'com.jfrog.bintray' version '1.8.1'
}
wrapper.gradleVersion = '4.5.1'
def groupName = 'noxtech'
group = 'uk.co.noxtech'
archivesBaseName = 'noxtech-java-utils'
description = 'Assorted Java 8 utilities'
def projectUrl = "https://github.com/noxtech/noxtech-java-utils"
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
api 'joda-time:joda-time:2.9.9'
implementation 'org.projectlombok:lombok:1.16.20'
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest-all:1.3'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
javadoc.failOnError = false
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
def pomConfig = {
licenses {
license {
name "The Apache Software License, Version 2.0"
url "http://www.apache.org/licenses/LICENSE-2.0.txt"
distribution "repo"
}
}
scm {
url projectUrl
}
}
publishing {
publications {
mavenPublication(MavenPublication) {
from components.java
artifact sourcesJar {
classifier "sources"
}
artifact javadocJar {
classifier "javadoc"
}
groupId = project.group
artifactId = project.archivesBaseName
version = project.version.toString()
pom.withXml {
def root = asNode()
root.appendNode('description', project.description)
root.appendNode('name', project.name)
root.appendNode('url', projectUrl)
root.children().last() + pomConfig
}
}
}
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url "$buildDir/repo"
}
}
}
bintray {
user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
key = project.hasProperty('bintrayKey') ? project.property('bintrayKey') : System.getenv('BINTRAY_KEY')
publications = ['mavenPublication']
pkg {
repo = "maven"
name = project.archivesBaseName
userOrg = groupName
licenses = ['Apache-2.0']
websiteUrl = projectUrl
vcsUrl = projectUrl + '.git'
issueTrackerUrl = projectUrl + '/issues'
version {
name = project.version.toString()
desc = project.description
vcsTag = project.version.toString()
released = new Date()
}
}
}
artifactory {
contextUrl = 'http://oss.jfrog.org'
publish {
repository {
repoKey = 'oss-snapshot-local'
username = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
password = project.hasProperty('bintrayKey') ? project.property('bintrayKey') : System.getenv('BINTRAY_KEY')
}
defaults {
publications('mavenPublication')
publishArtifacts = true
publishPom = true
}
}
resolve {
repoKey = 'jcenter'
}
clientConfig.info.setBuildNumber(project.hasProperty('buildNumber') ? project.property('buildNumber') : System.getenv('BUILD_NUMBER'))
}
This turned out to be a simple solution. When moving over to use the organisation from the personal account on CircleCI, the environment variables were lost.
I have a gradle multi project build with the following layout:
Root
+---ProjectA
+---ProjectB
+---ProjectC
+ ...
The jars which will be produced by the subprojects need to be prefixed. e.g. PREFIX-ProjectA.jar. That's working.
How can I tell maven-publish task that it searches for "PREFIX-ProjectA.jar"? Currently it searches for "ProjectA.jar"
My publishing section looks like this:
task sourceJar(type: Jar) {
baseName 'PREFIX-' + project.name
from sourceSets.main.allJava
}
publishing {
publications {
mavenJava(MavenPublication) {
artifactId 'PREFIX-' + project.name
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
In case somebody runs in the same issue here's my solution:
/**
* Creates a jar containing all sources
*/
task jarSources(type: Jar) {
baseName = 'PREFIX-' + project.name
from sourceSets.main.allJava
classifier = 'sources'
}
/**
* Creates the jar file
*/
task jarExecutable(type: Jar) {
baseName = 'PREFIX-' + project.name
from sourceSets.main.output
}
publishing {
publications {
mavenJava(MavenPublication) {
artifactId 'PREFIX-' + project.name
artifact jarExecutable
artifact jarSources
//....
}
}
}