0
votes

It has been 2 weeks since I've managed to setup Nexus OSS, Maven and then Gradle to be able to realize the following goals:

  1. Upload files into a corporate Nexus Repository linking them together. So the files must keep a link in order to define a "package" populated by their groupId/artifactId/version.

  2. Download a "package" (set of files) linked together by something (like a POM dependency schema).

Currently, I am able to use Maven and Gradle to upload ONE file on Nexus. Even if I specify dependencies in the POM file or in the build.gradle with Gradle, I am not able to download the file with all of its dependencies at once.

The goal here is only to be able to define a set of component ordered by group/name/version, and be able to download them all at once for one package. This is like version packaging management.

I have look 2 weeks and wasn't able to use Maven or Gradle to achieve these goals. Please can anyone tell me EXACTLY how to use Maven or Gradle to achieve this work ?

This is the POM files i use to link artifactA with artifactB in Maven:

ArtifactA POM file:

<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>groupA</groupId>
   <artifactId>artifactA</artifactId>
   <version>1.2.4</version>
   <packaging>jar</packaging>

   <dependencyManagement>
      <dependencies>
      </dependencies>
   </dependencyManagement>

   <distributionManagement>
      <repository>
         <id>nexus</id>
         <name>Nexus Test Repository</name>
         <url>http://localhost:7080/repository/content/repositories/releases/</url>
      </repository>
   </distributionManagement>

</project>

ArtifactB POM file:

<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>groupA</groupId>
   <artifactId>artifactB</artifactId>
   <version>1.0.0</version>
   <packaging>jar</packaging>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>groupA</groupId>
            <artifactId>artifactA</artifactId>
            <version>1.2.4</version>
            <type>jar</type>
         </dependency>
       </dependencies>
    </dependencyManagement>

    <distributionManagement>
       <repository>
          <id>nexus</id>
          <name>Nexus Test Repository</name>
          <url>http://localhost:7080/repository/content/repositories/releases/</url>
       </repository>
    </distributionManagement>
</project>

Once I have uploaded A then B using these commands:

mvn deploy:deploy-file \
-Dfile=artifactA_package.jar \
-Dpackaging=jar \
-DpomFile=pomA1.2.4.xml \
-Durl=http://localhost:7080/repository/content/repositories/releases/ \
-DrepositoryId=nexus

mvn deploy:deploy-file \
-Dfile=artifactB_package.jar \
-Dpackaging=jar \
-DpomFile=pomB1.0.0.xml \
-Durl=http://localhost:7080/repository/content/repositories/releases/ \
-DrepositoryId=nexus

The two files are well uploaded on my Nexus repository BUT, even if the artifactB POM file specify a dependency to A, when I download B using this script:

mvn dependency:get \
-Dartifact=groupA:artifactB:1.0.0:jar \
-DremoteRepositories=nexus::default::http://localhost:7080/repository/content/repositories/releases/

Maven only downloads B, but not A. And even if I use this command on B:

mvn dependency:resolve

It tells me something like "dependency: none". So Nexus or Maven is not aware of the dependency from B to A.

Gradle also does not work to achieve my goal when I use this "build.gradle" file:

apply plugin: 'maven'

repositories {
    maven {
        url "http://localhost:7080/repository/content/groups/public/"
    }
}

artifacts {
    archives file('artifactB_package.jar')
}

dependencies {
    archives files('artifactA_package.jar')
}

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "http://localhost:7080/repository/content/repositories/releases/") {
            authentication(userName: "deployment", password: "deployment123")
            }

            pom.project {
                groupId "groupA";
                artifactId "artifactB";
                version "1";

                dependencies {
                    dependency {
                        groupId "groupA";
                        artifactId "artifactA";
                        version "1";
                    }
                }
            }
        }
    }
}

It only uploads B.

2

2 Answers

0
votes

You are some sort of working around the system? I'm not sure what you try to achieve. But if you want to upload several artifacts at once during one build you need to create a multi-module project.

In maven there is a convention: one pom - one artifact (this is more of a pirate guideline, its not true, for example source jars and javadoc jars can be in the same build - so there are 3 files uploaded for one pom).

The deploy command you use mainly tells maven to upload the artifact manually. The goal you need would be mvn deploy. This executes the build phases and uploads the created artifacts to nexus (using the configuration in the distributionManagement section. With a multi-module build this is done for every module. There is a guide and there are also several archetypes if you need examples (for ex. at appfuse).

I don't know how gradle handles this or what best practice they use.

0
votes

There are example project for Maven, Gradle and others in the Nexus evaluation guide. The whole docs are at http://books.sonatype.com/nexus-book/reference/eval.html and the example projects are at https://github.com/sonatype/nexus-book-examples

Beyond that if you want to create a project that adds all dependencies together with Maven you can use at the assembly plugin e.g. at http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html