How to tie publishing of sources to installing to local Maven repository? - maven

To install sources to local Maven repository, I was taught to write
apply plugin: "maven-publish"
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
and it copies files as required. Now I wish this work automatically along with install task of maven plugin.
I tried several lines like
install.dependsOn(publishing.publishSources)
or
install.dependsOn(publishSources)
but failed with various errors.

You could use the nebula.source-jar plugin. Or you could at least copy/paste the bolierplate from the readme.
Eg: Eliminates this boilerplate:
tasks.create('sourceJar', Jar) {
dependsOn tasks.classes
from sourceSets.main.allSource
classifier 'sources'
extension 'jar'
group 'build'
}
publishing {
publications {
nebula(MavenPublication) { // if maven-publish is applied
artifact tasks.sourceJar
}
nebulaIvy(IvyPublication) { // if ivy-publish is applied
artifact tasks.sourceJar
}
}
}

Related

publish groovy doc with gradle

I have a project with about 50% Java code and 50% Groovy that i try to publish to Sonatype ossrh. Publishing snapshots goes well but the docs jar is missing (both when publishing locally and publishing to Sonatype Nexus). I can create the combined groovy/java docs by defining:
groovydoc {
use = true
groovyClasspath = configurations.compile // http://issues.gradle.org/browse/GRADLE-1391
}
task groovydocJar(type: Jar, dependsOn: groovydoc ) {
classifier 'javadoc' // must use javadoc classifier to be able to deploy to Sonatype
from groovydoc.destinationDir
}
and running ./gradlew groovydocJar produces the intended -javadoc.jar without problems.
My issue is that this docs jar is not included the publish task.
I tried the following
publishing {
publications {
maven(MavenPublication) {
from components.java
artifacts {
archives sourcesJar
archives groovydocJar
}
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
pom {
// omitted for brevity
}
}
}
}
... but e.g. `./gradlew publishToMavenLocal` publishes only the classes jar, the pom, a module and the sources jar.
No sign of a javadoc jar. I thought this was the idea of the artifacts section but maybe something is missing.
How can i tell the publishing task to include publishing of the groovydocs jar?
Gradle version is 6.8.3, jvm version is 1.8 and i depend on `compile 'org.codehaus.groovy:groovy-all:3.0.7'`
The complete build script is available here:
https://github.com/perNyfelt/ProjectDeployer/blob/main/build.gradle
I figured it out:
That artifacts.archives way was not working.
The syntax for adding the groovy doc is like this:
publishing {
publications {
maven(MavenPublication) {
from components.java
artifact(groovydocJar) {
classifier = 'javadoc'
}
// etc...
}
}
}

Can't Publish SubProjects with Gradle

I have a gradle project, it has jvm (Scala) subprojects, and I want to publish all of their jars to a m2 repository.
I applied maven-publish like this ...
allprojects {
apply plugin: 'maven-publish'
publishing {
repositories {
// path to somewhere in my repo
maven { url new File(project.rootProject.getProjectDir().absoluteFile.parentFile, ".m2-repo") }
}
}
}
... but it only publishes the root project. I've tried running publish from the subprojects; I get an up-to-date message but nothing is added to the repo on disk.
I'm using the 4.0.1 GradeWrapper
It needs to be;
apply plugin: 'maven-publish'
publishing {
repositories {
// publish to a "local" repo that I can also consume or upload as I wish
maven { url new File(project.rootProject.getProjectDir().absoluteFile.parentFile, ".m2-repo") }
}
// really, really publish things
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}

Generate local maven repository with all dependencies using Gradle

What I currently have
dependencies {
compile ...
}
task copyDependencies(type:Copy) {
from configurations.compile
into 'build/dependencies/'
}
This task copies all the required dependencies to the build/dependencies/ directory. Inside the directory, it looks as follows:
/dep1-1.0.jar
/dep2-1.0.aar
...
So essentially what is known as flatDir in gradle terms.
What I'd like to have
It's a local maven repository with all these dependencies, instead of a flatDir.
If I understand your question correctly, you just want to publish to a local directory as if it were mavenLocal() but in a location you specify.
In that case, I believe you just need:
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
url "/path/to/wherever"
}
}
}
See the Maven Publish plugin docs

How to publish an XML file to Maven repo from Gradle

I have a static xml file that I want to publish to a Maven repository (my local Maven repo for now) from Gradle using the maven-publish plugin.
build.gradle looks like this:
apply plugin: 'maven-publish'
group 'com.example.gradletest'
version '1.0-SNAPSHOT'
publishing {
publications {
beta(MavenPublication) {
artifactId 'feature-file'
artifact 'src/main/resources/features.xml'
}
}
}
This works, but I would like to add a classifier too, where should I define that?
https://docs.gradle.org/current/userguide/publishing_maven.html says in section 68.2.2 that it should be possible to do:
publishing {
publications {
beta(MavenPublication) {
artifactId 'feature-file'
artifact ('src/main/resources/features.xml') {
classifier 'features'
}
}
}
}
but then I get this error:
A problem occurred configuring root project 'testapplication'.
Exception thrown while executing model rule: org.gradle.api.publish.plugins.PublishingPlugin$Rules#publishing(org.gradle.api.plugins.ExtensionContainer)
No signature of method: java.io.File.call() is applicable for argument types: (build_101arjzoe908rdkh5aikrn6bt$_run_closure5_closure16_closure19_closure20) values: [build_101arjzoe908rdkh5aikrn6bt$_run_closure5_closure16_closure19_closure20#2aa7399c]
Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure), list()
Using the below solution will correctly add the classifier, but will also change the packaging in the pom.xml from <packaging>xml</packaging> to <packaging>pom</packaging>. I don't want that.
publishing {
publications {
beta(MavenPublication) {
artifactId 'feature-file'
artifact source: 'src/main/resources/features.xml', classifier: 'features'
}
}
}
Use the map syntax as specified here.
publishing {
publications {
beta(MavenPublication) {
artifactId 'feature-file'
artifact source: 'src/main/resources/features.xml', classifier: 'features'
}
}
}
Adding pom { packaging 'xml' }
works for me (gradle 3.2.1)
publishing {
publications {
beta(MavenPublication) {
artifactId 'feature-file'
artifact source: 'src/main/resources/features.xml', classifier: 'features'
pom { packaging 'xml' }
}
}
}

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