Java runtime query of local maven repository - java

In my application, I want to get a 'view' of the local maven repository. The application is used to start another Java application (via ProcessBuilder), and allows users to add/replace libraries on the classpath. Part of this will provide a view of the artifacts in a user's local maven repository that could be used on the subsequent classpath.
Given an M2_HOME setting, I can get the settings.xml and from that the directory that the local maven repository is stored in (<localRepository>).
I could then do a file system query to get the jars, poms etc. that are in the user's local maven repository, determining the groupId, artifactId, version, packaging etc. from the file path and filename.
However, I am wondering whether there is scope to do this through the maven machinery - i.e. using maven libraries. If possible, that strikes me as being the correct / clever way of doing this.

Have a look at following maven dependencies,
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
</dependency>
Something on following line
RepositorySystem and LegacyRepositorySystem classes
RepositorySystem ArtifactRepository createLocalRepository( File localRepository ) method.
You might still need to inspect your local maven installation conf if local repository is set to other than ${user.dir}/.m2
I have just realized that it does not give a complete artifactory view. Could you check the following,
org.apache.maven.index.Scanner
org.apache.maven.index.DefaultScanner
This is probably used in eclipse Maven repositories view. See following,

Related

How do i update the maven local repository, i do not have the download rights

How do i update the maven local repository? if, i do not have the download rights.
I have the latest jar files with me. how do i replace the jar files in m2 folder and update the pom file?
I have tried this :
You can copy them in your local repository. For example, if you should add this dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
You should create folder in your maven local repository:
//com//fasterxml//jackson//core//jackson-databind//2.9.4 and put your jar in this folder.
Now getting below error -
failed to read artifact descriptor for org selenium selenium selenium-java:jar:3.13.0
You can copy them in your local repository.
For example, if you should add this dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
You should create folder in your maven local repository:
//com//fasterxml//jackson//core//jackson-databind//2.9.4 and put your jar in this folder.
You probably do not have the whole list of jars with all relevant parent poms, transitive dependencies etc. It is very hard to create a complete list manually. If any dependency of a dependency of a dependency is missing, your project won't compile.
First of all, I would advise you to try to get the "download rights" because using Maven with a bunch of manually deployed jars is very brittle and will take a huge amount of time to get right.
If you cannot get them, you either need to add all jars manually that Maven is missing (like the selenium jar mentioned in your question), or, if you are sure that certain artifacts are not necessary, you can exclude these dependencies in your pom.

Platform-Specific Maven Dependencies

Here is the Maven Repository that I'm trying to use. A snippet from my pom.xml:
<dependency>
<groupId>edu.wpi.cscore.java</groupId>
<artifactId>cscore</artifactId>
<version>1.0.2</version>
</dependency>
When run, it grabs the cscore-1.0.2.pom file just fine, but then it goes searching for cscore-1.0.2.jar which doesn't exist - if you look in the repo, each JAR is platform-specific, and is named accordingly (e.g. cscore-1.0.2-linux.jar).
I've tried:
<dependency>
<groupId>edu.wpi.cscore.java</groupId>
<artifactId>cscore</artifactId>
<version>1.0.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>edu.wpi.cscore.java</groupId>
<artifactId>cscore</artifactId>
<version>1.0.2-linux</version>
<type>jar</type>
</dependency>
It gets the cscore-1.0.2.pom fine but then it goes looking in the 1.0.2-linux/ directory for the JAR, which simply doesn't exist. Is there a way to tell Maven that the JAR file it is looking for will be named something else? Or is this simply not a valid Maven repository?
Thanks!
There should be one [and only one] artifact addressable via a coordinate [group-id, artifact-id, version].
These platform-specific artifacts were likely pushed into the repository manually and can't be referred to individually via the expected maven dependency POM element.
The easiest short-term solution would be to manually extract the dependency/dependencies and install each one with its own unique maven coordinates in your local repository. Then refer to that artifact using its local coordinates from the element in your project's POM.
Unfortunately that makes it impossible for maven to automatically detect and download a newer version of the artifact in the remote repo.
The long-term solution would be to petition the keepers of the external dependency's project, asking them to re-consider how they identify and install their platform-specific artifacts.

Maven dependency for javax.ejb.jar into .m2 directory

