Andrew Kew
Andrew Kew

Reputation: 3363

Deploying jar to Nexus using Maven Ant Task

I am going completely mad here. I am simply trying to upload a jar to a Nexus server using Maven Ant Task but when I run the build.xml file using ant I get the following error

Deploying to http://nexus.xxxx.co.uk:8081/repository/maven-snapshots/
[INFO] Retrieving previous build number from snapshot
Uploading: com/xxx/xxx-yyy-zzz/3.0.0-SNAPSHOT/xxx-yyy-zzz-3.0.0-SNAPSHOT.jar to repository snapshot at http://nexus.xxxxx.co.uk:8081/repository/maven-snapshots/
Transferring 1091K from snapshot
Error writing to server
An error has occurred while processing the Maven artifact tasks.
 Diagnosis:

Error deploying artifact 'com.xxx-yyy-zzz': Error deploying artifact: Error transferring file
Error writing to server

This is what my build.xml file looks like

<project name="deploy" default="mvn-deploy" xmlns:artifact="antlib:org.apache.maven.artifact.ant">

    <path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar"/>
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
             uri="antlib:org.apache.maven.artifact.ant"
             classpathref="maven-ant-tasks.classpath"/>

    <artifact:remoteRepository id="snapshot" url="http://nexus.xxxx.co.uk:8081/repository/maven-snapshots/">
        <releases enabled="false"/>
        <snapshots enabled="true"/>
    </artifact:remoteRepository>

    <target name="mvn-deploy">
        <artifact:deploy file="/path/to/jar/xxx-yyy-zzz-3.0.0-SNAPSHOT.jar"
                         uniqueVersion="false">
            <remoteRepository refid="snapshot">
                <authentication username="username" password="password"/>
            </remoteRepository>
            <pom file="/path/to/pom/pom.xml"/>
        </artifact:deploy>
    </target>
</project>

I think I have read every blog post/stackoverflow post, ant documentation page out there and still cant work out why this doesnt work.

On the Nexus server this is what the logs show (request.log)

77.101.44.67 - - [16/Apr/2020:12:08:03 +0000] "PUT /repository/maven-snapshots/com/xxx/xxx-yyy-zzz/3.0.0-SNAPSHOT/xxx-yyy-zzz-3.0.0-20200416.120803-3.jar HTTP/1.1" 401 1117368 0 1 "maven-artifact/2.2.1 (Java 1.8.0_31; Mac OS X 10.13.6)" [qtp615812221-41545]
77.101.44.67 - - [16/Apr/2020:12:08:03 +0000] "PUT /repository/maven-snapshots/com/xxx/xxx-yyy-zzz/3.0.0-SNAPSHOT/xxx-yyy-zzz-3.0.0-20200416.120803-3.jar HTTP/1.1" 401 1117368 0 1 "maven-artifact/2.2.1 (Java 1.8.0_31; Mac OS X 10.13.6)" [qtp615812221-41531]

So as you can see from the logs, I am getting a 401 HTTP response code, which means Unauthorized. Also the 3rd parameter in the log lines above should show the username, so this kind of leans towards ant not passing the username and password through when upload the file, and its failing because you need credentials to upload the file.

Its defo not a Nexus configuration issue or nexus user permissions issue because if I use maven and run

mvn deploy

With the same info it uploads the jar without an issue

Any suggestion would be greatly appreciated before I have no hair left...

Upvotes: 2

Views: 1649

Answers (1)

Andrew Kew
Andrew Kew

Reputation: 3363

So all the credit here has to go to @user944849 for making me aware that the maven ant tasks are actually retired now as per the website

Maven Ant Tasks Website

Right at the top of the page it plainly says its retired and also that its been replaced with the Maven Artifact Resolver Ant Tasks. So I gave that a go and just like magic that worked first go.

I also got it to work with handling sending snapshots and releases to the correct repos. This is handled by the pom that you link and the version of the project, SNAPSHOT version goes to snapshot repo and non SNAPSHOT version to the releases repo

For anyone finding this, this is the build.xml file I created and used

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:resolver="antlib:org.apache.maven.resolver.ant">
    <taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
        <classpath>
            <fileset dir="lib" includes="maven-resolver-ant-tasks-1.2.0-uber.jar"/>
        </classpath>
    </taskdef>

    <resolver:authentication username="user" password="pass" id="auth"/>

    <resolver:remoterepo id="ossrh" url="http://nexus.xxxx.co.uk:8081/repository/maven-snapshots/"
                type="default" releases="false" snapshots="true" updates="always" checksums="fail">
        <resolver:authentication refid="auth"/>
    </resolver:remoterepo>

    <resolver:remoterepo id="rao" url="http://nexus.xxxxx.co.uk:8081/repository/maven-releases/">
        <resolver:releases enabled="true" updates="daily" checksums="warn"/>
        <resolver:snapshots enabled="false"/>
        <resolver:authentication refid="auth"/>
    </resolver:remoterepo>

    <resolver:remoterepos id="all">
        <resolver:remoterepo refid="ossrh"/>
        <resolver:remoterepo refid="rao"/>
    </resolver:remoterepos>

    <resolver:artifacts id="producedArtifacts">
        <resolver:artifact file="/path/to/jar/xxx-yyy-zzz-3.0.0-SNAPSHOT.jar"/>
    </resolver:artifacts>

    <resolver:pom file="/path/to/pom/pom.xml" id="pom"/>

    <target name="deploy">
        <resolver:deploy artifactsref="producedArtifacts">
            <resolver:remoterepo refid="rao"/>
            <resolver:snapshotrepo refid="ossrh" />
        </resolver:deploy>
    </target>
</project>

Only major gotcha here was the taskdef at the top of the file needs the jar for the maven resolver ant tasks. I found that the one in maven central was giving me dependency errors when I ran my build file , so what I did instead was download the source from the website, and then I ran

mvn package -DskipTests=true

Reason I ran skip tests was some of the tests were failing for some reason, I guess you could try and work out why and remove that switch.

You will find in the target directory will be the Uber jar, copy that into your lib directory and then it all works like magic.

Happy ant'ing :)

Upvotes: 2

Related Questions