How to overwrite a build jar on publishing to a repository

I publish an artifact to our local Maven repository (so that I can then use it in another app).

The relevant section in the build.gradle file is:

publishing {
    publications {
        binary(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            name 'nexus'
            url "http://ourserver/nexus/content/repositories/releases/"
            allowInsecureProtocol = true
        }
    }
}

However the artefact is still under development and not stable, so I want to republish the artefact under teh same version number, but I can’t do so I always get the error:

Could not PUT ‘http://ourserver/nexus/content/repositories/releases/org/mysystem/myconnector/1.0.0/connector-1.0.0.jar’. Received status code 400 from server: Bad Request

How can I tell the server to accept the jar and overwrite one with the same name which already exists?

From Gradle side this is what is happening.

It is your repository configuration which prevents you from overwriting.
So you need to change your Nexus configuration if you really want to do this.

But be aware that when not using a SNAPSHOT version, consumers will not necessarily get the updated jar unless they manually specify to re-get it or the build defines it as a changing version. Otherwise it is quite likely that the consumer uses an old cached version it downloaded before.

I see! So I would need to change Nexus, so that it allows redeployment. Thanks! :smile:

1 Like

Unfortunately it seems to be difficult to modify the Deployment policy of the nexus server because it is administered by somebody else.

So now I’m thinking to “publish” the jar of this app part (‘part2’) locally, so that the other app parts can use it. The full gradle build file for this part looks like this:

apply plugin: 'java'
apply plugin: 'jsonschema2pojo'
apply plugin: 'maven-publish'

buildscript {
    repositories {
        maven {url "xxx"
        		allowInsecureProtocol = true}
        maven { url "yyy"
        		allowInsecureProtocol = true
                credentials {
                username project.properties["mavenUsername"]
                password project.properties["mavenPassword"]
                }
            }
    }
    dependencies {
        classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.1.1'
        classpath "org.ourdomain.myarea.myapp:part1:1.0.0"
    }
}


repositories {
    maven {url "xxx"
            allowInsecureProtocol = true}
    maven { url "yyy"
            allowInsecureProtocol = true
            credentials {
            username project.properties["mavenUsername"]
            password project.properties["mavenPassword"]
            }
        }
}

group 'org.ourdomain.myarea.myapp'
version '1.0.0'

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.1'
    implementation 'org.opensearch.client:spring-data-opensearch:1.1.0'
    implementation "org.ourdomain.myarea.myapp:part1:1.0.0"
    implementation 'io.swagger:swagger-annotations:1.6.1'
}

jsonSchema2Pojo {

    ...

}

publishing {
    publications {
        binary(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            name 'nexus'
            url "yyy"
            allowInsecureProtocol = true
            credentials {
                username project.properties["mavenUsername"]
                password project.properties["mavenPassword"]
            }
        }
    }
}

Is there a way to tell gradle to “publish” into a directory instead and to tell the other parts to check first from this directory and then go out to Nexus?

Yes, you could use mavenLocal(), but be aware that it is usually a pretty bad idea as it is broken by design already in Maven. See Declaring repositories for some further information.

So if you use it, at least us content repository filter so that only exactly what you want to use from there is used.

You can also use an arbitrary local directory as repository to publish to and consume from.

But actually, what you imho better should use is composite builds as long as the Gradle versions are compatible or optimally identical between the projects: Composite Builds

1 Like