Use gradle to upload jar to local Maven repository - maven

This question has been asked several times, but somehow I don't get this to work. Gradle is a great tool, but its documentation is anything but great. No examples make it almost impossible to understand for someone who doesn't use it on a daily basis.
I am using Android Studio and I want to upload my module output jar to my local Maven repository.
apply plugin: 'java'
dependencies {
compile 'com.google.http-client:google-http-client-android:1.18.0-rc'
}
apply plugin: 'maven'
configure(install.repositories.mavenInstaller) {
pom.project {
groupId 'com.example'
artifactId 'example'
packaging 'jar'
}
}
When I start a build in Android Studio, I can see on the Gradle tab that
:install
is invoked. I also get a new jar in my build folder, but that jar is not uploaded to Maven. [The Maven repo exists and the Google appengine gradle plugin uploads its jar from another module of the same project just fine.]
What am I missing?

I suspect the problem is that you are only editing the POM (via pom.project), instead of configuring the actual Maven coordinates used for installation. Try the following instead:
// best way to set group ID
group = 'com.example'
install {
repositories.mavenInstaller {
// only necessary if artifact ID diverges from project name
// the latter defaults to project directory name and can be
// configured in settings.gradle
pom.artifactId = 'myName'
// shouldn't be needed as this is the default anyway
pom.packaging = 'jar'
}
}
PS: The samples directory in the full Gradle distribution contains many example builds, also for the maven plugin.

Peter N is CORRECT in the comments of the accepted answer, which works, but shouldn't be the accepted answer.
You should have this in your build.gradle file
apply plugin: "maven"
Then you can just do a
$ ./gradlew install
Or use the built-in task
$ ./gradlew publishToMavenLocal
Both methods install the artifacts in $HOME/.m2/com/example/example/version

Related

Get full artifact name generated by gradle

I'm publishing an artifact using maven-publish.
When I'm pushing a release artifact, the resulting artifact version is the same as the project version itself. For example if gradle.properties has version=1.2.3, the artifact would be something like foo-1.2.3.zip.
When I run a SNAPSHOT publish, the resulting artifact will include additional information in the version. For example version=1.2.4-SNAPSHOT gives foo-1.2.4-20220427.094127-1.zip. The additional information would appear to be the time and date to avoid clashes, I assume.
Is there anyway I can access this full artifact name in my gradle scripts?
If you don't specify the artifact names explicitly like this for example
publishing {
publications {
maven(MavenPublication) {
groupId = 'org.gradle.sample'
artifactId = 'library'
version = '1.1'
from components.java
}
}
}
Then gradle will use the defaults that are taken from your build.gradle, gradle.properties and the module directory names.
I'm not sure if there's a way to configure the mavenPublish plugin to print out the maven artifact upon publishing, but you can look in your ~/.m2 directory to see what exactly has been published to your local if you run ./gradlew publishToMavenLocal
I'm also thinking you could create a custom task to print out the group, artifact and version, something like this in the build.gradle of your project you are publishing:
project.task('getMavenCoordinates') {
doLast {
println "Maven Artifact: ${project.group}:${project.name}:${project.version}"
}
}
project.task("publishToMavenLocal").dependsOn("getMavenCoordinates")
If you wanted to configure the task to run after a different maven-pubish task, just change it accordingly! But something like that could help achieve what you want.

Publish reusable gradle tasks

I would like to publish some common parts of build.gradle file to be reusable in different projects (using apply from: url_to_file construction). To achieve this I've created a project called gradle-common that contains those common build files, with this build.gradle file:
group 'org.example'
version '0.1.0'
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
artifact source: file('files/first.gradle'), classifier: 'first'
}
mavenJava(MavenPublication) {
artifact source: file('files/second.gradle'), classifier: 'second'
}
}
repositories {
mavenLocal()
}
}
Files after publishing in maven repository there are files like:
gradle-common-0.1.0-first.gradle
gradle-common-0.1.0-second.gradle
And my question is: how can I remove version number from published artifacts and the classfier? For me ideal files would be:
first.gradle
second.gradle
There are many different answers to your question, but I think you are trying to create something that a plugin usually does without creating a plugin.
The best way to add functionality to multiple gradle projects is to create a plugin. You can also leverage Rules which this simple tutorial doesn't show, but you can inspect some of the gradle native plugins, such as maven-publish.
To answer your question, it is not possible to publish an artifact to a maven repository without a version. You have to download it with a version (you can use my-artifact:1+ to download the latest) and then strip the version yourself.
I am also wondering how are you planning to include these files to your specific gradle files. You won't be able to use them as dependencies, since the dependency resolution happens after the scripts are read. If you are downloading them somehow before the script runs, then you probably don't need a maven repository for that.

Getting all files of an artifact from maven repository