I cant find javax.ejb.jar in my .m2 dirctory, I need this jar for import javax.ejb.Schedule; , here is my pom file entry.
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
I am not sure if it will work or not, or its a right way to do things. Can some one please help to make a change in the POM file so that it downloads javax.ejb.jar into the .m2 directory.
Updated
by .m2 I mean in the repository directory in the correct folder
hierarchy (What ever it is).
Why? We have multiple sub projects (In eclipse workspace), In order to resolve dependency we use M2_REPO/path/to/the/required_library_file.jar, Now theses projects are part of code bases, Every developer download the source code, Maven download all jar to the repository directory(of the developer using any OS/Platform). This relative path from M2_REPO helps developer to have consitenat code (for eclipse project). Otherwise everyone will be adding their own path.
If it still doesn't make sense, here is what I want, Please give me an entry for POM file which download the javax.ejb.jar file into .m2 directory what ever the sub path is.
I have to include this jar in every project manually (And every developer needs to them as well from what ever directory have glassfish (C: , D:, E:, or /home/glassfish/modules/)
D:\servers\glassfish-3.1.2\glassfish3\glassfish\modules\javax.ejb.jar
where rest of the jars in each project are included as M2_REPO/path/to/jar which makes less no changes in the code base to commit.
M2_REPO/javax/ejb/ejb-api/3.0/ejb-api-3.0.jar
M2_REPO/javax/enterprise/cdi-api/1.0-SP1/cdi-api-1.0-SP1.jar
M2_REPO/javax/inject/javax.inject/1/javax.inject-1.jar
etc etc
I think I hear what you mean now :)
The maven dependency you specify
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
which you have in .m2/repository/javax/ueb/ejb-api/3.0/ejb-api-3.0.jar does not contain the class/interface javax.ejb.Schedule.
But you found the jar-file in your glassfish server, which does contain javax.ejb.Scheduleand its name is D:\servers\glassfish-3.1.2\glassfish3\glassfish\modules\javax.ejb.jar and now you ask how to get that into the pom?
Well, the Java EE APIs and their official jars in maven are somewhat a study in disharmony.
If you run a search on maven central you will find multiple jars containing exactly that class. You will probably note that all appserver vendors provide their own edition of every aspect of every api in every version.
You should be able to find a jar with the javax.ejb module from glassfish in version 3.1.2
http://search.maven.org/#artifactdetails|org.glassfish|javax.ejb|3.1.2|jar
in which case the dependency would be
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.ejb</artifactId>
<version>3.1.2</version>
<scope>provided</scope>
</dependency>
I found another artifactId here, though maven has your version too.
A (very) weird maven caching problem? Then it might work tomorrow.
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
</dependency>
Though this is a new version, for compilation it should do.
You may need to provide the repository location in your pom.xml file or in .m2/settings.xml file for the required jar to get downloaded into .m2 directory.
The dependency is declared as provided what means that the container will provide it.
What container are you using? I think Tomcat/Jetty won't provide that jar as it seems so Java EE. In that case just change the scope to compile.
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>compile</scope>
</dependency>
More info about dependency scopes:
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

POM.xml error fix

I am very new to eclipse and maven repository. I got a project to config the errors of POM.xml. I fixed many of it, but I am unable to fix some. Following are that errors.
<dependency>
<groupId>jlibs</groupId>
<artifactId>jlibs-jdbc</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.jgodies.form</groupId>
<artifactId>forms</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
<scope>runtime</scope>
In Maven repository folder there are not all the files which are in the other folders, so I put them manually, but still no results.
All the Oracle realted jars are not the part of the Central Maven Repository.
May be the case with the other oracle related jars. That is the reason why its not able to resolve the dependencies.
You should create a separate custom repository on your company server where you can put all these jar and configure that in POM.xml. Maven will first check the Central reposiroty and if it does not find the files will later check the custom repositry.
For locally running Maven you will have to do a MVN Install manually. you should never manuallly copy any jars to the m2 repositiry
Getting the JDBC drivers is a bit tricky. Please check this article for directions.
Due to Oracle license restriction, there is NO public Maven repository provides Oracle JDBC driver. To use Oracle jdbc drive with Maven, you have to install it manually into your Maven local repository.

have maven download a jar as a dependency

There is a website with a jar I want to use in my project. I could download it, and make a local dependency on it, but that is lame.
Other people will build this project. I don't want to clutter source control by checking it in. I want to comunicate where the resource can be downloaded from and to easily change the versions. At some point I would like to attach the sources and javadocs.
Is there a plugin that will let me use the URL and download it to my .m2 with a spoofed pom?
The way I do it, is to search for the jars in Maven's central repo:
http://search.maven.org/
This gives you enough data to build the dependency in your pom.xml file
Example:
If I wanted to add jSoup to my project as dependency, then I go and search in the central repo
and add the dependency to the pom file with the info that's in there:
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.jsoup%22%20AND%20a%3A%22jsoup%22
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>
I think maven2 repo is included by default when creating the super pom, so you don't have to write it, it will look there by default.
I think the best solution is to set up a Nexus or Artifactory repo for the team available over the network. Download the jar from the third-party location, and then upload it with the proper pom GAV values to your new local repo. Then add the URL of this repo to the repositories section of the pom. Everyone will get it when they sync up with version control.
maven-install-plugin can install files in local repo and generate poms, usage example:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc6_g.jar -DgeneratePom=true

Categories