maven generates wrong download url for artifact - java

We have Nexus(https://maven.mycompany.com/repository/maven-public/) repository. We have deployed jar snapshot repo
<dependency>
<groupId>com.mycompany.my-proj</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
But another application that uses this dependency couldn't download artifact. I've tried download dependency with command line*(maven version 3.8.4)*
mvn org.apache.maven.plugins:maven-dependency-plugin:3.2.0:get -DrepoUrl=https://maven.mycompany.com/repository/maven-public/ -Dartifact=com.mycompany.my-proj:my-app:1.0-SNAPSHOT
-P my-repo
Suddenly I've noticed that maven builds the wrong download URL as below that "com." missed
Downloading from my-repo: https://maven.mycompany.com/repository/maven-public/mycompany/my-proj/my-app/maven-metadata.xml
Then I used quotation marks for -Dartifact="com.mycompany.my-proj:my-app:1.0-SNAPSHOT" after that worked and downloaded. I didn't understand what is the problem? This problem also exists in the building of the project.

Related

Maven - how to extract/download dependencies from a remote pom.xml

I am putting together a pom.xml for a java project. This java project has some java code that depends on a number of other libraries that exist in a private remote repository.
In my pom.xml:
<dependency>
<groupId>com.mycom</groupId>
<artifactId>my-id</artifactId>
<type>pom</type>
<version>9.1.00-SNAPSHOT</version>
</dependency>
Note that the artifact is a pom. This POM contains references to all the dependencies for the artifact ID my-id.
The issue I'm facing is this:
If I change the <type> to jar, it doesn't work because the actual file is not a single jar, it is a POM file that references all of the dependencies.
The remote POM is downloaded, but maven doesn't download and install all of the dependent .jar libraries referenced in the remote POM.
How can I achieve the end goal of having maven grab the latest POM file from the remote repo and subsequently download as dependencies all of the jars listed in this remote pom.xml? Is this even possible?

how to add jar dependency in maven?

When I run
mvn compile
I get package com.ibm.icu.util does not exist. So I downloaded the ICU4J jar and installed it into the local repository. I confirmed it's in .m2/repository/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar. Inside that jar file is the missing class file com/ibm/icu/util/Calendar.class. Then I added the following into the dependencies section of pom.xml:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.4.4</version>
</dependency>
But when I run mvn compile again, I get the same error. What am I doing wrong?
You should avoid adding dependencies manually.
If you don't know a groupId and artifactId of the dependency you need, search for it at http://mvnrepository.com/. Usually, groupId matches the package names in the jar file.
For your case, the dependency is already there: http://mvnrepository.com/search?q=com.ibm.icu
So, go to http://mvnrepository.com/artifact/com.ibm.icu/icu4j and get the version of the dependency you need, e.g. 55.1: http://mvnrepository.com/artifact/com.ibm.icu/icu4j/55.1
Grab maven dependency xml and put it to your pom.xml file:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>55.1</version>
</dependency>
If you didn't find your dependency try to find it in google. Sometimes the dependency may be found in some corporate public repositories, not in a central. In this case you need to add the third-party repository to repositories section of your pom.xml.
If you're unable to find your dependency in the public repository then you have three options:
A. Install jar to internal repository server (e.g. nexus)
B. Put the JAR file in your project sources and declare project maven repository :
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/my-repo</url>
</repository>
</repositories>
Important: You should keep the maven repository layout in your local repository.
C. [Bad Practice] Use maven install plugin to install your custom jar to local repository on your machine. But it's a badIt's not recommended.
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom
D. [Bad Practice] Use system dependency with absolute path to the JAR file, although it's a bad practice and should be avoided.
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${user.home}/jars/my.jar</systemPath>
</dependency>
You should not be manually installing things into the maven repository directory. That directory is maintained by maven itself.
The way dependencies work is that when you run mvn compile or some other goal, it will connect to the maven central repository and download the needed dependencies and their dependencies.
If you manually install a jar file, it may not have it's dependencies. That icu artifact will likely have other things it depends on. Maven will automatically resolve these dependencies.
I would recommend using mvn clean install this will clean the target directory and rebuild everything.
If it fails to download, then you likely need to change the maven configuration. For example, if you are behind a proxy server you need to configure maven with the proxy credentials.
Also, if you manually copied anything into the .m2/repository/ directory you should delete it and let maven put it in there correctly instead.
The beauty of maven is that you don't need to worry about things like downloading jars. It just handles that for you. Let it do it's job.
If you have an internal artifactory like JFrog maybe you should check that the jar is there. Do not download manually to .m2 because it's at least strange. At most you can upload the jar in that artifactory manually.

Maven dependency not getting corresponding POM file downloaded

I've got a maven project that has a dependency that it gets from a remote Nexus repository. I believe that the dependency was not built with maven, and just uploaded with a barebones POM file. The layout on the server looks fine though, so it was probably deployed with maven.
When maven downloads the dependency to my local repository, it downloads the jar file, but doesn't get the POM. At build time, there's a warning that the POM couldn't be found, and no dependency information available. I'm not actually using any of its code directly (it's actually a transitive dependency), so build completes successfully.
The real problem arises when I try to perform site generation for my project. The part that tries to generate the dependency graph report fails, because it can't find the POM for this dependency to work with.
I can't figure out why I'm not getting the POM downloaded, when the jar file for it gets downloaded just fine.
The POM file for that particular dependency looks like this (you can see why I don't think it's built with maven :))
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company.component</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0.1</version>
</project>
You'll notice that the root <project> element doesn't contain any namespace or schema information. Could this be related? Could it be Nexus not recognizing it as a POM? Apart from the possibility of some small syntactical character missing or mistaken, this is my current train of thought...please don't let it influence any ideas you may have! :)
Also, while troubleshooting, I've pasted the contents of the remote POM file into the correct file location in my local .m2 repo. Everything works fine when I do that. This isn't an acceptable fix though, because we will need the build to be done on our CI build servers.
Any help/suggestions greatly appreciated!
Edit:
I've managed to temporarily solve my actual problem, but the strangeness here still exists. I solved the problem by explicitly excluding the thing that depends on this from the dependency that's in my pom (the trouble dep is two steps away at least, and I'm not using anything that uses the thing that pulls it in):
<dependency>
<groupId>com.company.utility</groupId>
<artifactId>shared-utility</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>com.company.common.component</groupId>
<artifactId>thing-that-puls-in-bad-artifact</artifactId>
</exclusion>
</exclusions>
</dependency>
I've created a dummy project to prove it, with the following POM:
<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.company.project</groupId>
<artifactId>my-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.company.component</groupId>
<artifactId>bad-artifact</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</project>
Now in ~/.m2/repository/com/company/compnent/bad-artifact/1.0.1/, I've got:
_remote.repositories
bad-artifact-1.0.1.jar
bad-artifact-1.0.1.jar.sha1
bad-artifact-1.0.1.pom.lastUpdated
With no actual POM file.
If you look inside of _remote.repositories, then this file probably contains information which says "downloading the POM failed last time, don't try it again."
That's one of the things where Maven's policy "don't try to download releases again" gets in your way. Try to delete the folder ~/.m2/repository/com/company/compnent/bad-artifact/1.0.1/ and run Maven again to see the error.
It's quite possible that Maven refuses to use such a broken POM since the root element doesn't have the correct XML namespace. But it's hard to tell without seeing the actual error message.
A way to fix this is to download the JAR and to use mvn install:install-file from the command line to install the dependency locally. Even better, you can use mvn deploy:deploy-file to deploy it to your own Nexus server so all other developers now get a "good" version of the POM.
You should also get in contact with the people running the remote Nexus server so they can fix the issue.
Not related to your actual problem, but with maven (at least, recent version), generated jar contains their pom.xml in the META-INF/maven folder of that jar.
You should try to run maven with -e -X, and move your local repository to force Maven to download all, again.
mv "~/.m2/repository" "~/.m2/repository.old"
mvn -X -e dependency:tree
[edit] it was initially a comment, but it will be too long:
As far as I understand your problem, I think it is an error on Nexus, and not on your machine. Any valid solution would require you to mess with that your company Nexus. If you don't have permissions to do anything with your Company Nexus, you can test it with a local Nexus.
You can also enforce use of that Nexus in your ~/.m2/settings.xml like this:
<mirrors>
<mirror>
<id>nexus-local-central</id>
<mirrorOf>central</mirrorOf>
<url>http://localhost:8081/nexus/content/repositories/central</url>
</mirror>
<mirror>
<id>nexus-local-any</id>
<mirrorOf>external:*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>
You should not lose too much time as to why it fails, but focus on making it working.
For that, I think you should write a valid pom.xml for that artifact, and redeploy it on the server using the pomFile option:
mvn deploy:deploy-file -DpomFile=valid-pom.xml -Dfile=foobar.jar -Durl=http://nexus:8081 -DrepositoryId=company-nexus-deploy
Or if you are too lazy (or if this command fail), do it from the Nexus GUI!
PS: the Nexus default admin login/password are admin/admin123, and I think there was also deploy/deploy123 for deployment. Most Nexus that I've seen were not configured to use another login/password.

Can't run the google-api-java-client samples in eclipse

I'm trying to run the YouTube JSON-C Sample in eclipse. I have followed the instructions in the link and I managed to run it in the command line using mvn -q exec:java, but when I import the project to eclipse (I use eclipse indigo), it says that "The import com.google.api.client.googleapis cannot be resolved", and gives me compiler errors in every line that is related to the api. Is there some other configuration that needs to be done? Specifically adding google-api-java-client-1.5.0-beta jars to the build path?
Ok I finally solved the problem. What did the work for me was to execute mvn eclipse:eclipse on the project folder in terminal...
You need to check if you are pulling out all the dependencies in pom.xml from the repository:
<dependencies>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.5.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.5.0-beta</version>
</dependency>
</dependencies>
Check the settings.xml file in the Maven installation directory if it is pointing to a global repository or if you have hosted your own Nexus/Sonatype repository then make sure that your settings.xml points to that and also you must have the above two jars in your repository.
Or if you just want to get the project up and running, then instead of importing as a Maven project, just import it as a normal Java project and manually add the two libraries to the project's classpath. That should at the least get you started with the project without worrying about the Maven stuff.

Maven + Flexbuilder artifact source and javadoc attachment

I come to you today with an interesting challenge.
I have a maven+flexbuilder project that has a dependency on an external artifact.
This is specified with the following declaration:
<dependency>
<groupId>com.abc.commons</groupId>
<artifactId>commons-abc-client</artifactId>
<version>${commons.abc.version}</version>
<type>swc</type>
</dependency>
The commons-abc-client is deployed to an external releases repository, and is not available on the local environments for the AS developers.
The artifact is deployed to the remote repository using the followind command:
mvn clean source:jar javadoc:jar deploy
If I run
mvn eclipse:clean flexmojos:flexbuilder -DdownloadSources=true -DdownloadJavadocs=true
The dependency to commons-abc-client is properly generated (pointing to the downloaded-to-the-local-repo version) and it shows that the source is attached.
Trying to open any of the sources in the commons-abc-client throws an error. It seems as if it is looking the resources from inside the SWC and not in the attached jar.
Any ideas on work arounds?

Categories

Resources