How to Create a Maven Project: Archetype, Directory Layout and IntelliJ IDEA - Quiz

Total: 5 questions

1. 

What are the ways to create a Maven project, and do you have to use an archetype?

Creating a Maven project means getting a directory with a pom.xml file (coordinates groupId, artifactId, version) and the standard folder structure src/main/java, src/test/java and so on. The ways to do it:

  • Command line - mvn archetype:generate from a template (an archetype). Requires installed Maven and a JDK; works everywhere, including on a server and in scripts, but the template may contain outdated settings, so check pom.xml.
  • IntelliJ IDEA - the New Project wizard: the Java generator with Build system = Maven, or the Maven Archetype generator. Maven is bundled with the IDE, and the project is imported right away.
  • Manually - write pom.xml and create the directories. Full control, nothing extra; this is how existing code is moved to Maven.

An archetype is not required: it is just a quick way to get a skeleton. What makes a directory a Maven project is pom.xml and the standard folder layout. The result is the same in every case - a project that builds with mvn package. Spring Boot projects are usually created with start.spring.io (Spring Initializr), which also produces a regular Maven project.

2. 

What is a Maven archetype, how does the mvn archetype:generate command work, and what should you check in the generated pom.xml?

A Maven archetype is a project template published in a repository as a regular artifact: a draft pom.xml, directories and sample classes. Official archetypes (group org.apache.maven.archetypes): maven-archetype-quickstart (jar, an App class and an AppTest test - the default choice), maven-archetype-webapp (war, src/main/webapp), maven-archetype-simple, maven-archetype-archetype (a skeleton for your own archetype).

mvn archetype:generate runs the generate goal of maven-archetype-plugin and creates the project in a subdirectory named after the artifactId. Modes: interactive (a list of archetypes and questions in the console) and batch - -DinteractiveMode=false, all parameters passed with -D (archetypeGroupId, archetypeArtifactId, archetypeVersion, groupId, artifactId, version, package). In batch mode groupId and artifactId are required, version defaults to 1.0-SNAPSHOT, and package equals groupId. Always set the archetype version explicitly. In PowerShell wrap every -D argument in quotes.

After generating, check pom.xml: the Java version (a single maven.compiler.release property instead of the source/target pair - quickstart up to and including 1.4 sets 1.7), the JUnit version (old templates use JUnit 4 or 3.8.1, version 1.5 uses JUnit 5) and the encoding project.build.sourceEncoding = UTF-8.

3. 

How do you choose groupId, artifactId and version for a new Maven project?

  • groupId - the organization or project group as a reversed domain name you own (or io.github.your-login); stick to lowercase Latin letters, digits and dots so that it is also a valid Java package name. Good: com.examclouds, org.example.shop; bad: MyCompany, com.my-company.
  • artifactId - the project name in lowercase, words separated by hyphens, no spaces and no version: hello-maven, online-shop, but not Hello Maven or shop-1.0.
  • version - a version number; the -SNAPSHOT suffix means a version in development (1.0-SNAPSHOT), no suffix means a released version (1.0.0).

By default the archetype derives the Java package name from groupId (classes end up in com/examclouds/). If groupId contains a hyphen or a segment that starts with a digit, the generated package will not compile - set a valid -Dpackage explicitly. The artifactId and version form the name of the built file: hello-maven-1.0-SNAPSHOT.jar.

4. 

What is the Maven Standard Directory Layout, and what goes where?

Maven knows the standard paths by default, so you do not declare them in pom.xml - you only need to put files in the right place:

  • src/main/java - source code in folders that mirror packages; compiled into target/classes and included in the jar/war.
  • src/main/resources - configuration and data (.properties, .xml, .yml, templates, SQL); copied to target/classes, i.e. the classpath root and the jar.
  • src/test/java - tests in the same packages as the code under test (so they can access package-private members); compiled into target/test-classes, not included in the jar.
  • src/test/resources - files needed only by tests; not included in the jar.
  • src/main/webapp - only for packaging = war (WEB-INF/web.xml, JSP, static files).
  • target - the build output, nothing is kept there by hand; deleted by mvn clean and added to .gitignore.

Maven does not search the whole project for files: a resource in src/main/java does not reach the jar, a .java file in src/main/resources is never compiled, and a src/java folder is not seen at all (No sources to compile). Override the paths in pom.xml only for an old project that cannot be reorganized.

5. 

How do you create and open a Maven project in IntelliJ IDEA, and what do you do after editing pom.xml?

New project: File → New → Project, the Java generator with Build system = Maven (if you leave IntelliJ, the project has no pom.xml) or the Maven Archetype generator; enter Name, Location, JDK and GroupId/ArtifactId under Advanced Settings. The IDE creates pom.xml and the standard directories and imports the project immediately; there is no need to install Maven separately - it is bundled.

The Maven tool window shows Lifecycle (phases run with a double-click), Plugins and Dependencies. The IDE does not re-read pom.xml on every keystroke: after editing it, click Load Maven Changes (or Reload All Maven Projects in the Maven window), otherwise a new dependency stays red in the code.

Importing an existing project: File → Open → pom.xml → Open as Project; if a pom.xml in a subfolder is not recognized, right-click it → Add as Maven Project.

Settings (Build, Execution, Deployment → Build Tools → Maven): Maven home path - Bundled or your own installation, User settings file and Local repository, Runner - the JDK that runs Maven. The bundled Maven is not available in the Terminal tab, and the IDE and the terminal may run Maven on different JDKs - if the project builds in the IDE but not in the terminal, compare the Runner JDK with the mvn -version output.

Page 1 of 1