Publish source jar when reading external .gradle file - gradle

I have a git submodule (of which I have no control of) in my project which contains file named publish.gradle. The contents of this file are:
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
version = System.getenv()['VERSION'] ?: version
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
artifactory {
contextUrl = repoBaseUrl
publish {
String publishRepoKey = version.endsWith('SNAPSHOT') ? repoKeySnapshot : repoKeyRelease
repository {
repoKey = publishRepoKey // The Artifactory repository key to publish to
username = System.getenv()['ARTIFACTORY_USERNAME'] ?: artifactoryUser // The publisher user name
password = System.getenv()['ARTIFACTORY_PASSWORD'] ?: artifactoryPassword // The publisher password
}
defaults {
publications('mavenJava')
publishArtifacts = true
publishPom = true
}
}
resolve {
repoKey = repoKey
}
}
In my build.gradle file I have the following line:
apply from: "${rootDir}/submodule/gradle/publish.gradle"
When I try to add the following line after this line:
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing.publications.mavenJava.artifact sourceJar {
classifier 'sources'
}
Then the correct things get published, except that the pom file in the repository only contains a <dependencyManagement/> section and all the dependencies are missing. When I remove the above lines from build.gradle the pom is correct.

Try to override the all publishing block with :
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}

Related

maven publish artifacts to local repository fail

I would like to test my publish task locally first before deploy it to maven server. I wish it can be deploy to local maven repository as below but it doesn't work
apply plugin: 'maven-publish'
publishing {
repositories {
maven {
name 'release'
url "/home/user/.m2/repository"
credentials {
username = ""
password = ""
}
}
}
publications {
deployApp(MavenPublication) {
groupId "com.sample"
artifactId "demo"
version "1.0.0"
artifact ("demo.apk")
}
}
}
What's the proper way to verify publish task locally?
to locally publish you just need
publishing {
publications {
mavenJava(MavenPublication) {
artifact bootJar //replace with your artefact
}
}
}
it defaults to /Users/username/.m2/repository
remove your repositories section

How do I suppress POM and IVY related warnings in Gradle 7?

After upgrading to Gradle 7 I have many warnings like:
Cannot publish Ivy descriptor if ivyDescriptor not set in task ':myProject:artifactoryPublish' and task 'uploadArchives' does not exist.
Cannot publish pom for project ':myProject' since it does not contain the Maven plugin install task and task ':myProject:artifactoryPublish' does not specify a custom pom path.
The artifactoryPublish task works fine.
My Gradle script:
buildscript {
repositories{
maven {
url = '...'
credentials {
username '...'
password '...'
}
metadataSources {
mavenPom()
artifact()
}
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.24.12"
}
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
suppressAllPomMetadataWarnings()
}
}
}
group = '...'
artifactory {
contextUrl = '...'
publish {
repository {
repoKey = '...'
username = '...'
password = '...'
}
defaults {
publishConfigs('archives')
publishIvy = false
publications("mavenJava")
}
}
}
How do I disable those warnings?
It looks like you mixed between the old Gradle publish-configuration method and the new Gradle publication method.
You applied the maven-publish plugin which allows creating publications. In artifactory.default, you added the "mavenJava" publication as expected.
However, the archives publish-configuration doesn't exist in your build.gradle file. Basically, publish-configurations are created by the legacy maven plugin. The configured mavenJava publication does the same as the archives publish-configuration and therefore all of the JARs are published as expected.
To remove the warning messages you see, remove the publishConfigs('archives') from artifactory.default clause:
artifactory {
publish {
defaults {
publishConfigs('archives') // <-- Remove this line
publishIvy = false
publications("mavenJava")
}
}
}
Read more:
Gradle Artifactory plugin documentation
Example

Gradle will not publish artifact?

