Using github repository as maven dependency - java

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"

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.

Related

Maven shade is only shading some classes from the dependency (2 of 3) into final jar

I am using Jitpack to get one of my github repositories as a dependency, and then using maven shade to include said dependency in the compiled output of the project. However, only 2 of the 3 classes in the dependency are being shaded into the final jar. There seem to be no compilation issues (syntax or maven), yet at runtime the missing class causes a NoClassDefFoundError to be thrown. Decompiling the jar shows that ConfigManager and Utils are shaded, but Logger is ignored.
Dependency: https://github.com/Benlewis9000/PluginToolsAPI
Project pom:
<?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.benlewis9000</groupId>
<artifactId>PluginToolsAPITestPlugin</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<repositories>
<!-- This adds the Spigot Maven repository to the build -->
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<!-- Access github repositories -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<!--This adds the Spigot API artifact to the build -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.13.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.Benlewis9000</groupId>
<artifactId>PluginToolsAPI</artifactId>
<version>82aaa66bbf</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>false</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Please let me know if any info is lacking,
Thank you
I didn't know about Jitpack, it does look rather useful!
I've used this config in the past for shade:
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
</filter>
</filters>
</configuration>

Maven can't find artifacts

After I'd searched the net and similar StackOverflow questions and tried to apply the proposed sollutions I didn't come up with an idea how to fix this error, which gives me pom.xml:
Multiple annotations found at this line:
- Missing artifact jakarta.json:jakarta.json-
api:jar:2.0.0-RC3
- Missing artifact org.glassfish.hk2:hk2-
locator:jar:3.0.0-M2
- Missing artifact org.glassfish:jakarta.json:jar:2.0.0-
RC3
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 //error appears here
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gson</groupId>
<artifactId>advanced-jaxrs-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>advanced-jaxrs-01 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-
media-moxy -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>3.0.0-M6</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -
->
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>3.0.0-M6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>advanced-jaxrs-01</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven
defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-
bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
I've had a look at .m2/repository and I've noticed, that there are files maven can't find. What should I do? What's the matter?
If there are files that maven can't find, you can always try to install the JAR file and going into
Project folder properties --> Java Build Path --> Add external JARs (Click on Modulepath if you cant immediately press Add External JARs)

Maven - adding a war dependency to another war

I'm developing a Jersey webapp that serves as a webhook for chatbots, currently I have a generic service that handles the basic connections and now I want to create a specific app webhook that will be able to use the generic service.
I've already added the generic webapp as a maven dependency and can use it in my workspace, but when deploying to tomcat I get error:
[org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError:
.../generic/services/Webhook] with root cause
java.lang.ClassNotFoundException: ...generic.services.Webhook
Generic services pom file:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</parent>
<artifactId>...-generic-services</artifactId>
<name>...</name>
<groupId>...</groupId>
<packaging>jar</packaging>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>...</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
</build>
App services pom file:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</parent>
<artifactId>...-app-services</artifactId>
<name>...</name>
<groupId>...</groupId>
<packaging>war</packaging>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>..generic.services</groupId>
<artifactId>...-generic-services</artifactId>
<version>...</version>
<classifier>classes</classifier>
</dependency>
</dependencies>
<build>
<finalName>...-app-services</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
So what I basically want to accomplish is to be able to call the generic services functions from the app service webapp, so for example if in generic service there is a function that receives and returns a json then I want to be able to make a function like this in app and call the generic service function with a json and receive from it.
You can't add a WAR as a Maven dependency. WAR has a specific layout which is understood by Servlet Containers and JEE Servers. This layout is not understood by other tools e.g. Maven which expects dependencies to be JARs.
This problem is normally solved by extracting either a new services-common Maven module which stores the shared code or moving the Java code outside of the WAR into a new JAR Maven module.
You can try using WAR Overlays to achieve what you want but it won't be as clean as extracting new JAR Modules.

JUnit test with Maven in a Jenkins plugin

