DEV Community

Fırat Küçük
Fırat Küçük

Posted on • Updated on

Creating Private Maven/Gradle Repository

In some cases, you need a private library, maybe because of your company's privacy policy or your personal preferences. For instance, you have multiple projects and you need some common DTOs or common utilities. It might be a good choice to separate them and make it a common jar package for those.

You need to install Maven and JDK for this tutorial. If you don't have Maven or JDK installed. Maybe you can try sdkman. In general, I prefer using maven wrapper in my projects.

Deployment to Private Maven Repository

I am going to use Repsy for this purpose. It's easy to use a Cloud Maven repository. So let's create a common library first. You can create a maven project using your favorite IDE or editor.

Your initial project structure will be like this:

- src
  - main
    - java
    - resources
  - test
- pom.xml
Enter fullscreen mode Exit fullscreen mode

First, let's create a package that represents our company TLD or our personal preference. I am gonna use my personal GitHub TLD. You can use yours or something else. After creating the package directory structure will be something like this:

- src
  - main
    - java
      - com
        - github
          - firatkucuk
            - hellomavenlib
    - resources
  - test
- pom.xml
Enter fullscreen mode Exit fullscreen mode

Ok, now we can create our super complicated library class.

package com.github.firatkucuk.hellomavenlib;

public class Hello {

    private Hello() {
    }

    public static String sayHello(final String name) {
        return "Hello " + name;
    }
}
Enter fullscreen mode Exit fullscreen mode

For the final stage, we need to modify our pom.xml. A definitive section for our library artifact is needed. It will be something like that.

<?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.github.firatkucuk</groupId>
  <artifactId>hello-maven-lib</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <!-- The other stuff will come here -->
</project>
Enter fullscreen mode Exit fullscreen mode

To proceed to the next section, we need to create an account at Repsy. You can directly go to the register section. Pick a username and password that's all. Now we can use our maven repository. Repsy automatically creates a private Maven repository called default for us.

It might be a good practice to not store the Maven repository password in our Maven library pom.xml file. So let's create a settings file or modify the existing one. In your operating system, there should be .m2 folder in your home directory. If you haven't used Maven yet you might need to create one. For Linux/Mac users ~/.m2 directory. If it doesn't exist. You can create one via mkdir ~/.m2.

Let's create/modify ~/.m2/settings.xml file.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
   http://maven.apache.org/xsd/settings-1.0.0.xsd">

   <servers>
     <server>
       <id>repsy</id>
       <username>YOUR_REPSY_USERNAME</username>
       <password>YOUR_REPSY_PASSWORD</password>
     </server>
   </servers>
 </settings>
Enter fullscreen mode Exit fullscreen mode

Please modify your username and password sections. Let's put the deployment repository definition section into our pom.xml file.

<?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.github.firatkucuk</groupId>
  <artifactId>hello-maven-lib</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <distributionManagement>
    <repository>
      <id>repsy</id>
      <name>My Private Maven Repositoty on Repsy</name>
      <url>https://repo.repsy.io/mvn/YOUR_REPSY_USERNAME/default</url>
    </repository>
  </distributionManagement>
</project>
Enter fullscreen mode Exit fullscreen mode

Please change your repository URL. It will be something like: https://repo.repsy.io/mvn/YOUR_REPSY_USERNAME/default. Now we can deploy our very first private repo library.

mvn compile deploy
Enter fullscreen mode Exit fullscreen mode

That's all for the deployment part. If you want to access the source code for this library. You can access it from here.

Using Private Maven Repository

Let's create another project with a proper TLD structure.

- src
  - main
    - java
      - com
        - github
          - firatkucuk
            - hellomavenapp
    - resources
  - test
- pom.xml
Enter fullscreen mode Exit fullscreen mode

and modify our pom.xml with proper remote repo settings and dependency settings.

<?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.github.firatkucuk</groupId>
  <artifactId>hello-maven-app</artifactId>
  <version>1.0</version>

  <properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <repositories>
    <repository>
      <id>repsy</id>
      <name>My Private Maven Repositoty on Repsy</name>
      <url>https://repo.repsy.io/mvn/firat/default</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>com.github.firatkucuk</groupId>
      <artifactId>hello-maven-lib</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
</project>
Enter fullscreen mode Exit fullscreen mode

Please do not forget to change your repository URL.

Let's create a Java class that uses our hello library.

package com.github.firatkucuk.hellomavenapp;

import com.github.firatkucuk.hellomavenlib.Hello;

public class Main {

    public static void main(final String[] args) {

        if (args.length > 0) {
            Hello.sayHello(args[0]);
        } else {
            Hello.sayHello("maven");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Let's compile our project.

mvn compile
Enter fullscreen mode Exit fullscreen mode

That's all our sample application uses a remote private repository. If you're not using a framework like Spring you need to take a look at some fatjar solutions or if you're gonna deploy to Java Application server. You can change your packaging to war and can execute mvn compile package. I am going to copy dependencies and execute with that dependency.

Let's run the Maven dependency plug-in:

mvn compile dependency:copy-dependencies
Enter fullscreen mode Exit fullscreen mode

let's run with the classpath definition

java -cp target/dependency/hello-maven-lib-1.0.jar:target/classes com.github.firatkucuk.hellomavenapp.Main "World!"
Enter fullscreen mode Exit fullscreen mode

You can reach the source code from here

Top comments (2)

Collapse
 
shekharrai profile image
Shekhar Rai • Edited

Great! works well :) But I'm having an issue while uploading dependencies from one machine and downloading dependencies using another one.

Collapse
 
fk profile image
Fırat Küçük

you can open a support ticket. Will respond very quickly.