Gradle v4.10.2
I’m building a Gradle Java plugin, and it builds. However when I run ./gradlew publish it does nothing, i.e., the artifact doesn’t get published. Here’s my build.gradle file (I have all the variables defined in my gradle.properties file). Also, if I just run ./gradlew publish w/o running ./gradlew build first, it doesn’t run the build phase. What am I missing in my build.gradle file? Thanks.
plugins {
id 'java'
id 'maven'
id 'maven-publish'
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
group=project.groupId
version = '0.0.1'
jar {
manifest {
attributes 'artifactId': project.artifactId,
'groupId': project.groupId,
'version': project.version
}
baseName artifactId
doLast {
println "artifactId: $project.artifactId\ngroupId: $project.groupId\nversion: $version"
}
}
dependencies {
compile gradleApi()
}
// For publishigh to S3 maven repo
publishing {
repositories {
maven {
url "s3://" + s3_bucket
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}
RTFM. I was missing the publications block inside the publishing block. Here’s the whole block
publishing {
publications {
myLibrary(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "s3://" + s3_bucket
credentials(AwsCredentials) {
accessKey AWS_ACCESS_KEY
secretKey AWS_SECRET_KEY
}
}
}
}

Publish Java artifact to Maven Local with Gradle

I am facing a problem when trying to install a generated jar into my local Maven Repository. The message error just show me 'task 'publish' is not found'
I am using this Gradle Script:
buildscript {
ext {
springBootVersion = '1.3.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'maven-publish'
jar {
baseName = 'mongofoundry'
version = '1.0.0'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-data-mongodb')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
Do you have some idea Why I am reading that error message?
Thanks.
UPDATED
Running the command as #RaGe mentioned, solved the problem:
gradle publishToMavenLocal
The correct task to publish artifacts to local maven is
gradle publishToMavenLocal
Check Maven locally
For developing and testing it is useful to check library locally
gradle settings for apply plugin: 'com.android.library' not apply plugin: 'java-library'(where you can use it by default)
apply plugin: 'maven-publish'
//simple settings
project.afterEvaluate {
publishing {
publications {
library(MavenPublication) {
//setGroupId groupId
setGroupId "com.company"
//setArtifactId artifactId
setArtifactId "HelloWorld"
version "1.1"
artifact bundleDebugAar
/* add a dependency into generated .pom file
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', 'com.company')
dependencyNode.appendNode('artifactId', 'HelloWorld-core')
dependencyNode.appendNode('version', '1.1')
}
*/
}
}
}
}
to run it using command line or find this command in Gradle tab
./gradlew publishToMavenLocal
Location
artefact will be added into .m2 folder
//Unix
~/.m2
//Windows
C:\Users\<username>\.m2
//For example
/Users/alex/.m2/repository/<library_path>/<version>/<name>.<extension>
build folder
<project_path>/build/outputs/<extension>
other repositories location
~/.gradle/caches/modules-2/files-2.1/<group_id>/<artifact_id>/<version>/<id>
//For example
/Users/alex/.gradle/caches/modules-2/files-2.1/com.company/HelloWorld/1.1/c84ac8bc425dcae087c8abbc9ecdc27fafbb664a
To use it add mavenLocal(). It is important to place it as a first item for prioritise it, which is useful for internal dependencies
buildscript {
repositories {
mavenLocal()
}
allprojects {
repositories {
mavenLocal()
}
}
and
dependencies {
implementation 'com.company:HelloWorld:+'
}
*Also remember if you use a kind of shared.gradle file (via apply from) you should set path which is relevant to project.gradle (not shared.gradle)
[iOS CocoaPod local]
This is how I did it with Kotlin DSL (build.gradle.kts) for my Android library:
plugins {
id("maven-publish")
// OR simply
// `maven-publish`
// ...
}
publishing {
repositories {
// Local repository which we can first publish in it to check artifacts
maven {
name = "LocalTestRepo"
url = uri("file://${buildDir}/local-repository")
}
}
publications {
// ...
}
}
You can create all the publications with the following command:
./gradlew publishAllPublicationsToLocalTestRepoRepository
Or just a single publication with this command:
./gradlew publishReleasePublicationToLocalTestRepoRepository
See Gradle documentations: Maven Publish Plugin for more information.
Add maven plugin to your project and then:
gradle clean install
Here is an alternative skeleton for Gradle 7.5.1 with Java 17
build.gradle
plugins {
id 'org.gradle.java'
id 'org.gradle.maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
withJavadocJar()
withSourcesJar()
}
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'your-group'
artifactId = 'your-artifact'
version = "0.0.1"
from components.java
}
}
repositories {
mavenLocal()
}
}
Publishing
You can see more details on the publishing steps with --info
./gradlew --info publishToMavenLocal
Output Directory
Linux/macOS
/Users/<username>/.m2/repository/your-group/your-artifact/0.0.1
Windows
C:\Users\<username>\.m2\repository\your-group\your-artifact\0.0.1

Publishing both jar and sources jar to Artifactory from Gradle

Here is my build.gradle:
buildscript {
repositories {
maven {
url 'http://localhost:8081/artifactory/plugins-release'
credentials {
username = "admin"
password = "password"
}
name = "maven-main-cache"
}
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'codenarc'
apply plugin: 'maven-publish'
apply plugin: "com.jfrog.artifactory"
version="0.0.2"
group = "mylib"
repositories {
mavenCentral()
add buildscript.repositories.getByName("maven-main-cache")
maven {
url "http://localhost:8081/artifactory/myapp-snapshots"
}
}
dependencies {
compile 'commons-validator:commons-validator:1.4.0'
testCompile 'junit:junit:4.11'
}
artifactory {
contextUrl = "http://localhost:8081/artifactory"
publish {
repository {
repoKey = 'myorg-snapshots'
username = "admin"
password = "password"
maven = true
}
defaults {
publications ('mavenJava')
}
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
task dist(type: Zip, dependsOn: build) {
classifier = 'buildreport'
from('build/test-results') {
include '*.xml'
into 'tests'
}
from('build/reports/codenarc') {
into 'reports'
}
from('build/docs') {
into 'api'
}
from(sourcesJar) {
into 'source'
}
from('build/libs') {
exclude '*-sources.jar'
into 'bin'
}
}
Based on this current setup:
To build my JAR I have to run gradle clean build groovydoc sourcesJar dist and then
To publish to Artifactory, I have to run a second command of gradle artifactoryPublish
Two things I'm looking to change here:
gradle artifactoryPublish only publishes my built JAR and a dynamically-created POM to Artifactory. I'd like it to also publish the sources JAR that my build is creating. How?; and
Ideally I'd like to be able to do all of the above by just invoking gradle publish instead of having to run the 2 commands sequentially. Is this possible? If so, how?
When it comes to publishing source you need to modify your script in the following way:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact (sourcesJar) {
classifier = 'sources'
}
}
}
}
When it comes to a single command what you need to do is to define dependencies between tasks. Unfortunately I can't try the script so it may be redundant but should do the job:
artifactoryPublish.dependsOn('clean', 'build', 'groovydoc', 'sourcesJar', 'dist')
publish.dependsOn(artifactoryPublish)

Resources