Where does maven dependency fetch packages from? - maven

As per this below snapshot, I see list of packages for hibernate:
I regularly see update index activity by m2e plugin(maven) in eclipse, for which I have no clue, What does it mean?
Where are these packages fetched from and displayed?
What is groupId/ArtifactId? Why can't one just say package/class instead?

Where are these packages fetched from and displayed?
By default, Maven will download from the Maven Central Repository, which is located at this URL: http://search.maven.org/
You can also add a custom repository by using the <repository> tag. Here is an example of how you can add the JBoss repository to your Maven project:
<project>
<repositories>
<repository>
<id>JBoss repository</id
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
Maven will download the artifacts when it needs them. So doing an mvn update or mvn install would trigger Maven to go to the repository if it doesn't already have the necessary JARs locally. And the local folder where the JAR files gets stored is C:\Users\your_windows_user\.m2\repository by default.
What is groupId/ArtifactId? Why can't one just say package/class instead?
Maven operates by managing dependencies, which are individual JAR files. So if you need to use a class, Maven will pull in the entire JAR file containing that class. The main reason for this is that Java libraries typically ship as JAR files, not individual classes.

Related

In a Springboot Maven project, how to reference from another Jar?

I have a SpringBoot Maven project. I am dependent on another set of libraries. Currently am pointing to their repository path , downloading it to .m2 repository and using.
But the repository website is not reliable. SO I wanted to package the dependent libraries as part of JAR in the resources folder.
After putting the jars in resource folder. How can I get references of the Types/libraries ?
Currently:
<dependencies>
<dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-core</artifactId>
<version>5.23.1</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>www.dcm4che.org</id>
<name>dcm4che Repository</name>
<url>https://www.dcm4che.org/maven2</url>
</repository>
</repositories>
You can put all your libraries in a single folder either in your project or in some folder in your local. You can then add them to your maven POM.
Lets say you put all your jars in a folder called libs in your base project directory. You can then add something similar to the below in your maven POM.
<groupId>com.abc.xyz</groupId>
<artifactId>my-artifact-id</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/libs/my-jar-name.jar</systemPath>
You need to use the scope system. Excerpt from Maven website,
Scope : system - This scope is similar to provided except that you
have to provide the JAR which contains it explicitly. The artifact is
always available and is not looked up in a repository.
Maven Documentation
If you read further, it also mentions that this has been deprecated. So, its a nice quickfix or a hack but then the best thing would be to set up a repository manager as suggested by others.

How to publish all jar for an existing project in a nexus/sonartype?

I have a maven project, and I have already all my dependencies in my local host. I want to push all these dependencies into nexus/sonartype.
I don't want to push my jar one by one by uploading via nexus interface.
So I copied the content of my maven repository ($MAVEN_HOME/repository) into {nexus-data}/storage/public
Nexus have to recalculate index after restarting?
You might want to use Distribution Management -
<distributionManagement>
<repository>
<id>some-artifactory</id>
<name>Artifactory Name</name>
<url>http://your.artifactory.address/releases</url>
</repository>
<snapshotRepository>
<id>other-artifactory</id>
<name>Other Artifactory Name</name>
<url>http://your.artifactory.address/snapshots</url>
</snapshotRepository>
</distributionManagement>
Repository
Where as the repositories element specifies in the POM the location
and manner in which Maven may download remote artifacts for use by the
current project, distributionManagement specifies where (and how) this
project will get to a remote repository when it is deployed. The
repository elements will be used for snapshot distribution if the
snapshotRepository is not defined.
So I copied the content of my maven repository ($MAVEN_HOME/repository) into {nexus-data}/storage/public
Don't do that. In Nexus 3, Nexus doesn't use raw files so relying on this approach isn't going to work for you anyway.
You should be using distribution management as described by #nullpointer. If you can't for some reason, you could write a script to use Nexus' REST API to upload many artifacts. That's more useful when you aren't building using Maven. For example, maybe you have a whole pile of legacy jar files to upload.

Can I resolve one Maven dependency from one repo and another dependency from second repo in same POM?

