UPDATE:
If I change ${env.JAVA_HOME} to a system property ${java.home} then it executes the maven-dependency-plugin unpack and copy tasks. But now it fails on the maven-antrun-plugin:1.8:run (build-content).
[ERROR] one of its dependencies could not be resolved: Could not find artifact ... at specified path ... -> [Help 1]
Why is it looking for my jar files in the JRE folder?
/{path to jdk}/Contents/Home/jre/lib/...
It's sitting right here:
/{path to jdk}/Contents/Home/lib
My pom.xml file is not reading my JAVA_HOME environment variable. I'm on a Mac. Here's the entry:
<dependencies>
<dependency>
<groupId>{groupId}</groupId>
<artifactId>{artifactId}</artifactId>
<version>${version}</version>
<scope>system</scope>
<systemPath>${env.JAVA_HOME}/lib/myPersonalJar.jar</systemPath>
</dependency>
</dependencies>
Here's the error:
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] 'build.plugins.plugin[org.apache.maven.plugins:maven-antrun-plugin].dependencies.dependency.systemPath' for ... must specify an absolute path but is ${env.JAVA_HOME}/lib/myPersonalJar.jar
In the iTerm app I can echo $JAVA_HOME and it points to the right place.
It works just fine when I hardcode the systemPath to my Java home. I can't figure out why. To give some context, my ant build files aren't reading my environment variables either.
Am I missing something stupid simple?
Try adding this profile to your pom.xml (in <profiles/>):
<profile>
<id>tools.jar</id>
<activation>
<file><exists>${java.home}/../lib/tools.jar</exists></file>
</activation>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jdk</groupId>
<artifactId>tools</artifactId>
<version>${java.specification.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>jdk</groupId>
<artifactId>tools</artifactId>
<version>${java.specification.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<docletPath>${tools.jar}</docletPath>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
I've included the corresponding change to the javadoc plug-in which may or
may not be useful to you. You can remove it if you don't need it.
Is it possible to retrieve the version of a specific maven dependency at runtime?
E.g. for the pom.xml:
<dependency>
<groupId>foo.bar</groupId>
<artifactId>foobar</artifactId>
<version>1.0</version>
</dependency>
I would like to retrieve the version 1.0 of a specific dependency with artifact ID foobar.
The most problematic part here is to find a JAR by its name. Unfortunately, there is no 100% reliable way for this in Java. To get a JAR by name, you need to scan classpath of the running application which may not always be present (e.g. because custom class loaders are used or module path is used instead of classpath).
But let's assume you are not using any fancy features like custom class loaders and you got your classpath which contains all your Maven dependencies. What do you need to do now? I'll try to describe a rough algorithm:
Retrieve paths of all JAR files from your classpath.
Scan each JAR file and find the file pom.properties. It's located in META-INF/maven/{groupId}/{artifactId}.
Find the value of the version property in pom.properties.
Again, this solution will not be completely reliable. You have to decide: do you really need the version information and for what purposes?
Are you looking at jars you created yourself? Or third-party libaries? I have published a lightweight solution for the former. So if the packaging of the jars you are looking for at runtime is in your own hands, go ahead and give it a try ;-). It may be a little more elegant than having to read XML or properties from jar files.
The idea
use a Java service loader approach to be able to add as many components/artifacts later, which can contribute their own versions at runtime. Create a very lightweight library with just a few lines of code to read, find, filter and sort all of the artifact versions on the classpath.
Create a maven source code generator plugin that generates the service implementation for each of the modules at compile time, package a very simple service in each of the jars.
The solution
Part one of the solution is the artifact-version-service library, which can be found on github and MavenCentral now. It covers the service definition and a few ways to get the artifact versions at runtime.
Part two is the artifact-version-maven-plugin, which can also be found on github and MavenCentral. It is used to have a hassle-free generator implementing the service definition for each of the artifacts.
Examples
Fetching all modules with coordinates
No more reading jar manifests, just a simple method call:
// iterate list of artifact dependencies
for (Artifact artifact : ArtifactVersionCollector.collectArtifacts()) {
// print simple artifact string example
System.out.println("artifact = " + artifact);
}
A sorted set of artifacts is returned. To modify the sorting order, provide a custom comparator:
new ArtifactVersionCollector(Comparator.comparing(Artifact::getVersion)).collect();
This way the list of artifacts is returned sorted by version numbers.
Find a specific artifact
ArtifactVersionCollector.findArtifact("foo.bar", "foobar");
Fetches the version details for a specific artifact.
Find artifacts with matching groupId(s)
Find all artifacts with groupId foo.bar (exact match):
ArtifactVersionCollector.findArtifactsByGroupId("foo.bar", true);
Find all artifacts where groupId starts with foo.bar:
ArtifactVersionCollector.findArtifactsByGroupId("foo.bar", false);
Sort result by version number:
new ArtifactVersionCollector(Comparator.comparing(Artifact::getVersion)).artifactsByGroupId("foo.", false);
Implement custom actions on list of artifacts
By supplying a lambda, the very first example could be implemented like this:
ArtifactVersionCollector.iterateArtifacts(a -> {
System.out.println(a);
return false;
});
Installation
Add these two tags to all pom.xml files, or maybe to a company master pom somewhere:
<build>
<plugins>
<plugin>
<groupId>de.westemeyer</groupId>
<artifactId>artifact-version-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>generate-service</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>de.westemeyer</groupId>
<artifactId>artifact-version-service</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
pom.xml
<?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>
<groupId>org.example</groupId>
<artifactId>WDProject</artifactId>
<version>1.0-SNAPSHOT</version>
<name>WDProject</name>
<url>http://www.nosite.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<selenium.version>3.141.59</selenium.version>
<webdrivermanager.version>3.7.1</webdrivermanager.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.3.9</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Although what you want is to search for a specific dependency, the below code retrieves all the details of the dependencies available in pom file.
You just need to create a wrapper method to retrieve details of specific dependencies.
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.FileReader;
import java.io.IOException;
class SampleMavenDependencyReader {
public static void main(String[] args) throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("/Users/nonadmin/bins/projects/IdeaProjects/WDProject/pom.xml"));
for (int i = 0; i < model.getDependencies().size(); i++) {
System.out.println(model.getDependencies().get(i));
}
}
}
OUTPUT
Dependency {groupId=junit, artifactId=junit, version=4.11, type=jar}
Dependency {groupId=io.github.bonigarcia, artifactId=webdrivermanager, version=${webdrivermanager.version}, type=jar}
Dependency {groupId=org.seleniumhq.selenium, artifactId=selenium-api, version=${selenium.version}, type=jar}
Dependency {groupId=org.seleniumhq.selenium, artifactId=selenium-server, version=${selenium.version}, type=jar}
Dependency {groupId=org.seleniumhq.selenium, artifactId=selenium-remote-driver, version=${selenium.version}, type=jar}
Dependency {groupId=org.seleniumhq.selenium, artifactId=selenium-java, version=${selenium.version}, type=jar}
Dependency {groupId=net.lingala.zip4j, artifactId=zip4j, version=2.2.4, type=jar}
Dependency {groupId=org.apache.maven, artifactId=maven-model, version=3.3.9, type=jar}~~~
Archive for required library: 'C:/Users/TOPS/.m2/repository/net/bytebuddy/byte-buddy/1.7.5/byte-buddy-1.7.5.jar' in project 'Maven1' cannot be read or is not a valid ZIP file
this is the error which is shown in Problem window in eclipse
Steps I have done to convert seleniumn project
Create a simple selenium webdriver project
convert it to maven project
remove all the selenium jar from the library options
In pom.xml add all the dependencies
Add the JRE and Maven from Add library options
Then it is showing me this error.
My pom.xml file is this
if there is anything missing then plz tell me
<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>
<groupId>Maven1</groupId>
<artifactId>Maven1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-booter</artifactId>
<version>2.20.1</version>
</dependency>
</dependencies>
</project>
About Byte Buddy
Byte Buddy is a Java library for creating Java classes at run time. This artifact is a build of Byte Buddy with a remaining dependency onto ASM. You should never depend on this module without repackaging Byte Buddy and ASM into your own namespace.
As you are seeing an error as Archive for required library: 'C:/Users/TOPS/.m2/repository/net/bytebuddy/byte-buddy/1.7.5/byte-buddy-1.7.5.jar' in project 'Maven1' cannot be read or is not a valid ZIP file I will suggest the following steps :
Remove all the dependencies and do maven clean and maven install
Add the required Selenium dependency only
Either add selenium-server or selenium-java as a dependency as per your exact requirement.
Use only the latest Selenium dependencies from Maven Artifact current one being <version>3.7.1</version>
Use only compatible junit dependencies
I can see you have used the surefire-booter dependency as follows (cross check if you need it):
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-booter</artifactId>
<version>2.20.1</version>
</dependency>
Finally, the maven-surefire-plugin is clearly missing. You need to add the following plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
I am trying to solve this for about a week now so I'll be very grateful for any help.
I am developing an Eclipse plugin. I need to read the pom.xml file in my code. To do this I need three maven dependencies.
The project was created as plugin project and then converted to Maven project using m2eclipse. This is my POM:
<properties>
<tycho-version>0.25.0</tycho-version>
<manifest-location>META-INF</manifest-location>
<name>${project.name}</name>
<version>${project.version}</version>
</properties>
<repositories>
<repository>
<id>eclipse-mars</id>
<layout>p2</layout>
<url>http://download.eclipse.org/releases/mars</url>
</repository>
</repositories>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.3.9</version>
</dependency>
</dependencies>
</project>
Now when I run new Eclipse app to test the plugin. It throws an exception: ava.lang.NoClassDefFoundError: org/apache/maven/model/io/xpp3/MavenXpp3Reader
Same goes with Apache HttpClient as a Maven dependency. For this I was able to solve it by importing bunch of org.apache.http packages.
I also tried this which didnt help.
This really bugs me because in all the articles and tutorials is written it should work.(that m2eclipse manages maven dependencies automatically).
I am not familiar with Maven, but as an Eclipse plug-in (osgi bundle) in order to access the class at run-time you will need to add a dependency to the package or bundle that provides those classes.
This is done in the plugin/META-INF/MANIFEST.MF file with an Import-Package or Require-Bundle manifest header. I don't know if maven/tycho does this automatically for you or not.
Also, the plug-in/bundle that provides those classes needs to specify an Export-Package in their manifest.
I am upgrading my system's Maven runtime from 3.0.5 to 3.1.1 and trying to build my project using mvn clean install like I normally would. Using the older Maven runtime, the build would always succeed. However, I am now always getting this error message during the build:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor (default-descriptor) on project XYZ: Execution default-descriptor of goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor failed: 48188 -> [Help 1]
I thought that perhaps it was due to my dependencies and plugins being outdated, so I ran mvn versions:use-latest-versions to update my pom.xml versions. That still did not fix this issue. Any ideas?
UPDATE
By popular demand, here is what my pom.xml file looks like. Note that all dependency and plugin versions were updated by mvn versions:use-latest-versions except for Sitebricks and qdox, due to breaking changes that I didn't want to integrate into my project.
<?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>
<groupId>com.my.company</groupId>
<artifactId>my-own-project</artifactId>
<version>1.0.1-SNAPSHOT</version>
<name>My Cool Maven Plugin</name>
<packaging>maven-plugin</packaging>
<distributionManagement>
<repository>
<id>someID</id>
<url>http://some.url.com</url>
</repository>
</distributionManagement>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>com.google.sitebricks</groupId>
<artifactId>sitebricks</artifactId>
<version>0.8.5</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<effort>Max</effort>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</reporting>
</project>
Upgrading and using below dependency with config resolved my issue:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<!-- see http://jira.codehaus.org/browse/MNG-5346 -->
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
</executions>
</plugin>
I had exactly the same problem and tried probably every hint provided in the possible duplicate Basic maven plugin project not working, Mojo plugin descriptors not generating, but nothing worked. Eventually in turned out that an old version 2.6.1 of the library com.ibm.icu:icu4j that was used by some other 3rd party module was responsible. I got rid of the icu4j dependency entirely and maven-plugin-plugin run with out problems thereafter.