Creating Private Maven/Gradle Repository
Tags: maven, gradle, java, kotlin
Published: 2020-05-31 15:03:47

---
title: Creating Private Maven/Gradle Repository
published: true
description: creating and deploying private maven repositories
tags: maven, gradle, java, kotlin
//cover_image: https://direct_url_to_image.jpg
---

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](https://sdkman.io). In general, I prefer using [maven wrapper](https://github.com/takari/maven-wrapper) in my projects.

# Deployment to Private Maven Repository

I am going to use [Repsy](https://repsy.io) 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
```

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
```

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

```java
package com.github.firatkucuk.hellomavenlib;

public class Hello {

    private Hello() {
    }

    public static String sayHello(final String name) {
        return "Hello " + name;
    }
}
```

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
<?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>
```

To proceed to the next section, we need to create an account at [Repsy](https://repsy.io). You can directly go to the [register](https://panel/repsy.io/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.

```xml
<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>
```

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

```xml
<?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>
```

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.

```bash
mvn compile deploy
```

That's all for the deployment part. If you want to access the source code for this library. You can access it from [here](https://github.com/firatkucuk/hello-maven-lib).

# 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
```

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

```xml
<?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>
```

Please do not forget to change your repository URL.

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

```java
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");
        }
    }
}
```

Let's compile our project.

```bash
mvn compile
```

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:

```bash
mvn compile dependency:copy-dependencies
```

let's run with the classpath definition

```bash
java -cp target/dependency/hello-maven-lib-1.0.jar:target/classes com.github.firatkucuk.hellomavenapp.Main "World!"
```

You can reach the source code from [here](https://github.com/firatkucuk/hello-maven-app)