Spring and maven in java


I'm starting to read about spring framework, where in the course they use maven and there are already things that I do not understand, which some do not have to do with spring or maven, but more with java.

Well, the first thing is, maven, by adding a dependency to the pom.xml file, I automatically download the library (the classes) to my project?

The second question comes with this image:

Why do I have so many folders that do not have any content when I do a project with Spring or Maven? (Ex: src-target)

asked by MatiEzelQ 03.04.2016 в 22:24

2 answers


The answer to the first question is yes, maven is responsible for downloading the libraries that you declare as dependency for your project only if these libraries are in any of the maven central repositories (see section Approved Repository Hosting) . But maven does a lot more than this. Maven also allows you to download the sources and documentation of those projects (if they are available). Also, Maven will also download the dependencies of the bookstores that you indicate as a dependency. For example, Spring Core 4.2.5 depends on the commons-codec version 1.10 library (as shown in your pom.xml file ), if Maven detects that you do not have that library installed locally, then it will download it for you.

In detail, when you declare a dependency on Maven, you will do the following:

  • Find if the library is in your local maven repository, usually located in <carpeta de usuario>/.m2 . In Windows it would be C:\Users\<tu usuario>\.m2 . On Mac it would be /Users/<tu usuario>/.m2 , and so on.
  • If it is not there, it will download the library from a central repository.

It should be noted that Maven will use the downloaded libraries to compile your project. This DOES NOT mean that your final artifact will contain the dependencies embedded in it. This depends on the type of project and some additional configurations that you can do in Maven. For example, if your project is a jar type, Maven by default will not add any dependency on the generated jar. However, if your project is a war type, Maven will by default add the dependencies within the WEB-INF / lib folder (Java web application standard).

The second question is answered and well detailed in Introduction to Standard Design of Maven Directories (link in English). Anyway, here I give a detail for the folders that you have in your project according to the image that you have shown:

  • src: Folder for all sources and resources used in the project
    • main: Sources and main resources of the project. By default, the elements located in this folder will be added to the generated artifact (jar, war, etc)
    • java: Project sources. By default, the compiled Java files (.java) located here are added to the generated artifact. Any other type of file (eg xml) will be ignored. If there is a compilation error, the artifact will not be generated.
    • resources: Project resources. Here reside the configuration files and other additional resources to be used in the project. For example: images (jgp, pgn), configuration files (properties, xml, json), templates (xlsx, docx, pdf), among others. By default, all files are stored directly in the generated artifact.
    • test: Sources and resources used during the testing phase of the project. By default, Maven will NOT add these files to the generated artifact.
    • java: Sources of project evidence. These sources will be compiled together with the sources located in src / main / java. These compilations are not added to the generated artifact. However, if there is a compilation error, the artifact will not be generated. To avoid this, you can indicate in your IDE that the testing phase is not used or from the command line use mvn -Dmaven.test.skip=true .
  • target: Contains all the elements generated after running Maven (mvn) on the project. This includes: files generated at compile time, compiled, final generated artifact (jar, war, etc)

Additionally, Maven by default will make your project use Java 5 (as shown in the image, your project uses Java 5 or J2SE 1.5). To avoid this and use your favorite version of Java (6, 7, 8, 9?) You must add a plugin under the build section in your pom.xml file:

    <!-- En mi caso, declaro utilizar Java 8 en el proyecto -->


Keep in mind that <build> and <dependencies> are two separate sections.

answered by 04.04.2016 / 02:31

First, if you add a dependency to the pom.xml file,   automatically download the library (classes) to my project?

Yes, it is exactly what you do.


with Spring or maven, I have so many folders that do not have any   content? (Ex: src-target)

The /target directory is where the files are created when you build!

Here is a list of directories created by Maven, where you specify what they are for (English):


answered by 03.04.2016 в 22:32