I have a problem when I tried to run JUnit tests with Maven. For a Jenkins plug-in I wrote a class for test. For example: I have the class ConsoleParser in package com.jenkins_plugin in folder src/main/java. The JUnit test case is ConsoleParserTest in package com.jenkins_plugin in folder src/test. For running the JUnit test I added the dependency in the pom:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
And I ran in cmd the next command: mvn -Dtest=ConsoleParserTest test
The problem is that I've got the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
12:test (default-test) on project swatt_jenkins: No tests were executed! (Set -
DfailIfNoTests=false to ignore this error.) -> [Help 1]
The full pom.xml is:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<build>
<finalName>Jenkins_Plugin</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
<inherited>true</inherited>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.526</version>
</parent>
<groupId>Jenkins_Plugin</groupId>
<version>1.0</version>
<packaging>hpi</packaging>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
Also, I must mention that with those settings I ran successfully the test but after adding findbugs it crashed. So, I removed findbugs and try to run the test again but the problem that I mentioned appeared. So, can someone explain what I have to do? I tried to add another version of JUnit, Surefire but I had the same problem and I don't understand why.
Several things:
add you JUnit classes to src/test/java not src/test
for your JUnit class use the scope "test"
E.g.:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
Also I recommend to manage your repositories in a repository manager such as Nexus, not in your POM:
http://books.sonatype.com/nexus-book/reference/maven-sect-single-group.html

restlet: Using Server and Client restlet Jars in same web application

I am not able to use restlet server and client jars in same java web application. The problem is some jars of server and client have the same name. If I try to remove duplicate jars I get errors like
java.lang.NoSuchMethodError: org.restlet.Context.getClientDispatcher()Lorg/restlet/Restlet;
org.restlet.resource.ClientResource.createNext(ClientResource.java:503)
org.restlet.resource.ClientResource.getNext(ClientResource.java:829)
org.restlet.resource.ClientResource.handleOutbound(ClientResource.java:1221)
org.restlet.resource.ClientResource.handle(ClientResource.java:1068)
org.restlet.resource.ClientResource.handle(ClientResource.java:1044)
org.restlet.resource.ClientResource.post(ClientResource.java:1453)
com.xxxxxx.web.restletclient.services.CommonService.sendRequest(CommonService.java:25)
com.xxxxxx.web.restletclient.services.adminService.execute(adminService.java:24)
com.xxxxxx.web.restletclient.client.adminLoginClient.connect(AdminLoginClient.java:41)
com.xxxxxx.web.action.operator.adminLoginAction.performAction(adminLoginAction.java:75)
com.xxxxxx.common.action.AbstractBaseAction.execute(AbstractBaseAction.java:137)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
My scenario is such that my web application can work as a web service client as well as server. So I am looking for an option where I can use restlet client and server jars in same web application. I have searched over the net, but did not find any solution yet which will work.
Thank you for your help.
In fact, the core jar of Restlet supports both client and server sides. That said extensions (jar files with the name org.restlet.extension.XXX) can be specified to one or other side or both. It depends on the extension.
Could you give me the list of jars we tried to use?
Here is a sample pom file you could use to initialize the dependencies of your Restlet project:
<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>org.restlet</groupId>
<artifactId>restlet-war</artifactId>
<name>${project.artifactId}</name>
<packaging>war</packaging>
<version>1.0.0-snapshot</version>
<properties>
<java-version>1.7</java-version>
<restlet-version>2.3.1</restlet-version>
<wtp-version>2.0</wtp-version>
</properties>
<dependencies>
<!-- Restlet core -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet-version}</version>
</dependency>
<!-- To embed Restlet within a servlet container -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.servlet</artifactId>
<version>${restlet-version}</version>
</dependency>
<!-- To use HTTP Client to actual make HTTP
requests under the hood -->
<dependency>
<groupId>org.restlet.jee</groupId>
<artifactId>org.restlet.ext.httpclient</artifactId>
<version>${restlet-version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.com</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>sources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>${wtp-version}</wtpversion>
</configuration>
</plugin>
</plugins>
</build>
</project>
Hope it helps you,
Thierry

Categories

Resources