1

I have one project created which contains some reusable methods which can be used in other projects by adding it as a dependency. To do the same I have added the following in pom.xml file and when I run mvn clean deploy branch is created successfully with artifacts.

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.abc</groupId>
    <artifactId>api-authenticator</artifactId>
    <version>1.0.0</version>
    <name>API Authenticator</name>
    <description>Project to authenticate API usage</description>

    <distributionManagement>
        <repository>
            <id>internal.repo</id>
            <name>Staging Repository</name>
            <url>file://${project.build.directory}/${version}</url>
        </repository>
    </distributionManagement>

    <properties>
        <github.global.server>github</github.global.server>
        <java.version>11</java.version>
        <java-jwt.version>3.16.0</java-jwt.version>
        <jwks-rsa.version>0.18.0</jwks-rsa.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>${java-jwt.version}</version>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>jwks-rsa</artifactId>
            <version>${jwks-rsa.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>0.12</version>
                <configuration>
                    <message>Maven artifacts for ${project.version}</message>
                    <noJekyll>true</noJekyll>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <branch>refs/heads/${version}</branch>
                    <includes>
                        <include>**/*</include>
                    </includes>
                    <merge>true</merge>
                    <repositoryName>rwg-dip-api-authenticator</repositoryName>
                    <repositoryOwner>robert-walters</repositoryOwner>
                    <server>github</server>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <altDeploymentRepository>
                        internal.repo::default::file://${project.build.directory}/${version}
                    </altDeploymentRepository>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

In settings.xml (Maven home: /opt/homebrew/Cellar/maven/3.8.2/libexec) I have generated personal access token with all access required and added it.

<server>
   <id>github</id>
   <password>access_token</password>
</server>   

To use this as a dependency I have added the following in the project where I want to reuse the functions and when I do reimport the dependency from IntelliJ I can see my internal project's dependency in external dependencies But when I run the project it shows java: package com.abc.util does not exist althoght I am getting the suggestions for the class in Intellj.

    <repositories>
        <repository>
            <id>https://github.com/me/api-authenticator</id>
            <url>https://github.com/me/api-authenticator/1.0.0</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

    <dependency>
        <groupId>com.abc</groupId>
        <artifactId>api-authenticator</artifactId>
        <version>1.0.0</version>
   </dependency>

I have gone through the below but it was not helpful for the above case

Hosting a Maven repository on github

https://www.baeldung.com/maven-repo-github

https://dev.to/iamthecarisma/hosting-a-maven-repository-on-github-site-maven-plugin-9ch

UPDATE

As M.Deinum has suggested I have removed spring-boot-maven-plugin and now I am able to use it as depency in other projects in Intellj and it works fine locally but when the pipeline execute to create build for the project mvn package steps fails with "The POM for com.abc:api-authenticator:jar:1.0.0 is missing, no dependency information available"

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
  • First it is a spring boot project, which generates a special jar with a different structure. Is this is a jar that needs to be used as a dependency don't use that plugin. Second you are publishing to some internal repo and not github (judging from what little you posted here). – M. Deinum Apr 05 '22 at 10:00
  • Hello @M.Deinum, Yes it is a spring boot project. Can you please give the name of a plugin that I don't need to use. Do I need to replace "internal.repo" with github repository URL i.e "https://github.com/me/api-authenticator" – Romil Patel Apr 05 '22 at 10:04
  • 1
    You need to remove the `spring-boot-maven-plugin` from the project that you want to use as a dependency (or configure it to build 2 artifacts, one runnable fat-jar and a regular jar). – M. Deinum Apr 06 '22 at 07:33
  • Thanks, @M.Deinum. I am now able to use its dependency on another project locally. But when mvn package step executes on pipeline it results in "The POM for com.abc:api-authenticator:jar:1.0.0 is missing, no dependency information available" I have updated the question with the details. Can you please suggest on that – Romil Patel Apr 06 '22 at 17:15
  • Hello @M.Deinum. Thanks for your suggestions it is working now. Can you please suggest on https://stackoverflow.com/q/71823908/10961238 – Romil Patel Apr 11 '22 at 07:18

1 Answers1

0

If you are ok with using an external service for that, you can check https://jitpack.io/ out.

It is able to build your maven project directly from a GitHub repository and then acts as a maven repository that is serving the artifact.

It takes time to download the artifact for the first time (as it builds the project only with the first request for the artifact), but then it reuses the already built artifacts for the next requests.

VANAN
  • 488
  • 2
  • 5