Does maven automatically download artifact dependencies? - maven

I've been working with Maven for a little while now and I had a question about the information shown on the Maven Repository site. I was looking at the tags to paste into my pom for spring-web-mvc 3.2.8.RELEASE and noticed the table with the header "this artifact depends on" and saw the host of artifacts listed below.
My question is simple: Am I supposed to include the all of the dependencies listed in that table in my pom?

To answer your question, no you do not need to include all of the dependencies listed in the artifact dependencies section. It is my understanding that when you include a dependency in your pom file, maven will automatically download any needed jars. I am inferring this due to the fact that I personally don't add any of the artifact's dependencies other than what I need to my pom.
For example if I wanted spring-core I would do the following:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
And maven will automatically take care of the dependencies for me.
A good way to test this out is to open a new maven project in eclipse and specify a dependency such as this, update the project, and then check in the Maven dependencies folder.
For fun, I experimented with this and it is indeed true, Maven will download any necessary dependencies when you update your project. After putting only the above dependency in my pom.xml file I got the following:

No need to download all those.
Maven will take care of all the artifact's dependencies for the specified dependency mentioned in pom file.

Related

Dependency javax.mail:mail:1.4 not found

After I imported a maven project in IntelliJ IDEA, there are two errors in the pom.xml file:
"Failed to read artifact descriptor for javax.mail:mail:jar:1.4"
"Dependency javax.mail:mail:1.4 not found"
Does anyone know the reason behind these errors and how can I fix them?
Thank you and have a great day!
Due to license restrictions the older java mail classes are not in maven central or the java.net repository. usually companies host their own maven proxy and add these classes there.
Since around version 1.4.5 the dependencies are available in the java.net repository. Some later versions are also in maven central.
All other versions need to be downloaded from the oracle website and either added to a maven proxy or to your local maven repository - for example using the dependency plugin.
Change the pom.xml to include:
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
Or if you have a build.gradle file in the dependencies section add:
compile "com.sun.mail:jakarta.mail:1.6.3"

Maven looks for jar that isn't present in the repository

I have a repository set up on Artifactory for my Maven projects. I have declared this repo in the <repositories> tag of my settings.xml for one of the projects, I have created a dependency on some other prooject (Say projectA) in my POM for a snapshot version. For this projectA, there is only POM artifact on the Artifactory and not a jar. Still when I compile my POM, Maven tries to download the jar for this project and fails saying it couldn't find it.
I shall paste the exact POM, error, settings.xml and repo view soon. Any idea what could be wrong?
If you would like to have a dependency of pom type, you need to explicitly declare it. For example:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>my-artifact</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>
If type is not specified, Maven would use the default value which is jar.
Edit from OP: Just a caution, I use IntelliJ and even though I had the settings right in place, it was using some old (probably cached) settings.xml. I ran from commandline and it picked up the right one.

How to add dependencies to pom.xml in IntelliJ 14

I have a few dependencies in Project Structure/Libraries in IntelliJ 14. How can I add them to my maven pom.xml? There is one single tutorial on IntelliJ's website that does not work for me. I don't want to manage them manually.
The proper way to do this would be to install the dependency artifacts (most likely jars) into your local maven repo, like this.
How to install artifacts to your local maven repo
And then add the dependencies into your pom.xml
<dependencies>
<dependency>
<groupId>com.something</groupId>
<artifactId>artifact</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
Yes, this does require going through each artifact manually, one at a time, but it's a one time setup process.
That is the "proper" way. After that, you can do away with library dependencies in your project structure (they will be picked up correctly via maven).
There is the alternative possibility to "hack" in your project libraries path as a sort of "embedded" maven repo in your project, but that's a little bit hacky and I wouldn't advise that.

How to include jar in Maven Netbeans proj that doesnt exist in maven repo

I am using Netbeans to build a Maven project, and have the JTidy java library as a dependency. It turns out JTidy doesnt exist in any maven repos, so I can't just add a "normal" depedency entry for it.
What is the best way of handling dependencies to libraries in Maven projects that arent available on repos?
I've currently tried adding it to my maven pom as such (after copying the jar to my projects /libs folder)
<dependency>
<groupId>org.w3c</groupId>
<artifactId>org.w3c.tidy</artifactId>
<version>9.3.8</version>
<scope>system</scope>
<systemPath>${basedir}/libs/jtidy-r938.jar</systemPath>
</dependency>
However it complains that it will be unresolvable by dependent projects.
First of all, it's under another groupId, that's why you didn't find it.
<dependency>
<groupId>net.sf.jtidy</groupId>
<artifactId>jtidy</artifactId>
<version>r938</version>
</dependency>
Jtidy
But to answer your question, one way of doing this is to manually install it in your local repo as described here.
The best way IMHO is to add it to a proxy like Nexus. That way other people can access it from there without having to install it locally. However, this means you have to set up a repository manager, which doesn't make much sense if you are the only developer on the project.

about generate maven dependency

I am pretty new to maven.
Now I have a maven project developed. My another project needs to depend on this one.
Does anyone know how can I generate my own dependency? So that my second project can add the first one as a dependency in pom.
thank you very much
Since your first project is already a maven-project, just install it in your local repository by running mvn install in the first project's root directory.
Then you can include a dependency in your second project by simply referencing the groupId, artifactId and version you defined in the first project.
So if your first project had the following in its pom:
<project>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
... <!-- more here -->
you can include this in your second project:
<dependencies>
<dependency>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Unless you deploy your project 1 jar to a central maven repository, this will only work if your jar is in your local repository (via mvn install).
Maven projects are identified by the "Maven coordinates", that is, the ArtifactID, GroupID and version.
Say you create your first project and run maven install. Your local repository (in $HOME/.m2/) will now contain the compiled project plus whatever coordinates you put in there.
Your second project must now only depend on the said coordinates.
I would suggest googling a bit on maven. I made a tutorial a long time ago that might help you, even if the examples are a little simple. Here you go and good luck!

Resources