High level api to interact with maven form within the jvm

Overview

maven-utils

High level api to interact with maven from within the jvm

Use it by adding the dependency to your maven pom:

<dependency>
    <groupId>se.alipsa</groupId>
    <artifactId>maven-3.8.4-utils</artifactId>
    <!-- or if you prefer the maven 3.3.9 version:
    <artifactId>maven-3.3.9-utils</artifactId>
    -->
    <version>1.0.0</version>

</dependency>

For SNAPSHOT builds you need to have snapshots enabled for the sonatype snapshots repo. Production releases are available in maven central so no repository configuration is needed.

<repository>
    <id>snapshots-repo</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    <releases><enabled>false</enabled></releases>
    <snapshots><enabled>true</enabled></snapshots>
</repository>

Basic usage

Running a maven goal

import org.apache.maven.shared.invoker.InvocationResult;
import se.alipsa.maven.MavenUtils;

File pomFile = new File("pom.xml");
InvocationResult result = MavenUtils.runMaven(pomFile, new String[]{"clean", "install"}, null, null);

The arguments to runMaven(final File pomFile, String[] mvnArgs, @Nullable InvocationOutputHandler consoleOutputHandler, @Nullable InvocationOutputHandler warningOutputHandler) are as follows:

  • pomFile the pom.xml file to parse
  • mvnArgs the arguments (targets) to send to maven (e.g. clean install)
  • consoleOutputHandler where normal maven output will be sent, defaults to System.out if null
  • warningOutputHandler where maven warning outputs will be sent, defaults to System.err if null
  • InvocationResult the result of running the targets
  • MavenInvocationException if there is a problem with parsing or running maven

Note that maven need to be installed locally for the maven invoker which is used to run maven to work. MavenUtils will first look for the MAVEN_HOME system property, then for the MAVEN_HOME environment variable and if still not found will try to locate the mvn command in the PATH.

The static method locateMavenHome is used to find maven home.

import se.alipsa.maven.MavenUtils;

String mavenHome = MavenUtils.locateMavenHome();

Get the local repository

import se.alipsa.maven.MavenUtils;
import org.eclipse.aether.repository.LocalRepository;

LocalRepository localRepository = MavenUtils.getLocalRepository();

For the methods below, an instance of MavenUtils must be created. This allows you to pass in a list of RemoteRepositories used for the resolution. If you use the default constructor (as in the examples below) you get the Maven Central and BeDatadriven repositories.

Parse a pom file into a Model

import java.io.File;
import org.apache.maven.model.Model;
import se.alipsa.maven.MavenUtils;

File pomFile = new File("pom.xml");
MavenUtils mavenUtils = new MavenUtils();
Model model = mavenUtils.parsePom(pomFile);

Get a Classloader with all dependencies resolved

import java.io.File;
import se.alipsa.maven.MavenUtils;

File pomFile = new File("pom.xml");
MavenUtils mavenUtils = new MavenUtils();
ClassLoader loader = mavenUtils.getMavenDependenciesClassloader(pomFile, this.getClass().getClassLoader())

The method is defined as getMavenDependenciesClassloader(File pomFile, @Nullable ClassLoader possibleParent)

Resolve a pom file and get a Set of files for the dependencies (and their transients)

import java.util.Set;
import java.io.File;
import se.alipsa.maven.MavenUtils;

File pomFile = new File("pom.xml");
MavenUtils mavenUtils = new MavenUtils();
Set<File> dependencies = mavenUtils.resolveDependencies(pomFile);

Fetch (resolve) a single artifact

import se.alipsa.maven.MavenUtils;
import java.io.File;

MavenUtils mavenUtils = new MavenUtils();
File file = mavenUtils.resolveArtifact("org.slf4j", "slf4j-api", null, "jar", "1.7.32");

The method is defined as resolveArtifact(String groupId, String artifactId, String classifier, String extension, String version)

  • groupId is the same as the tag in the pom.xml
  • artifactId is the same as the tag in the pom.xml
  • classifier is typically null, javadoc, sources, dist etc
  • extension could be pom, jar, zip etc.
  • version is the same as the tag in the pom.xml

For a more elaborate explanation see the maven documentation

Logging

Maven-utils uses slf4j for logging so a slf4j implementation needs to be present for logging to work.

License and dependencies

The code in this repository is licenced under the MIT license. However, maven-utils depends on a number of other libraries to be able to do its thing. Below is a list of them with their respective license.

org.slf4j:slf4j-api

Used for logging. Licence: MIT

org.apache.maven.shared:maven-invoker

Used to run maven. Licence: Apache 2.0

org.apache.maven.shared:maven-shared-utils

Used to run maven and to parse the pom file. Licence: Apache 2.0

org.apache.maven:maven-core

Used to run maven and to parse the pom file. Licence: Apache 2.0

org.eclipse.aether:aether-connector-basic

Used to resolve dependencies. License: EPL 1.0

org.eclipse.aether:aether-transport-file

Used to resolve dependencies. License: EPL 1.0

org.eclipse.aether:aether-transport-http

Used to resolve dependencies. License: EPL 1.0

org.junit.jupiter:junit-jupiter

User for unit testing. Licence: EPL 2.0

org.slf4j:slf4j-simple

User for unit testing. Licence: MIT

Version history

1.0.2

  • add dependency on later version of httpclient since the one that comes with aether-transport-http has security issues.
  • improve download artifact test
  • add dependency on later version commons-io since the one that comes with maven-shared-utils has security issues

