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
Related
I'm writing a library that I'd like to compile into implementable jar which then will be used in other projects / tests.
In my library I depend on various jars: okHttp, guava, etc., What I want to do is to tell maven not to put those dependencies into the final JAR but make that projects / modules that depend on this library provide those dependencies
How can this be done in maven?
library pom.xml
<groupId>com.example</groupId>
<artifactId>testing-library</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
implementation module pom.xml
<groupId>com.example</groupId>
<artifactId>implementation-</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>testing-library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
But I'm getting java.lang.NoClassDefFoundError: com/google/common/base/Preconditions error
If you put code into src/test/java, this code will not be part of the final jar. The code is meant for tests during the build of the jar.
If your library is a helper library for tests, put the code into src/main/java and reference it in other projects with <scope>test</scope>.
BTW, don't use Maven shade plugin or Maven assembly plugin for a library. These are mainly meant for standalone jars that run on their own.
Ok, I solved the issue. It seems that the generated POM.xml for the testing-library did not contain any dependencies.
I was using mvn install:install-file ... -DgeneratePom=true for installing jar into local repository for quick debugging and the pom generated this way seemed to be lacking library dependencies'
Need to be pointed in the right direction on this perhaps, but if I add a "provided" dependency that is not included in the tomcat set of provided dependencies, running tomcat7:run from within eclipse fails with a classnotfoundexception on the class from the provided scope jar.
It needs to "provided" because it's a custom jar from a separate project that I've run mvn install on and for production am copying the jar to the $CATALINA_BASE/shared directory so that it's available (as a singleton) across applications/webapps.
<dependency>
<groupId>IndexFileAccessTracker</groupId>
<artifactId>IndexFileAccessTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
Only way I see (with my limited knowledge of Maven and the Tomcat7 plugin) is to change the scope to compile when running tomcat from the plugin in Eclipse and then change the scope back to provided when running the package goal.
Are there solutions to this? I tried adding the dependency to the the tomcat maven plugin (keeping the main maven dependency as provided but got the same class not found error:
<!-- For Maven Tomcat Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/CounterWebApp</path>
</configuration>
<dependencies>
<dependency>
<groupId>IndexFileAccessTracker</groupId>
<artifactId>IndexFileAccessTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
Again, it needs to be provided in the main Maven dependency because I don't want it included in the deployed WAR.
Resolved by using profiles, similar to https://stackoverflow.com/a/5951630
...
</dependencies>
<profiles>
<profile>
<id>runineclipse</id>
<dependencies>
<dependency>
<groupId>IndexFileAccessTracker</groupId>
<artifactId>IndexFileAccessTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
...
Then in my run/debug configuration just added runineclipse to the Profiles: box.
(On a side note, to do step through debugging I had to manually add the project to the Source tab.)
The build configuration was just the same package in the Goals: box; and I left the original dependency to have scope provided.
The tomcat7-maven-plugin and its run goal
Requires dependency resolution of artifacts in scope: test
Everythig that is on the compile classpath is also on the test classpath.
Thats why it is working with scope compile.
So the solution in your case would be to mark your dependency as test what even is (imo) semantically correct.
This will make the library available at local test-time, but not in the final artifact.
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.
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 am new to using Maven and Eclipse, I am following this link.
Below is the pom.xml I have in my workspace:
<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>
<groupId>com.HelloWorld</groupId>
<artifactId>SpringHelloWorld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringHelloWorld Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
<jdk.version>1.6</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>SpringHelloWorld</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
For adding the required jars, I run the below command from command prompt:
mvn eclipse:eclipse -Dwtpversion=2.0
I got the "BUILD SUCCESS" message in my console. But when I refresh the project in Eclipse IDE, I can see all the libraries are missing and the path for those jars does not exists.
e.g.
Description Resource Path Location Type
Project 'SpringHelloWorld' is missing required library: 'C:\Users\username\.m2\repository\org\springframework\spring-aop\3.0.5.RELEASE\spring-aop-3.0.5.RELEASE.jar' SpringHelloWorld Build path Build Path Problem
For this error, when I check manually, I can't see any folder named springframework inside the org folder, hence the jar is missing and the error is displaye din Eclipse IDE.
I tried running the command mvn eclipse:eclipse -Dwtpversion=2.0 multiple times but of no use.
Please advice how to resolve this.
EDIT
Below is the snapshot of the Eclipse Installations:
EDIT 2
I am not getting the Maven option when I Right Click on my project in Eclipse IDE. Hence when I create the java folder under the main folder, I am not able toUpdate Project Configuration` as mentioned here. Please advice
The Maven command mvn eclipse:eclipse generate the eclipse project files from POM.
The "-D" prefix in the argument means that it's a system property.
System property are defined like http://docs.oracle.com/javase/jndi/tutorial/beyond/env/source.html#SYS
mvn eclipse:eclipse -Dwtpversion=2.0 command convert the web based java project to maven based java project to support Eclipse IDE.
You can simple use given below command to clean and build your project:
mvn clean and mvn install
OR mvn clean install
Suggestion: Check maven is installed properly using command mvn --version.
Please try installing m2eclipse plugin (if you haven't done so yet) and nextly convert project to use maven behaviour through right clicking on project and choosing "convert to maven" option.
mvn eclipse:eclipse -Dwtpversion=2.0 -e clean install
run this instead. This builds your project by resolving dependencies