4

This is my POM file that generate a JAR artifact and it is stored in a private repository with Nexus Repository

<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>org.home.mac</groupId>
  <artifactId>hyper-dev</artifactId>
  <version>0.0.1</version>
  <build>
    <sourceDirectory>src</sourceDirectory>

    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>  
  </build>

  <dependencies>
    <dependency>
        <groupId>org.home.mac</groupId>
        <artifactId>hyper-test-linux</artifactId>
        <version>0.3.5</version>
    </dependency>
  </dependencies>
</project>

I want to check two things:

  • If the artifact that I am going to install hyper-dev-0.0.1 exists in my private maven repository.
  • If the dependency with artifactID: hyper-test-linux-0.3.5 exists in my private maven repository.

Is it possible?

gtx911
  • 1,189
  • 4
  • 25
  • 46
  • What kind of problem are you trying to solve? Why do you need to do that? – khmarbaise May 23 '18 at 15:44
  • @khmarbaise Check if exists. If exists not generate again, and if exists generate and upload it. – gtx911 May 23 '18 at 15:45
  • Sorry I don't understand your answer? – khmarbaise May 23 '18 at 15:59
  • @khmarbaise Check before generating the artifact, if it is stored in my private Nexus repository. If that version is not stored, generate it using mvn clean install. – gtx911 May 23 '18 at 16:15
  • You express the same in different flavours but unfortunately I don't understand what your real problem is? If you have already a release you will fail cause Nexus will not allow to redeploy the same version again which shows you have a flaw in your CI process which should automatically increment the version etc. ? Furthermore I would recommend not to change the conventions in Maven means `src`.... – khmarbaise May 23 '18 at 16:17

2 Answers2

3

You can use Nexus' Rest Api to check if an artifact exists.

For example, the url

http://<your private nexus server>:8081/service/rest/beta/search/assets?group=org.home.mac&name=hyper-dev&version=0.0.1&maven.extension=jar&maven.classifier

will show you if the artifact hyper-dev in the version 0.0.1 is available in your private Nexus.

If you want to automate the process, you can use a command line tool like wget or curl to access the Rest Api, as shown in the document linked above.

Remark: I would like to repeat the comment of khmarbaise that is usually not possible to upload a released artifact to Nexus if it already exists in the repository. If you want to upload it again, you have to increase the version and by doing so, create a new artifact. It would be an unwanted feature to update existing artifacts, as Maven assumes that a downloaded artifact will never change and caches them locally on every machine.

Snapshot artifacts can actually be updated, but you have asked about released artifacts.

werner
  • 13,518
  • 6
  • 30
  • 45
1

For Nexus2 (using as example org.jboss.security:jboss-negotiation-toolkit:3.0.2.Final)

curl -I -s -u admin:admin123 http://mynexusserver/service/local/repositories/mymavenrepo/content/org/jboss/security/jboss-negotiation-toolkit/3.0.2.Final/jboss-negotiation-toolkit-3.0.2.Final.war | grep HTTP

This will print "HTTP/1.1 200 OK" if found, "HTTP/1.1 404 Not Found" if not found

Pierluigi Vernetto
  • 1,954
  • 1
  • 25
  • 27
  • Thanks, i use nexus3, so my curl is a bit different `curl -I -s -u ${NEXUS_USER}:${NEXUS_PWD} https://${NEXUS_SERVER}/repository/${REPO}/${GROUP_ID}/${POM_ARTIFACT}/${VERSION}/${POM_ARTIFACT}-${VERSION}.${PACKAGING} | grep HTTP)` – Juan Jul 10 '19 at 15:46