Skip to content

Instantly share code, notes, and snippets.

@dkirrane
Forked from cedricwalter/readme.md
Created May 30, 2017 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkirrane/4ed4bc6baa76deef59dc2bad2bd586fb to your computer and use it in GitHub Desktop.
Save dkirrane/4ed4bc6baa76deef59dc2bad2bd586fb to your computer and use it in GitHub Desktop.
Sonatype NEXUS 2 has a rest API :-) but NEXUS 3 has none/not ready, the following simulate curl/wget call

Fetching artifact programmatically through REST/API

Nexus 2.x

Nexus 2.x had a REST API to download artifacts like below based on some Maven GAV co-ordinates but this no longer works for Nexus 3.x

wget "http://local:8081/service/local/artifact/maven/redirect?g=com.mycompany&a=my-app&v=LATEST" --content-disposition

or

curl --insecure "https://local:8081/service/local/artifact/maven/content?r=public&g=log4j&a=log4j&v=1.2.17&p=jar&c=" > log4j.jar

Nexus 3.x

workaround in 2 steps now, require maven in path

generic template file, do not modify

echo '<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acme</groupId>
    <artifactId>any</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <properties>
        <groupId></groupId>
        <artifactId></artifactId>
        <version></version>
        <type></type>
        <classifier></classifier>
        <destFileName></destFileName>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${groupId}</groupId>
                                    <artifactId>${artifactId}</artifactId>
                                    <version>${version}</version>
                                    <type>${type}</type>
                                    <classifier>${classifier}</classifier>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>.</outputDirectory>
                                    <destFileName>${destFileName}</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>' > getDependency.xml

we can now replace the curl/wget with, change -D values

mvn -f getDependency.xml process-resources \
   -DgroupId=log4j -DartifactId=log4j -Dversion=1.2.17 \
   -Dtype=jar -Dclassifier= -DdestFileName=log4j.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment