Can't Publish SubProjects with Gradle - 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
}
}
}

Related

Move publishing configuration to one place in gradle

I have a project which has modules, my goal is to configure publishing after each module is built and after all modules are built, so I could create a zip file with all the jars inside and upload it as well. I do it in subprojects section and in outer section.
publishing {
publications {
mavenJava(MavenPublication) {
//my artifacts here
}
}
repositories {
maven {
url "${artifactoryURL}"
credentials {
username = "${artifactoryUsername}"
password = "${artifactoryPassword}"
}
}
}
}
Is there a way to move repositories configuration to one place, so I could avoid duplication of this configuration?
I guess that you're creating your deployables in a top-level (root, parent) project, that does not have any sources.
Is there a way to move repositories configuration to one place, so I could avoid duplication of this configuration?
Sure. Just use subprojects, allprojects or generic configure, depending on your needs:
allprojects {
id 'maven-publish'
publishing {
repositories {
maven {
url "http://maven.repo"
}
}
}
}
This will configure publishing for all the projects (be aware of that you may not want to publish everything).
For a projects with Java source you can configure publishing like usual:
subprojects {
publishing {
publications {
main(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
}
}
}
}
And for root project just configure deployments as in your previous question.

How to prioritize mavenLocal over artifactory repo in gradle?

I have multi-modules project and I'm using artifactory for resolving custom libraries:
build.gradle of parent project:
...
subporjects {
...
apply plugin: "com.jfrog.artifactory"
artifactory {
resolve {
contextUrl = ext.getProperty('ARTIFACTORY_URL')
repoKey = ext.getProperty('ARTIFACTORY_REPO_NAME')
username = ext.getProperty('ARTIFACTORY_USERNAME')
password = ext.getProperty('ARTIFACTORY_PASSWORD')
}
}
}
It works as expected my library is published to artifactory with gradle artifactoryPublish and then it's fetched from there. But in some cases I want to fetch my custom library from mavenLocal() repo. I have next subproject build.gradle
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile 'my-custom-library'
}
But as I can see it is still resolves from artifactory. Can I somehow prioritize mavenLocal() over it ?
The repository priority will be the order in which they were added to the RepositoryHandler
I'm guessing that the artifactory repository is added when the plugin is applied so you could delay this by
afterEvaluate {
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}
}
Or maybe
evaluationDependsOnChildren()
subprojects {
apply plugin: "com.jfrog.artifactory"
// etc
}
If all you want to do is to depend on one subproject from within another, you should declare dependencies using the project notation:
dependencies {
compile project(':shared')
}
https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_jar_dependencies

Artifact is not published using gradle and artifactory plugin

I'm trying to upload a simple zip to artifactory using gradle. This is my script:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
def user = "$System.env.ARTIFACTORY_USER"
def pass = "$System.env.ARTIFACTORY_PASSWORD"
task createArchive(type: Zip){
baseName 'myzip'
destinationDir(new File('target'))
from '.'
exclude 'target'
exclude '.gradle'
exclude 'build.gradle'
exclude 'README.md'
doFirst{
new File('target').mkdirs()
}
}
task clean(type: Delete){
delete 'target'
}
publishing {
publications {
customArtifact(MavenPublication) {
artifact createArchive
}
}
}
artifactory {
publish {
contextUrl = 'http://myrepo/artifactory'
repository {
repoKey = "myrepo-tools"
username = user
password = pass
}
publications ('customArtifact')
}
}
The code seems to work fine but actually only the build info are published. The zip is not:
Deploying build descriptor to: http://myrepo/artifactory/api/build
Build successfully deployed. Browse it in Artifactory under http://myrepo/artifactory/webapp/builds/myproject/1503338121156
My code does not seem different from the one published here: How to build Groovy JAR w/ Gradle and publish it to in-house repo (which was pointed as reference in question nothing published to artifactory using gradle artifactory plugin) but I'm not sure that is still valid since is quite old.
The main difference is that almost all the examples are using java plugin so they have the from components.java line in the publications. In my case I cannot use it as I have a custom zip.. but I can't find what else can be changed.
Thanks,
Michele.

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

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
}
}
}

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

Resources