We have a project with multiple parent/child POMs. All the POMs are pointing to a single repository for resolving all Maven dependencies.
Now I have a need like: In a single POM, one jar has to be downloaded from repo1 and rest 4-5 jars from repo2.
How can you do that?
In a word - yes. Maven's dependency resolution mechanism is completely separate from the repository mechanism. Theoretically, you could have every single jar delivered from its own repository (however ridiculous it may to actually do this).
What I have understand the,
I can setup maven repo in artifactory/nexus
e.g. http://localhost:18081/artifactory/ --> L1
1. create remote(R1) repository in artifactory, which can point URL to outside repository, hosted by artifactory/nexus
e.g http://remotehost:18081/artifactory/remote-repo1
2. create a "virtual" repository(V1) in my artifactory and add remote(R1) in to this V1.
3. Let all my poms points to my local artifactory virtual repository(V1),
e.g.>http://localhost:18081/artifactory/virtual
that way, maven will look
a. local .m2 folder
b. then look for jars in virtual repo of my artifactory
hence virtual will look
b1. all local repo
b2. all remote cache repo
b3. all remote repo --> e.g.http://remotehost:18081/artifactory/remote-repo1
I am experimenting this,once succeed, i will update
EDIT :
This has worked for me, the only hiccup I faced was my ~/.m2/settings.xml
the snapshot was false, and my jar in remote repo is a snapshot jar.
After changing this value to true, now its fetching the jars :)
</profile>
<profile>
<id>virtual-repo</id>
<repositories>
<repository>
<id>central</id>
<url><repo_url></url>
<snapshots>
**<enabled>true</enabled>**
</snapshots>
</repository>
</repositories>

How to deploy maven project with parent packaging pom

I'm trying to deploy a maven web app with two modules and I tried using mvn package to get a war. Instead I got a pom.xml for the parent app and two jars for the submodules. That's...that's just great. How do I deploy that? I was expecting a war. Also, trying to deploy to jboss.
Edit: What goes in web.xml?
Make sure that your sub-modules declare <packaging>war</packaging>.
If you define not a packaging-type maven will choose jar as default.
If your parent-pom defines your modules it should be a POM as packaging (guess the parent-pom is in 99.9% cases POM as packaging-type). So as result for the parent-pom you get just a pom.xml.
For deploy you need to specifiy a <distributionManagent>.
<distributionManagement>
<repository>
<id>corp1</id>
<name>Corporate Repository</name>
<url>scp://repo/maven2</url>
</repository>
</distributionManagement>
For username and password i prefer to declare a <server> in the ~/.m2/settings.xml
so your password is not public for everyone.
I recommend to look into the maven-book from Sonatype.
If this don't help, please edit your post and insert your POMs.
Here is a good reference for building multi-module project from Maven into one war file.
The war file can be deployed via jboss-maven plugin.

Should I write repositories in my pom.xml?

I am new to Maven. If I start new project with Maven, should I know any repository URLs for it to work?
For example, this Hibernate tutorial http://docs.jboss.org/hibernate/core/3.3/reference/en/html/tutorial.html says about how to create a sample project with pom.xml text. But this pom.xml does not contain any repositories.
So, my m2eclipse plugin says, for example Project build error: 'dependencies.dependency.version' for org.hibernate:hibernate-core:jar is missing., for all dependency tag in pom.xml
Is this because of repositories absence?
Where to know repositories URLs? Is there one big repository? Why doesn't it included by default?
UPDATE 1
It is said here, that Maven should use "central" repository by default: http://maven.apache.org/guides/introduction/introduction-to-repositories.html
I have searched there for hibernate-code artifact and found it. So, this artifact IS in central repository. By my maven says dependency not found. Hence it doesn't use it's central repository. Why?
Apparently your Hibernate dependency is missing <version> tag:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.9.Final</version> <!-- this line is missing -->
</dependency>
Note that you don't have to specify version of dependencies previously declared in <dependencyManagement>.
Old answer:
Every build script (not only with Maven) should be reproducible and independent from environment. Standard pom.xml (called super pom), which every pom.xml inherits from, already defines main Maven central repository:
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
You don't have to define this repository, and you don't have to define any others if all your dependencies are there. On the other hand if you are using some external repositories, you must add them to pom.xml, so that every developer is always able to build.
The bottom line is: if you can build the project having a completely empty repository, your pom.xml is fine.
It's not advisable to define repositories in POM files as that causes a lot of issues (Maven will search those repositories for ANY artifact even the ones available at Central, poor portability, ...)
Best approach: Setup a repository manager (Artifactory, Nexus) and edit your settings.xml file to use the repo manager as a mirror.
Second best approach: Define the required repositories in your settings.xml file, not in your pom.xml files.
Repositories in poms is a bad idea.

Resources