Our organization works in an isolated network so when we want to use libraries we need to retrieve them from maven-central and upload then to our own maven repository.
I am searching for way do download all the files of my required dependencies,
I have used gradle gradle eclipseClasspath to do this but it is not downloading all the files. for example org.jacoco:jacoco:0.7.8 contains a zip file and when I look in the GRADLE_USER_HOME I can not find the zip.
build.gradle:
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile 'org.jacoco:jacoco:0.7.8'
}
What should I do?
You could use this gradle task to download the dependencies to a local directory.
I wanted a simple solution, for now I have made this ugly one https://github.com/idosu/scripts/blob/master/gradle/full-download-from-central.gradle
usage:
apply from: 'https://raw.githubusercontent.com/idosu/scripts/master/gradle/full-download-from-central.gradle'
dependencies {
compile 'org.jacoco:jacoco:0.7.8'
}
windows: set GRADLE_USER_HOME=deps & gradle --no-daemon -b eclipseClasspath createRepo
shell: export GRADLE_USER_HOME=deps; gradle --no-daemon -b eclipseClasspath createRepo
The result will be in build/repo
A better way to do this is figuring out how eclipseClasspath resolves all the dependencies with all the parent poms. because using project.configurations.compile.resolvedConfiguration.lenientConfiguration.allModuleDependencies does not do the trick

Dependenciies in Gradle not working correctly

We have a project that is using Java 1.5 and we are trying to convert from Maven to Gradle.
We have a repository that is local to us containing all the versions of all the jars we need as the dev environment has no access to the internet.
The problem we are seeing is that it cannot find the commons-io jar and keeps trying to goto the external maven repo. we have not even set that up so where is it finding it from?
we have repositories and dependencies set up in the All projects section as follows
allprojects {
apply plugin: 'java'
sourceCompatibility = 1.5
targetCompatibility = 1.5
project.tasks.withType(AbstractCompile, { AbstractCompile ac -> ac.options.bootClasspath = "C:/Program Files/java/1.5.0_14/jre/lib/rt.jar" })
repositories {
mavenLocal()
maven { url "http://internalrepo/maven-local" }
}
dependencies {
compile "org.apache.commons:commons-io:1.3.2"
}
But its reporting
Could not resolve org.apache.commons:commons-io:1.3.2.
inconsistent module metadata found
even though it works fine in Maven using mvn install
Gradle will never query a repo that isn't set up. mavenlocal() is misspelled (should be mavenLocal()), which will make the build fail. "Inconsistent metadata" could mean that the group ID, artifact ID, or version in the POM doesn't match the one in the build script. mavenLocal() should only be used if the Gradle build needs to exchange artifacts with local Maven builds.
Found the issue,
Unbeknownst to me there was a hidden repo in the maven settings.xml in the maven install folder.
Adding that resolved the issue.

how to tell gradle to download all the source jars

Ideally, we would like to add a task for downloading all the source jars for the first level and transitive dependencies of our project. Is there a way to do that?
If not, is there a command line option to supply like maven has to get all the sources downloaded onto our machines?
It seems like that should just be the default these days at least for first level dependencies as it gives you the javadoc in eclipse then which is very nice when doing the code completion stuff.
The eclipse task can be configured with downloadSources. Following is an example of that configuration
apply plugin: 'java'
apply plugin: 'eclipse'
eclipse {
classpath {
downloadSources=true
}
}
So run
gradle cleanEclipse eclipse
to have it download sources.
If you use Eclipse and want to navigate the source code of your dependencies there, then the Eclipse plugin does this for you.
Install the eclipse plugin by adding apply plugin: "eclipse" to your build.gradle file. Then run gradle eclipse to generate the Eclipse .project, .classpath and .settings files. The plugin will download all available sources automatically and add references them in the .classpath file (see the sourcepath attribute of the classpathentry element).
To import the project into Eclipse, choose File > Import... > Existing Projects into Workspace and select your project.
(I'm not sure whether the Idea plugin does the same for Idea users, but it may do).
Another catch not mentioned in other answers is when you are using mavenLocal() repository in your gradle.build file. If there are downloaded jar in that local maven repo but no downloaded sources or javadocs in that repo, then gradle will not even try to download javadocs or sources for you. Even with enabled eclipse.classpath.downloadJavadoc and eclipse.classpath.downloadSources.
The solution is to remove mavenLocal() from repositories or place it to bottom of the list. Or you can setup maven to download sources and javadocs and clean your maven local repository (~/.m2/repository).
A more detailed description of the problem is here.
Here is how to add the required configuration in Gradle using the IDEs' plugins:
For Eclipse:
apply plugin: 'java'
apply plugin: 'eclipse'
eclipse {
classpath {
downloadJavadoc = true
downloadSources = true
}
}
For IntelliJ
apply plugin: 'java'
apply plugin: 'idea'
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
To run these plugins:
gradle cleanEclipse eclipse
gradle cleanIdea idea
Piohen's comment above should be it's own answer (since it was the only solution that worked for me)
Right click your project, then select "Build Path" --> "Configure Build Path";
Select "Order and export"
Select "Web App Libraries", and click "Bottom" button, then the "Web App Libraries" will be on the bottom;
And to get this into the Gradle Eclipse plugin (so you don't need to do it manually every time):
Why is Eclipse not attaching 3rd party libs source files to a WTP-faceted Gradle project?
There is only one problem here. This only works if you are generating NEW projects. If you are working on mature projects that can't be re-generated using Gradle, the above suggestions will not work.
One thing I should add is that the version of Gradle/Builsdhip plugin strongly depends on the version of Java you use to start Eclipse. They must all be compatible. I had to switch my Eclipse (current version) from Java 11 back to Java 8 to fix Buildship (3.0.1) errors. We are, and have been, stuck on Gradle 4.2.1 for some time due to API changes in Gradle breaking our build. So to move to Java 11 we have to move to a new version of Gradle, Buildship, and Eclipse. Ugh! Oh yeah, this also fixed the issue mentioned in this thread. Source is now avalable again.

Resources