Home » Android » java – Install jar with dependencies to maven repository (Android gcm-server push library)

java – Install jar with dependencies to maven repository (Android gcm-server push library)

Posted by: admin June 15, 2020 Leave a comment

Questions:

I work on an industrialized large maven project, and on an application module, I need to be able to send push notifications to Android (with new Google Cloud Messaging).

It seems google provide themselves the server-side library to send the notifications, and according to the mailing list they do not provide any maven repository.

The first step mentionned in the google documentation is:

Copy the gcm-server.jar file from the SDK's gcm-server/dist directory to your server classpath.

I’m not going to do that so i’ve added the jar to my local repository and will add it to our enterprise Nexus.

mvn install:install-file
 -Dfile=gcm-server.jar
 -Dsources=gcm-server-sources.jar
 -DgroupId=com.google.android.gcm
 -DartifactId=gcm-server
 -Dversion=r3
 -DgeneratePom=true
 -Dpackaging=jar

But i’ve just noticed that the library has dependencies (simple-json, mockito and junit).
It’s not precised if they are runtime or tests but i guess only simple-json is used at runtime.
I think retrieving the GCM dependency from our Nexus should also permit to retrieve the transitive dependencies like this simple-json lib right?

Thus what am i supposed to do? Should i create on my own a pom file for a project i don’t own, and then import the project to my maven repository with -DpomFile=my-custom-pom.xml?

Thanks

How to&Answers:

Finally i’ve ended creating my own pom.

I’ve created a maven repository on github here:
https://github.com/slorber/gcm-server-repository

Thus anyone can download this jar with maven and also its transitive dependencies.

I’ve added the files/commands so that you know how i’ve done.

Add repository:

<repository>
    <id>gcm-server-repository</id>
    <url>https://raw.githubusercontent.com/slorber/gcm-server-repository/master/releases/</url>
</repository>

And dependency:

<dependency>
    <groupId>com.google.android.gcm</groupId>
    <artifactId>gcm-server</artifactId>
    <version>1.0.2</version>
</dependency>

Answer:

See https://groups.google.com/forum/#!msg/android-gcm/oukjcHpbLj4/lql_IzpjmUkJ%5B1-25%5D.

It seems that google simply doesn’t publish this jar in any maven repository. You have two options:

  1. Include all (transitively) dependent jars in your repository
  2. (like you said) Create your own pom file

I would go for the second option. It’s not that hard.

Answer:

Works using:

 <dependency>
        <groupId>com.google.android.gcm</groupId>
        <artifactId>gcm-server</artifactId>
        <version>1.0.2</version>
 </dependency>

And

<repository>
        <id>gcm-server-repository</id>
        <url>https://github.com/slorber/gcm-server-repository/raw/master/releases/</url>
</repository>

Answer:

If you use Linux or Mac, you may use the following script saved as mvn-intall-jar:

#!/bin/sh
# Script to mavenize any JAR files
# by Muhammad Ichsan <[email protected]>
#

desc=$1
binary=$2
source=$3

if [ $# -lt 2 ]; then
    echo "Usage $0 <DESCRIPTION> <BINARY JAR FILE> [SOURCE JAR FILE]"
    echo "e.g. $0 com.google.android.gcm/gcm-server/2.1.4 gcm-server.jar gcm-server-src.jar"
    exit 1
fi

groupId=`echo $desc | awk -F '/' '{print $1}'`
artifactId=`echo $desc | awk -F '/' '{print $2}'`
version=`echo $desc | awk -F '/' '{print $3}'`

if [ "$version" = "" ]; then
    echo "Invalid DESCRIPTION"
    exit 1
fi

if [ "$source" != "" ]; then
    mvn install:install-file\
 -Dfile=$binary\
 -DgroupId=$groupId\
 -DartifactId=$artifactId\
 -Dversion=$version\
 -DgeneratePom=true\
 -Dpackaging=jar\
 -Dsources=$source
else
    mvn install:install-file\
 -Dfile=$binary\
 -DgroupId=$groupId\
 -DartifactId=$artifactId\
 -Dversion=$version\
 -DgeneratePom=true\
 -Dpackaging=jar
fi

And use this:

<dependency>
    <groupId>com.google.android.gcm</groupId>
    <artifactId>gcm-server</artifactId>
    <version>1.0.2</version>
</dependency>