loading...

Creating Private maven/gradle Repository

fk profile image Fırat KÜÇÜK ・4 min read

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. Or in general, I prefer using maven wrapper in my projects.

Deployment to Private Maven Repository

I am gonna use Repsy for this purpose. I am gonna 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 that:

- 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 that:

- src
    - main
        - java
            - com
                - github
                    - firatkucuk
                        - hellomavenlib
        - resources
    - test
- pom.xml

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;
    }
}

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

  <!-- The other stuff will come here -->
</project>

For proceeding 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 create a private maven repository called default for us.

It might be a good practice to not store maven repository password in our maven libary pom.xml file. So let's create a settings file or modify the existing one. In your operating system there should be .m2 folder at your home directory. If you haven't use maven yet you might need to create one. For linux users ~/.m2 directory. If it doesn't exist. You can create one 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>firat</username>
       <password>MY PASSWORD</password>
     </server>
   </servers>
 </settings>

Please modify your username and password sections. Let's put 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>

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

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

mvn compile deploy

That's all for deployment part. If you want to access sourcecode for this library. You can access 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

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>

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

Let's compile our project.

mvn compile

That's all our sample application uses 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 execut mvn compile package. I am gonna just copy dependencies and execute with that dependency.

Let's run maven dependency plug-in:

mvn compile dependency:copy-dependencies

let's run with classpath definition

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

Posted on May 31 by:

fk profile

Fırat KÜÇÜK

@fk

Lifetime Developer, Sci-fi addicted, animal lover, the last baklava bender, good lyrics detector

Discussion

markdown guide