In my gradle project, I want to upload my jar to multiple maven repository, so I do the following:
uploadArchives {
repositories {
mavenLocal()
mavenDeployer {
repository(url: maven_repo_1) {
authentication(userName: maven_user_1, password: maven_password_1)
}
repository(url: maven_repo_2) {
authentication(userName: maven_user_2, password: maven_password_2)
}
}
}
}
My question is:
Is this the correct way to upload to multiple maven repository at once?
Is there a way to use a parameters to upload to specific maven repository? I.e uploadArchives -r mavenLocal
Note:
Previously I had to remove the maven repository and run uploadArchives, I don't think this is the best practice. However I didn't find documentation for uploading it with parameters.
What you could do, is using a project property to pick the right configuration.
For example, if you want to deploy to a remote repository by default, but with a parameter that switches to a local repo:
uploadArchives {
repositories {
if (project.hasProperty('localRepoPath')) {
mavenDeployer {
repository(url: uri(project.getProperty('localRepoPath')))
...
}
} else {
mavenDeployer {
repository(url: maven_repo_1)
...
}
}
}
}
Then, to invoke:
# Local upload
./gradlew uploadArchives -PlocalRepoPath=/tmp/maven
# Public upload
./gradlew uploadArchives
You can adapt this approach to your own needs.
Related
When I run "gradle build", it generates a build/libs/project-version.war file which is able to run by itself with "java -jar" command. But when I run "gradle artifactoryPublish", it saves on the artifactory repository a "project-version-plain-war" file which does not have tomcat embedded.
I am following this instruccions https://www.jfrog.com/confluence/display/JFROG/Gradle+Artifactory+Plugin
The lines added to the gradle.build are something like this:
plugin "maven-publish"
plugin "com.jfrog.artifactory"
artifactory {
...
}
subprojects {
plugin "war"
group = group
version = version
publishing {
publications {
MavenPublication(MavenPublication) {
from components.web
}
}
repositories{
maven { url "https://artifactory-server" }
}
}
}
help is appreciated
Using from components.web means that you are publishing the artifact produced by the war task. You need to publish the artifact produced by the bootWar task. You can do so by changing the publication to use artifact bootWar instead:
publishing {
publications {
MavenPublication(MavenPublication) {
artifact bootWar
}
}
repositories{
maven { url "https://artifactory-server" }
}
}
This is described in the reference documentation for Spring Boot's Gradle plugin.
I'm using the Gradle local flatDir repository in the following way:
repositories {
flatDir {
dirs 'D:/path/to/local/directory'
}
}
but it only searches for the first layer of files. I wonder if there is an option of telling Gradle to locate the dependencies all over the repository, so I would be able to organize my jars by groups and still make Gradle read them all without specifying the groups.
Have you considered using the Maven directory conventions instead of flatDir?
Eg store jars like
myLocalRepo/com/foo/bar/1.0/bar-1.0.jar
myLocalRepo/com/foo/baz/2.0/baz-2.0.jar
Then in build.gradle
repositories {
maven {
uri file('myLocalRepo')
}
}
dependencies {
compile 'com.foo:bar:1.0'
compile 'com.foo:baz:2.0'
}
I have a Java application which uses a custom library named com.abc.XYZLogging.
This library is dependent on org.springframework:spring-context:4.3.7.RELEASE and org.slf4j:slf4j-api:1.7.10.
So the dependency diagram is like this:
com.abc.XYZLogging
|---org.springframework:spring-context:4.3.7.RELEASE
|---org.slf4j:slf4j-api:1.7.10
|---log4j:log4j:1.2.17
All the artifacts are present in the correct locations and in the same repo. However gradle picks up only the first level com.abc.XYZLogging and it does not pickup any other dependency in the hierarchy.
I use gradle 4.2 and Oracle JDK 1.8.144. This happens even when I invoke gradle clean build from command line and via Buildship plugin in eclipse.
Is there any dependency mapping missing in my artifactory ?
Example of how to configure a repository based on Artifactory to behave like in a Maven style using artifactory's own plugin, as sometimes just pointing to artifactory as usually done is not enough.
This is just an example for getting libs from artifactory, to publish extra config is necessary. Also this example is for a multi-build where the build script is separated from the artifactory config. More at: https://www.jfrog.com/confluence/display/RTF/Gradle+Artifactory+Plugin
This first example is getting the plugin from artifactory itself, and the second for jcenter.
buildscript {
dependencies { classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release" }
repositories {
maven {
credentials {
username = artifactory_username
password = artifactory_password
}
url "https://.../artifactory/libs-release"
}
}
}
allprojects{
apply plugin: org.jfrog.gradle.plugin.artifactory.ArtifactoryPlugin
artifactory {
resolve {
contextUrl = "https://.../artifactory"
repoKey = 'libs-global'
username = artifactory_username
password = artifactory_password
}
}
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:latest.release"
}
}
apply plugin: "com.jfrog.artifactory"
OR simpler, in newer versions
plugins {
id "com.jfrog.artifactory" version "latest.release"
}
so you can use blocks like:
artifactory {
resolve {
contextUrl = "https://.../artifactory"
repoKey = 'libs-global'
username = artifactory_username
password = artifactory_password
}
}
I'm trying to get my gradle script to upload to an sftp account, but it keeps failing with error,
Failed to deploy artifacts: Could not transfer artifact com.himself12794:Heroes-Mod:jar:0.9-rev1 from/to remote (sftp://himself1#ftp.himself12794-develops.com:18765/public_html/maven): Could not write to resource 'com/himself12794/Heroes-Mod/0.9-rev1/Heroes-Mod-0.9-rev1.jar'
I'm able to connect via sftp command:
sftp -P 18765 himself1#ftp.himself12794-develops.com
However, the publishMavenJavaPublicationToMavenRepository task fails, with the above error.
This is a Minecraft Forge mod, if that sheds any light on things.
My build.gradle is like so:
buildscript {
repositories {
jcenter()
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.0- SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: "maven-publish"
// for people who want stable
/*plugins {
id "net.minecraftforge.gradle.forge" version "2.0.1"
id "maven-publish"
}*/
repositories {
maven {
name = "himself12794-develops"
url = "http://maven.himself12794-develops.com"
}
}
version = "0.9-rev1"
group = "com.himself12794"
archivesBaseName = "Heroes-Mod"
minecraft {
version = "1.8-11.14.3.1514"
runDir = "run"
mappings = "snapshot_20141130"
// makeObfSourceJar = false
}
dependencies {
compile "com.himself12794:powersAPI:1.1-rev2"
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
publishing {
repositories {
maven {
url "sftp://himself1#ftp.himself12794-develops.com:18765"
}
}
}
processResources
{
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
expand 'version':project.version, 'mcversion':project.minecraft.version
}
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
Any assistance anyone can give is greatly appreciated.
I have not solved this specific problem yet, but I have discovered that the problem with the original maven plugin that caused me to try the maven-publish plugin was actually an issue with the current stable version of Gradle (2.7). Using the current nightly wrapper build has allowed me to circumvent this.
I successfully uploaded my jars to a nexus repository using the maven plugin for gradle but it didn't upload the sources. This is my configuration:
uploadArchives {
repositories{
mavenDeployer {
repository(url: "http://...") {
authentication(userName: "user", password: "myPassword")
}
}
}
}
I searched and found that I can add the sources by adding a new task.
task sourcesJar(type: Jar, dependsOn:classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
This works fine but I think there must be a better solution by configuring the maven plugin, something like uploadSource = true like this:
uploadArchives {
repositories{
mavenDeployer {
repository(url: "http://...") {
authentication(userName: "user", password: "myPassword")
}
uploadSources = true
}
}
}
There is no better solution than what you described yourself. The gradle maven plugin is uploading all artifacts generated in the current project. That's why you have to explicitly create a "sources" artifact.
The situation also doesn't change when using the new maven-publish plugin. Here, you also need to explicitly define additional artifacts:
task sourceJar(type: Jar) {
from sourceSets.main.allJava
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourceJar {
classifier "sources"
}
}
}
}
The reason is that gradle is more about being a general build tool and not bound to pure Java projects.
You can use gradle-nexus-plugin
In order to use the plugin, add the lines below and import plugin
buildscript {
repositories {
mavenLocal()
jcenter {
url "http://jcenter.bintray.com/"
}
}
dependencies {
classpath 'com.bmuschko:gradle-nexus-plugin:2.3'
}
}
apply plugin: 'com.bmuschko.nexus'
Add this section, where you will configure url to deploy
nexus {
sign = false
repositoryUrl = 'http://localhost:8081/nexus/content/repositories/releases/'
snapshotRepositoryUrl = 'http://localhost:8081/nexus/content/repositories/internal-snapshots/'
}
Note: You must have in ~/.gradle/gradle.properties
nexusUsername = deployment
nexusPassword = deployment123
Nexus for save artifact, not source code.
for upload compiled artifact I do:
apply plugin: 'java'
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://nexus-server:8081/nexus/content/repositories/snapshots") {
authentication(userName: "admin", password: "secret")
}
pom.version = "1.0.0-SNAPSHOT"
pom.artifactId = "${project.name}"
pom.groupId = "path.to.artifact"
}
}
}
and call upload from console
$ gradle upload
for source code use maven or git repository