官术网_书友最值得收藏!

Installing/configuring Maven

Maven is a tool which can be used for building and managing Java-based projects. The following are some of the key benefits of using Maven as a build tool:

  • It provides a simple project setup that follows best practices--it gets a new project or module started in seconds.
  • It allows a project to build using its Project Object Model (POM) and a set of plugins that are shared by all projects, providing a uniform build system. POM is defined in an XML file, namely, pom.xml, which defines the project configuration and dependencies along with their versions. One can manage the dependencies version from pom.xml.
  • Based on the project templates (also called archtype), Maven supports scaffolding, based on which the standard folder structure is created along with the base controller files, unit tests, and so on.
  • It allows usage of the large and growing repository of libraries and metadata to use out of the box. The project dependencies are installed in the local repository specified by path, such as ${user.home}/.m2/repository. Once installed, all the logged-in users can build from this common local repository. The settings related to the local repository can be found in the settings.xml file, which can be found in one of the folders such as ${maven.home}/conf  or ${user.home}/.m2. More details related to settings.xml can be found on this page: https://maven.apache.org/settings.html
  • Based on model-based builds, it provides the ability to work with multiple projects at the same time. Any number of projects can be built into predefined output types such as a Java archive (JAR), Web archive (WAR), Enterprise archive (WAR), or distribution based on metadata about the project, without the need to do any scripting in most cases.

One can download Maven from https://maven.apache.org/download.cgi. Before installing Maven, make sure that Java is installed and configured (JAVA_HOME) appropriately, as mentioned in the previous section. On Windows, you could check the same by typing the command echo %JAVA_HOME%:

  1. Extract the distribution archive in any directory. If it works on Windows, install an unzip tool such as WinRAR. Right-click on the ZIP file, and unzip it. A directory (with the name apache-maven-3.3.9, the version of Maven at the time of writing) holding files such as bin, conf, and so on will be created.
  2. Add the bin directory of the created directory, apache-maven-3.3.9, to the PATH environment variable. To do that, add the bin directory to the user PATH environment variable by opening up the system properties (Windows Key + Pause), select the Advanced tab and the Environment Variables button, and then add or select the PATH variable in the user variables with the value.
  3. Open a new shell, and type mvn -v. The result should print the Maven version along with details including the Java version, Java home, OS name, and so on.

Now, let’s look at how can we create a Java project using Maven from the command prompt before we get on to creating a Maven project in the Eclipse IDE. Use the following mvn command to create a Java project:

    mvn archetype:generate -DgroupId=com.healthapp 
-DartifactId=HealthApp
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Pay attention to some of the following in the preceding command:

  • archetype:generate: This is used to create a new project from an archetype. Archetype is a Maven project templating toolkit. Executing a command the mvn archetype:generate asks the developers a set of questions for setting up the project. In order to avoid these questions, Maven provides a flag, interactiveMode, which needs to be set to false.
  • groupId: This represents the name of the package.
  • artifactId: This represents the name of the project.
  • archetypeArtifactId: This represents the name of the template from which the project is scaffolded.
  • interactiveMode: This,when set to false, avoids the interactivity of the Maven Archetype plugin.

With the archetype:generate  and -DarchetypeArtifactId=maven-archetype-quickstart templates, the following project directory structure is created:

Figure 1.1: Maven project folder structure 

In the preceding diagram, the healthapp folders within the src/main and src/test folders consist of a Hello World program named as App.java, and a corresponding test program such as AppTest.java. Also,  a pom.xml file is created in the top-most folder.

In the next section, we will install the Eclipse IDE, and create a maven project using the functionality provided by the IDE.

主站蜘蛛池模板: 盐边县| 琼海市| 青龙| 光泽县| 湖州市| 玛多县| 河南省| 金坛市| 科技| 神农架林区| 响水县| 阳高县| 马山县| 勃利县| 城口县| 和政县| 建宁县| 巩留县| 讷河市| 周宁县| 贞丰县| 瓮安县| 裕民县| 镇雄县| 普格县| 新郑市| 遂川县| 鄱阳县| 宁阳县| 新蔡县| 宜春市| 肇州县| 彩票| 喀什市| 华池县| 花莲市| 潼南县| 普安县| 定远县| 固阳县| 岑溪市|