How can I figure out where Maven got an artifact from? - maven

I just spent half an hour working with a colleague to figure out why I could build a project when he (working from the exact same source) could not. Common start to a story about Maven, I know. Anyway, it turns out that we had a dependency on something like org.apache.commons:commons-pool, while the repo we pointed to only supplied commons-pool:commons-pool. We both overlooked the difference for a long time until we finally caught on. I must have been able to "see" the former, while he obviously could not.
So my question is: how can I ask Maven "where are you getting X:Y from"?
Bonus questions: If it's only showing up in the local cache, is there any way to tell how it got there? Can I tell Maven "just this once, make sure everything you're using in the build is also available from my remote repo"?

Maybe you have repositories available on your settings.xml that your friend don't.
As Ed Staub said you can try to clean your local repository by executing:
mvn dependency:purge-local-repository
After that try to download all your project dependencies by running:
mvn dependency:go-offline
Now if you have a problem with the last goal saying it could not find for example the artifact org.apache.commons:commons-pool you can try to find its repository going to http://www.mvnbrowser.com and searching for this artifact, then on the results page you can select the tab Repositories to see where you can find this artifact (for example JBoss maven repo).
After getting the artifact group, id, version you can add the repository where it's located to your project by adding the current content to your pom.xml:
<repositories>
<repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
Then if you try to download the dependencies again it now should work.
mvn dependency:go-offline

Related

Maven repository that sometimes works

