Over a million developers have joined DZone.

How to Manage Maven Third Party Jars

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

When you find yourself the need to load third party jars into Maven repository, there are few steps you normally do to test it out. You first install them locally into $HOME/.m2/repository, and then create your project pom that list those dependency. When things look good, then you deploy into your own hosted repository. The following scripts will help you perform these tasks.

If you have lot’s of jars under a group, it’s more conveninent to create an extra pom that list these dependency and install/deploy it into the repository as well. And then your project would only need to include one dependency with <type>pom</type>.
#!/usr/bin/env bash
# Install local jar files into Maven repository. The artifact name would be same
# as the filename minus the extension.
# :Author: Zemian Deng
# :Date: 2013/06/17
# Usage:
#   # Print as maven dependency used in pom file
#   mvn-install.sh mygroup 1.0.0 lib/*.jar
#   # Install jar files into local maven repo
#   RUN_TYPE=install mvn-install.sh mygroup 1.0.0 lib/*.jar
#   # Deploy jar files into remote maven repo
#   export REPO_URL=http://localhost/nexus/content/repositories/thirdparty
#   RUN_TYPE=deploy mvn-install.sh mygroup 1.0.0 lib/*.jar

# Capture command arguments and options
if [[ "$GROUP" == "" || "$VERSION" == "" || "$FILES" == "" ]]; then
 printf "ERROR: invalid arguments: GROUP VERSION FILES...\n"
 exit 1

RUN_TYPE=${RUN_TYPE:="print"} # values: print|install|deploy
REPO_ID=${REPO_ID:="nexus-server"} # Id defined in user's settings.xml for authentication

# For each file, perform action based on run type.
for FILE in $FILES; do
 ARTIFACT=`basename $FILE '.jar'`
 if [[ "$RUN_TYPE" == "deploy" ]]; then
  printf "Deploying file=$FILE as artifact=$ARTIFACT to repo=$REPO_URL\n"
  mvn deploy:deploy-file \
   -DrepositoryId=$REPO_ID -Durl=$REPO_URL \
   -DgroupId=$GROUP -DartifactId=$ARTIFACT -Dversion=$VERSION -Dpackaging=jar \
 elif [[ "$RUN_TYPE" == "install" ]]; then
  printf "Installing file=$FILE as artifact=$ARTIFACT\n"
  mvn install:install-file \
   -DgroupId=$GROUP -DartifactId=$ARTIFACT -Dversion=$VERSION -Dpackaging=jar \
 elif [[ "$RUN_TYPE" == "print" ]]; then
  printf "        <dependency>\n"
  printf "            <groupId>$GROUP</groupId>\n"
  printf "            <artifactId>$ARTIFACT</artifactId>\n"
  printf "            <version>$VERSION</version>\n"
  printf "        </dependency>\n"

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}