I have a custom maven plugin. In order to retrieve project's dependencies I use jcabi-aether library. It works fine for getting the project-scope dependencies. But what I need is to resolve plugin-scope dependencies so the call will look like:
<plugin>
<groupId>com.maven</groupId>
<artifactId>some-maven-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<configuration>
<some>${some}/path</some>
</configuration>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.1</version>
<classifier>sources</classifier>
</dependency>
</dependencies>
</plugin>
...
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aether</artifactId>
<version>0.10.1</version>
</dependency>
Does anybody has any idea?
Thank you
To retrieve plugin scope dependencies from the execute method of your custom Mojo, you need to loop over the elements of the build as following:
Build build = super.getProject().getBuild();
if (null != build) {
List<Plugin> plugins = build.getPlugins();
for (Plugin plugin : plugins) {
List<Dependency> dependencies = plugin.getDependencies();
// you can then use your custom code here or just collected them for later usage.
// An example of what you can get, below
for (Dependency dependency : dependencies) {
getLog().info(dependency.getGroupId());
getLog().info(dependency.getArtifactId());
getLog().info(dependency.getVersion());
getLog().info(dependency.getClassifier());
getLog().info(dependency.getScope());
// etc.
}
}
}
Once you have them, I believe you can then use the Aether API to get transitive dependencies as you already did for project dependencies.
Related
is it possible to inject dependencies in maven plugin and/or gradle plugin at runtime. I know you can add dependencies in plugin in pom.xml but i want those dependencies to be runtime as i want to be able to inject them something like this
mvn <plugin>:<goal> <arg=pass dependencies here)
This plugin is not in repo pom.xml so i want to run mvn cli to execute this plugin outside of the project. Has anyone done this
Add Plugin Dependencies Runtime
use tag for doing this. sample below here :
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
<scope>runtime</scope>
</dependency>
You could define an additional property like:
<properties>
<plugin.dependency.version>someDefaultVersion</plugin.dependency.version>
</properties>
and use it in the plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.verision}</version>
<dependencies>
<dependency>
<groupId>groupid</groupId>
<artifactId>artifactId</artifactId>
<version>${plugin.dependency.version}</version>
</dependency>
</dependencies>
</plugin>
By doing so you should be able to define plugin's dependency via command line using this command:
mvn org.springframework.boot:spring-boot-maven-plugin:yourGoal -Dplugin.dependency.version=runtimeDepenencyVersion
I've see you can add dependencies element of the plugin element in a pom.
Question: What's for? Should'nt all the lib used by a plugin be include inside it? Do it surcharge some lib used by the plugin?
<plugin>
<groupId>org.raml.plugins</groupId>
<artifactId>raml-jaxrs-maven-plugin</artifactId>
<version>1.3.4</version>
<dependencies>
<dependency>
<groupId>org.raml</groupId>
<artifactId>raml-parser-2</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
From official Maven POM Reference documentation:
dependencies: Dependencies are seen a lot within the POM, and are an element under all plugins element blocks. The dependencies have the same structure and function as under that base build. The major difference in this case is that instead of applying as dependencies of the project, they now apply as dependencies of the plugin that they are under. The power of this is to alter the dependency list of a plugin, perhaps by removing an unused runtime dependency via exclusions, or by altering the version of a required dependency.
That is, you can exclude some libraries from the plugin classpath or override certain versions, within the scope of that specific plugin.
Adding dependencies to a plugin would not alter the classpath of the application being built. The dependencies for a plugin is an entry point for further configurability, to directly change its classpath.
In most of the cases you would not need to work at that level of granularity, but indeed is quite useful in some cases and some plugin would actually need or recommend to add specific dependencies, for example plugins working on transformation or code generation (WSDL to Java, e.g.) would probably need a further dependency (you choose which one and which version) and so on.
A further official example is provided by the official Maven - Guide to configure plugins documentation:
You could configure the dependencies of the Build plugins, commonly to use a more recent dependency version.
For instance, the Maven Antrun Plugin version 1.2 uses Ant version 1.6.5, if you want to use the latest Ant version when running this plugin, you need to add <dependencies> element.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.2</version>
...
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-launcher</artifactId>
<version>1.7.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
...
</project>
Another example is provided by the official Exec Maven Plugin documentation in case you want to use its java goal to execute a Java program and you actually need to add libraries to its classpath but you don't want to alter the classpath of the application under build: this is a much cleaner and reasonable approach.
Dependency in plugin element allows you to define a specific version you will like the plugin to use.
Here is a good example on maven.apache.org
For instance, the Maven Antrun Plugin version 1.2 uses Ant version 1.6.5, if
you want to use the latest Ant version when running this plugin, you need to add <dependencies> element
I am trying to get used to Eclipse/Java but am more familiar with MS VisualStudio. Lets say I have Java Library (Project1) which has some dependencies on jar files via Properties->Java Build Path->Libraries (eg: AWS SDK, gson, swagger, etc). Now if I have Project2 and set a project dependency for Project2 to Project1 via Properties->Java Build Path->Project, I would hope that Project1 dependents would also be included for Project2. I dont see that happening or I am missing a step. I have been googling but I don't see any tutorial/documentation discussing 2 levels of dependents. I see that the Project1 jar is being referenced but what about the dependents for Project1? I am receiving an error such as:
The type XXXX cannot be resolved. It is indirectly referenced from
required .class files XXXX
I strongly suggest using Maven, which is a great and easy to use dependency manager.
Probably your eclipse already comes shipped with it, all you have to do is:
Do this for both projects:
Right click both projects, go to Configure -> Convert to Maven Project.
Create a group id,artirfact id and specify the version for your projects.
It will generate a pom.xml file in the root of your project.
Something like this:
<?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>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
You can add dependencies for your projects just by adding a dependency tag.
<dependency>
<groupId>yourGroup</groupId>
<artifactId>yourProject</artifactId>
<version>1.0.0</version>
</dependency>
After that just right click your projects go to
Run -> Run Configurations -> Maven Clean
Run -> Run Configurations -> Maven Install
and it will automatically download and install your dependencies for you.
You might want to have a look at Maven or a tool like this (Gradle, Ivy...) to handle your dependencies.
Relying on Eclipse for defining your build process (and dependencies) is a bad idea for long term projects.
This depends a little, on your project.
In case it is just a Java project, then it is better to use a build tool like Ant with Ivy, Maven or Gradle. As these contain the dependencies and other configuration details. Eclipse Mars (v4.5.1) comes with build in support for all these build tools.
In case it is an Eclipse Plug-in which you are developing, then you can configure it in Eclipse. And then store the configuration files, with the source code in the code repository.
I want to try this library in my android project. I am using Android Studio 0.4.6.
The README.markdown file tells me to insert this inside pom.xml:
<!-- in the 'repositories' section -->
<repository>
<id>keytwo.net</id>
<name>Keytwo.net Repository</name>
<url>http://audiobox.keytwo.net</url>
</repository>
<!-- in the 'dependencies' section -->
<dependency>
<groupId>io.socket</groupId>
<artifactId>socket.io-client</artifactId>
<version>0.2.1</version> <!-- the desidered version -->
</dependency>
The problem is that I do not have any pom.xml. I created one in my project root directory and synced gradle settings but it does nothing. Till now I only used already compiled .jar files or used the gradle compile function.
How can I use this library in my project?
Android Studio doesn't use Maven as its builder; it uses Gradle instead. Fortunately, Gradle can use Maven repositories to fetch dependencies, so it's a matter of taking that information that would go into the pom file and using it in Gradle format. These modifications go in the build.gradle file in your module's directory (not the build file in the project root directory).
First, set up the repository where it can find the dependency.
repositories {
maven { url 'http://audiobox.keytwo.net' }
}
and then add the dependency itself by adding this line to your dependencies block:
dependencies {
...
compile 'io.socket:socket.io-client:0.2.1'
}
Update:
From POM file:
compile '<groupId>:<artifactId>:<version>'
Syntax:
implementation 'groupId:artifactId:version'
If this is what you have to import in your Android Studio Project...
// Maven : Add these dependecies to your pom.xml (java6+)
// <dependency>
// <groupId>org.glassfish.jersey.core</groupId>
// <artifactId>jersey-client</artifactId>
// <version>2.8</version>
// </dependency>
// <dependency>
// <groupId>org.glassfish.jersey.media</groupId>
// <artifactId>jersey-media-json-jackson</artifactId>
// <version>2.8</version>
// </dependency>
then it translates to this...
implementation 'org.glassfish.jersey.core:jersey-client:2.8'
implementation 'org.glassfish.jersey.media:jersey-media-json-jackson:2.8'
I'm creating a custom plugin for maven. Normally, when you run a normal Java program that depends on some .jar files, you put it in the command line as a classpath. In my plugin, there are some things it needs to know about that are in other .jar files.
When compiling the plugin for maven, how do I add classpaths to my custom maven plugin? Would I add it as a dependency in the pom.xml?
You can add things on the classpath of a plugin via defining dependencies for a plugin which works like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.1</version>
<dependencies>
<dependency>
<groupId>checkstyle</groupId>
<artifactId>checkstyle</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
</plugin>
Apart from that it sounds strange that you need to define dependencies of a plugin during the runtime of the plugin.