I hosted a java project on github.
The project is located here.
The project is really just a java library, and I wanted to create a maven repository for it. Because this is tricky, I followed a guide which explained me which project structure to follow. He applies a trick: create a 2nd branch named "repository". As you can see the jar files are in there.
That all went well, I made my first build, and then I created a java "Hello World" project to test if I could use this new maven dependency. So, I created a pom file with a dependency:
<dependency>
<groupId>brugo</groupId>
<artifactId>brugo</artifactId>
<version>0.1.0</version>
</dependency>
And to tell maven where to find this dependency, I added this one:
<repositories>
<repository>
<id>brugo</id>
<url>https://github.com/brugo/brugo/raw/repository/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
The good thing: sometimes it works.
The bad thing: sometimes it does not work.
And I am trying to find out what could be wrong. This is what IntelliJ looks like when things go wrong:
And I've seen this error message showing up:
[ERROR] Failed to execute goal on project xxx:
Could not resolve dependencies for project xxx:
Could not find artifact brugo:brugo:jar:0.1.0 in
brugo (https://github.com/brugo/brugo/raw/repository/) -> [Help 1]
I tried an mvn clean install -U several times, but without success.
I had the same problem on a different computer yesterday, then suddenly I got it to work without really knowing how.
Special thanks to #wargre for pointing me in this direction.
It's indeed easier (and more convenient) to set it up to be directly in the central maven repository.
http://central.sonatype.org/pages/ossrh-guide.html explains all the required steps. Here follows a summary.
First release:
Create a JIRA account and a project
Add plugins to the pom.xml file
Add more meta to the pom.xml
Download/install GPG and create keys
Create a settings.xml which contains your credentials.
Run mvn clean deploy
Verify the result in the staging repository
Release when happy, drop and retry while unsatisfied.
Post a comments on the JIRA ticket
Admire the result in the maven central repository.
Consecutive releases:
Update the version number in pom.xml
Run mvn clean deploy
Release (manual/automatic)

Maven: how to find parent pom?

I am in the process of learning maven and setting up a build environment. One thing I can't figure out, how to set up a project such that it finds a company-wide parent pom. I would like this to work:
$ git clone some_project
$ cd some_project
$ mvn install some_project
The some_project/pom.xml should reference a company-wide pom which it could get from a company maven repository. But where do I specify the company repository?
Putting it in some_project/pom.xml would probably do but then the location is hardcoded in many projects, which could lead to quite some trouble down the road should the server location change.
Having it in settings.xml could work I guess but would break the above requirement.
Edit
Thanks for the answers. I am going with the settings.xml solution although it won't allow the above sequence of commands. Seems like there is no solution that does not require some sort of initial manual setup and of the proposed solutions settings.xml is the simplest to me. Therefore I can't decide which of the two answers to accept. Both are equally good.
Here's the part from settings.xml I came up with:
...
<profiles>
<profile>
<activation>
<property>
<name>!skip</name>
</property>
</activation>
<repositories>
<repository>
<id>internalrepo</id>
<name>Central Internal Maven Repository</name>
<url>http://server.company.example.com/mvnrepo</url>
</repository>
</repositories>
</profile>
</profiles>
...
I'd recommend putting it in settings.xml.
If your company runs its own maven repo, it makes sense to have this configured in settings.xml - especially as you may need to add things like access credentials, which of course should never appear in a (shared) project pom. The only downside is that each user will have to jump through one additional (one-time) hoop when first installing their maven client.
An alternative is to not actually get the root pom from the company maven repo initially, but instead install it directly into your local repo from git. If your root pom is itself a maven project (which is not uncommon) and is available in git, simply clone it and run mvn install.
Putting the company repository URL in settings.xml is considered a good practice. You can also have associated username/password needed for repository upload.
If you care about repository relocation, you must rely on a good DNS choice (repository.mycompany.com is generaly fine) and a good usage of web servers rewrite rules.

Maven: unresolvable build extension

I've been looking at google and nothing really points to this problem. When I run "mvn clean install" it returns the following error.
[ERROR] Unresolveable build extension: Plugin
org.sonatype.flexmojos:flexmojos-maven-plugin:3.8 or one of its
dependencies could not be resolved: Failed to collect dependencies for
org.sonatype.flexmojos:flexmojos-maven-plugin:jar:3.8 ()
I'm trying to figure out how to import the maven plugin flexmojos but there are no clear directions on how to do this.
How would I import this plugin into my project?
The dependency you are looking for does exist.
To troubleshoot this problem further we'd need to see your POM and the rest of your build output.
Taking a stab in the dark:
Is this the first time you're running this build on this machine? If so, a very common "gotcha" is a corporate firewall preventing access to Maven Central. The solution in this case is setup a Maven repository manager like Nexus, or configure Maven to use a HTTP proxy.
I had a similar problem. I set up the proxy in Eclipse then I discovered I also set up the proxy in my settings.xml. I deleted the proxy from settings.xml and all worked out.
I hope my situation will help!
I solved this by adding another profile parameter from my pom.xml to my mvn command, e.g. "-Pprofile-name" pointing to a non-Maven repo definition embedded inside that profile, since Maven might be looking for repo definitions to be standing alone in a settings.xml, which isn't always the case.
I resolved this by adding a settings.xml file in the ~\.m2 directory, with the appropriate configuration pointing to our internal libraries.
If this Problem is displayed in the Eclipse Environment, this is because the m2e Connector tries to download the Plugins in to your ~\.m2\ repository
to solve this, open your Eclipse Settings: Window->Preferences and go to the ->Maven->User Settings Section.
Check if either Global Settings or User Settings is connected to the settings.xml File that your Maven uses.
Generally: Maven or your m2e connector tries to download these plugins via the plugin-repositories configured in your settings.xml it's can't find them because the repository is unknown or not reachable because you are behind a proxy or so:
08.11.18, 15:54:47 MEZ: [WARN] Failed to build parent project for com.xxx.xxx.xxx:eclipse-plugin:1.0.0-SNAPSHOT
08.11.18, 15:54:47 MEZ: [WARN] Failure to transfer org.apache.maven.plugins:maven-site-plugin/maven-metadata.xml from http://repository.sonatype.org/content/groups/sonatype-public-grid was cached in the local repository, resolution will not be reattempted until the update interval of tycho has elapsed or updates are forced. Original error: Could not transfer metadata org.apache.maven.plugins:maven-site-plugin/maven-metadata.xml from/to tycho (http://repository.sonatype.org/content/groups/sonatype-public-grid): repository.sonatype.org
go to your settings.xml file and add:
Mirrors (in this example: central repository. Do so for any other repos accordingly e.g. tycho):
<mirror>
<id>central</id>
<name>Our mirror for central repo</name>
<url>http://<your host to>/nexus/content/repositories/central/</url>
<mirrorOf>central</mirrorOf>
</mirror>
Repository:
<repository>
<id>central</id>
<url>http://<your host to>/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
</repository>
this should fix your problems:
Eclipse Specific: if Errors prevails (e.g. unknown packaging) add lifecycle mappings via Window->Preferences->Maven->Discovery->Open Catalog and add Tycho Connector
Further you can add the lifecycle-mapping plugin that handles these lifecycle mappings in the eclipse environment: eclipse m2e lifecycle mappings
Maybe you end up here because you are running nexus for a long time.
I finally found the error Summary tab in the Repositories page.
At some point Maven Central decided to require https: and the URL listed in my configuration still was using http:.
Update the URL to use https:, Save and everything worked smoothly again!

Maven: Manually install a Jar from local folder and then go online and grab & install all its dependencies

In my project's pom file I have a dependency that I can see in Maven central repository, javax.enterprise:cdi-api:jar:1.0-SP4 but for some reason I am getting on some of my computers the following error while building the project (hence the build fails):
[INFO] Unable to find resource 'javax.enterprise:cdi-api:jar:1.0-SP4' in repository central (http://repo1.maven.org/maven2)
Any idea why I get this error only on some of my computers (I can say all the computers are using the same network)?
Why do I get this error at all? As far as I can see the jar IS in the maven central repository.
Even though maven could not locate/download the jar on some of the computers, I was able to download it manually through the browser on these computers. How could it be?
Okay, so I've downloaded the jar manually through the browser at one of the problematic computers. I want to install it manually to the local repository on this computer. So from the command line I do:
mvn install:install-file -DgroupId=javax.enterprise -DartifactId=cdi-api -Dversion=1.0-SP4 -Dpackaging=jar -Dfile=path/to/file
...but none of its dependencies has been downloaded as well. Is there some maven plugin to install the file to the local repository AND download & install all its dependencies to the local repository?
If so, please show me how to use it.
Please check in the computers where the jar are not downloaded if the settings.xml file present in .m2 folder is having the url of the jar needed. If not add them.
You are not able to download because maven searches in the urls defined in the settings.xml file only. So if the url is not present there, even if the jar is in the central repository it wont be downloaded.
In your browser you can navigate to that link and download. Maven wont be able to do it for the reason given in the second point
Ok, looks like you need to mention your repository path in your pom file, What is the url of the repository? is it local to your network or some open url? for example: http://repo2.mvn.org/???
Sample example would be like this::
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
If you could paste the pom file, it would be easier to help you.
To answer your 3 point: Check the settings.xml file in .m2\settings.xml, make sure that same content is present. Sometimes settings.xml are modified directly in maven installation directory. so you better check in that location also.
To Answer your 4th point: mvn install:install installs (in simple terms copies) an artifact (jar/ear/zip/any artifact that meant for distribution) into maven local repository or into remote repository. So in your command, you just tried to install only cdi-api jar.
Whenever you run mvn compile, mvn package, mvn install, maven will scan your pom file and downloads all dependency mentioned into your local repository. I would suggest you to look your local repository and see how is it creating directory structure.

Jenkins builds not honoring third party repositories?

I'm hosting ojdbc14.jar on my network nexus instance, but only local builds seem to pass.
I have this in my pom:
<repositories>
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<id>thirdparty</id>
<url>http://server/nexus/content/repositories/thirdparty</url>
</repository>
</repositories>
After completing removing ~/.m2/, a local mvn package will find ojdbc14.jar on nexus. My jenkins builds, conversely, consistently fail on this:
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal
org.csodehaus.mojo:sql-maven-plugin:1.3:execute (sql-ddl) on project mongo-dbunit: Execution
sql-ddl of goal org.codehaus.mojo:sql-maven-plugin:1.3:execute failed: Plugin
org.codehaus.mojo:sql-maven-plugin:1.3 or one of its dependencies could not be resolved: Could not find artifact com.oracle:ojdbc14:jar:10.2.0.4.0 in central
Why?
(http://repo1.maven.org/maven2)
Before you do anything, install Config File Provider Plugin
Then,
Go to Jenkins root page
Choose 'Manage Jenkins'
Choose 'Configuration files'
Choose type 'Maven settings.xml file'
Name it 'custom-maven-settings' and cut and paste contents of your ~/.m2/settings.xml file
Now back to your Maven2/3 job.
In the Build section click Advanced button
In the Maven Settings Configs drop down box choose custom-maven-settings
This setup works with our custom repository.
You have a <repositories> section in your POM, but not a <pluginRepositories>. Maven used to be a lot more sloppy about the difference between compile-time artifact dependencies and plugin dependencies, but in newer versions of Maven they're completely separate.
As others have pointed out, it might be easier to troubleshoot this by running Maven from the command line, outside of Jenkins. Just doing a build on a different machine than the one you are typically developing on will usually bring a lot issues to light.
Here's some documentation on the POM that may or may not be helpful.
I'd check and make sure that the Maven settings are the same on both machines.
Jenkins by default uses the file in $HOME/.m2/settings.xml. It's also possible for a Jenkins job to override this default.

Resources