1.0.1, Jan 28, 2022

  • make locateMavenHome public.

1.0.0, Jan 27, 2022

  • Initial implementation, heavily based on similar functionality in the Ride project.
You might also like...

Spring Boot Rest API unit test with Junit 5, Mockito, Maven

Spring Boot Rest API unit testing with Junit 5, Mockito, Maven Apply Spring Boot @WebMvcTest for Rest Controller Unit Test with JUnit 5 and Mockito. F

Dec 22, 2022

Spring-boot project using open-api, docker, maven, REST

library-service spring-boot project using open-api, docker, maven, REST I used docker to run the project, as well as open-api to generate basic GET an

Nov 27, 2022

An API Library that provides the functionality to access, manage and store device topologies found in JSON files using Java and Maven Framework

An API Library that provides the functionality to access, manage and store device topologies found in JSON files using Java and Maven Framework

Topology API 📙 About An API library which provides the functionality to access, manage and store device topologies. 📝 Description Read a topology fr

Aug 4, 2022

This is the term project for level-1 term -2

This is the term project for level-1 term -2

Java-Fx-Term_Project This is the term project for level-1 term -2 . It's a server-client playerDatabase system for some clubs of EPL, although sign up

Apr 1, 2022

A cross-platform Mod for the popular Minecraft project, where the border always has a radius equal to the player's current experience level.

A cross-platform Mod for the popular Minecraft project, where the border always has a radius equal to the player's current experience level.

Level = Border A cross-platform Mod for the popular Minecraft project, where the border always has a radius equal to the player's current experience l

Nov 22, 2022

Reads data from sentences from hundreds of movie reviews, and evaluates the sentiment level of sentences entered by the user.

Sentiment_Analyzer Reads data from sentences from hundreds of movie reviews included in reviews.txt. Assigns a value to each word depending on the rev

Jun 13, 2022

Java related projects and also a begginer level projects

Java related projects and also a begginer level projects

Dec 15, 2022

The world's top-level live sdk...

目录结构说明 本目录包含 Android 版 移动直播 SDK 的Demo 源代码,主要演示接口如何调用以及最基本的功能。 ├─ MLVB-API-Example // MLVB API Example,包括直播推流,直播播放,互动直播 | ├─ App //

Jan 5, 2023

React Native TurboModule for reading battery level.

react-native-turbo-battery React Native TurboModule for getting battery level. Installation yarn add react-native-turbo-battery Usage import { getBatt

Aug 28, 2022
Comments
  • Cannot handle pom dependencies

    Cannot handle pom dependencies

    Pom type dependencies are not handled properly. A good example is ivy:

    <dependency>
        <groupId>org.apache.ivy</groupId>
        <artifactId>ivy</artifactId>
        <version>2.5.0</version>
        <type>pom</type>
    </dependency>
    
    opened by perNyfelt 0
Releases(v1.0.2)
Owner
Alipsa
Alipsa
Portaudio4j - An exploratory project to interact with the PortAudio C library using Java's FFI (Panama) APIs

Examples of PortAudio called by Java's Panama APIs JEP 412. At the moment this is purely exploratory (still kicking the tires) as it progresses in the

Carl Dea 4 Dec 29, 2021
SecureDB is an extension for Ai2 Appinventor and its distros which stores the data in the form of key and value just like TinyDB but in a more secure manner.

SecureDB SecureDB is an extension for Ai2 Appinventor and its distros which stores data for your app in a secure format locally on user's device. Expl

Akshat Developer 3 Sep 24, 2022
An intelliJ plugin providing a UI layer for git-flow, which in itself is a collection of Git extensions to provide high-level repository operations for Vincent Driessen's branching model.

Git Flow Integration Plus for Intellij An intelliJ plugin providing a UI layer for git-flow, which in itself is a collection of Git extensions to prov

RubinCarter 35 Nov 8, 2022
Team project within the course of Software System Design and Analysis.

InnoBookCrossing - Application for sharing books at Innopolis Description The application is designed to help people share books with each other. All

Dariya 33 Oct 22, 2022
The ByteSkript development kit, including resources for third-party libraries that are not contained within the standard runtime.

ByteSkript Development Kit Todo. The development kit is a set of advanced resources available for library creators. These are not available within the

null 1 Jan 8, 2022
An easy-to-use Spigot library that implements a GUI interface within offhand maps.

MiniMapGUI An easy-to-use Spigot library that implements a GUI interface within offhand maps. Maven/Gradle Dependencies Replace Tag with the latest ve

byteful 10 Oct 17, 2022
This project contains a full example of an application developed using Spring Boot and GraphQL within the Java.

Spring boot GraphQL Example This project contains a full example of an application developed using GraphQL within the Java. The project includes a com

Haoqiao Wang 3 Jul 20, 2022
Spring-Boot-Plus is a easy-to-use, high-speed, high-efficient,feature-rich, open source spring boot scaffolding

Everyone can develop projects independently, quickly and efficiently! What is spring-boot-plus? A easy-to-use, high-speed, high-efficient, feature-ric

geekidea 2.3k Dec 31, 2022
This is simple project to show how to create a basic API using Java 11 + Maven + Spring Boot + PostgrSQL + Flyway.

This is simple project to show how to create a basic API using Java 11 + Maven + Spring Boot + PostgrSQL + Flyway.

Sean Maxwell 11 Dec 10, 2022