Build the project with Maven

As a highly automated build tool, maven itself provides the function of building projects. Let's experience the process of building projects using maven.
1. Build the Jave project
1.1. Create the Jave Project
1. Use the mvn archetype:generate command as follows:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode= false
2. Use the mvn archetype:create command as follows:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Use the "mvn archetype:generate" command You can create projects with both "mvn archetype:create" and "mvn archetype:create". There is no difference between the two. The only difference is that it takes a long time to create a project with the "mvn archetype:generate" command. Using the "mvn archetype:create" command can quickly create the project.
The process of creating a java project using the "mvn archetype:create" command is shown in the following figure:

BUILD SUCCESS means that the project is successfully built. When a Java Project called myapp is built under the former user directory (ie C:\Documents and Settings\Administrator).
The directory structure of the built Java project is as follows: As

you can see, the project created by Maven for us is a standard Maven project, but currently Maven only generates src/main/java (where the source code of the project is stored) and src/ test/java (stores the test source code) these two directories, but in actual project development, we generally have configuration files, such as log4j.properties, so we also need to manually create src/main/resources (store the ones used in project development Configuration files, such as storing log4j.properties, etc.) and src/test/resources (storing configuration files used in testing), as shown in the following figure:

Then we can import the created myapp project into Eclipse for development, As shown in the figure below:

1.2. Description of the pom.xml file of JavaProject JavaProject built
by Maven will have a pom.xml file in the root directory of the project, enter the myapp directory, you can see that there is a pom.xml file, this file is the core of Maven. As shown in the following figure:

1. pom means project object model.
2. pom.xml contains the information of project construction, including project information, project dependencies, etc.
3. The pom.xml file can be inherited. In large projects, the pom.xml of the submodule will generally inherit from the pom.xml of the parent module.
The content of the pom.xml file is as follows:

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3   <modelVersion>4.0.0</modelVersion>
4
5   <groupId>com.mycompany.app</groupId>
6   <artifactId>myapp</artifactId>
7   <version>1.0-SNAPSHOT</version>
8   <packaging>jar</packaging>
9
10   <name>myapp</name>
11   <url>http://maven.apache.org</url>
12
13   <properties>
14     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15   </properties>
16
17   <dependencies>
18 <dependency>
19 <groupId>junit</groupId>
20 <artifactId>junit</artifactId>
21 <version>3.8.1</version>
22 <scope>test</scope>
23 </dependency>
24 </ dependencies>
25 </project>

Node element description of pom.xml file:
<project> top-level node of pom file
<modelVersion> object model version, for Maven2 and Maven3, it can only be 4.0.0
<groupId> project creation organization The identifier, generally the reverse of the domain name
<artifactId> defines the unique identifier of the project under the identifier of the organization to which it belongs. There can be multiple projects under one organization.
<version> The version of the current project, SNAPSHOT, means the snapshot version, In
the way of <packaging> packaging in development , there are
<name> project name
<url> project address
<properties> attribute configuration, such as: <project. build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dependencies> Build the jars
that the project depends on. The groupId, artifactId and version uniquely determine a project coordinate
1.3. Use Maven to compile-test-package-install the project
1.3.1. Compile and
compile the source program, enter the command line, and switch to myapp directory, execute the command: mvn clean compile, as shown in the following figure:

the compilation is successful, there is an additional target directory under the myapp directory, and the compiled class file is stored in target\classes, as shown in the following figure:

1.3.2. Test
Enter the command line, switch to the myapp directory, execute the command: mvn clean test, as shown in the following figure: The

test is successful, there will be a test-classes directory in the myapp\target directory, which stores the class file of the test code, as shown in the figure below Display:

1.3.3. Packaging
Enter the command line, switch to the myapp directory, and execute the command: mvn clean package. Before executing the packaging command, the compile and test commands will be executed first, as shown in the following figure:


After the build is successful, it will be in the target directory. Generate the myapp-1.0-SNAPSHOT.jar package, as shown in the following figure:

1.3.4. Installation
Enter the command line, switch to the my-app directory, and execute the command: mvn clean install . Before executing the installation command, compile, test, Packaging command, as shown in the following figure:


If the build is successful, the jar package of the project will be installed in the local warehouse, as shown in the figure below:

1.3.5. Run the jar package
Enter the command line, switch to the myapp directory, and execute the command: java -cp target\myapp-1.0-SNAPSHOT.jar com.mycompany.app.App, as shown in the following figure:

2. Build JavaWeb Project
2.1, Create JavaWeb Project
1. Use mvn archetype:generate command as follows:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-WebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Use the "mvn archetype:generate" command to create a The process of the javaWeb project is shown in the following figure:

It takes a very long time to create a javaWeb project using the "mvn archetype:generate" command, it takes more than 40 seconds, sometimes even longer, I don't know why.
2. Use the mvn archetype:create command as follows:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=myWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Use the "mvn archetype:create" command to create The process of a javaWeb project is shown in the following figure:

Use "mvn archetype:create"
The directory structure of the

created JavaWeb project is as follows: There is currently only the src/main/resources directory in the created JavaWeb project, so you need to manually add src/main/java, src/test/java, and src/test/resources
as shown in the figure below Show:

Then we can import the created JavaWeb into Eclipse for development, as shown in the following figure:

2.2. Use Maven to package and publish the Web project
The JavaWeb project that Maven helped us create is an empty project with only one index.jsp page , we use Maven to package and publish the web project to run.
Switch to the myWebApp directory on the command line and execute: mvn package. After the build is successful, a target directory will be added to the myWebApp directory. This directory will be packaged into the myWebApp directory.war, and the war package will be copied to the Tomcat release directory. and it's ready to run. As shown in the following figure: The


packaging is successful, and a myWebApp.war file is generated in the myWebApp\target directory, as shown in the following figure:

Put myWebApp.war on the tomcat server to run, as shown in the following figure: The

running effect is as follows:

In addition to using Tomcat In addition to running the web project on the server, we can also integrate Jetty publishing and running in the web project. First, configure the Jetty plugin in the pom.xml file, as follows:

1 <project xmlns="http://maven.apache.org/POM/ 4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3   <modelVersion>4.0.0</modelVersion>
4   <groupId>com.mycompany.app</groupId>
5   <artifactId>myWebApp</artifactId>
6   <packaging>war</packaging>
7   <version>1.0-SNAPSHOT</version>
8   <name>myWebApp Maven Webapp</name>
9   <url>http://maven.apache.org</url>
10   <dependencies>
11     <dependency>
12       <groupId>junit</groupId>
13       <artifactId>junit</artifactId>
14       <version>3.8.1</version>
15       <scope>test</scope>
16     </dependency>
17   </dependencies>
18   <build>
19 <finalName>myWebApp</finalName>
20 <pluginManagement>
21 <!--Configure Jetty-->
22 <plugins>
23 <plugin>
24 <groupId>org.mortbay.jetty</groupId>  
25 <artifactId>maven- jetty-plugin</artifactId>
26 </plugin>
27 </plugins>
28 </pluginManagement>
29 </build>
30 </project>

Open the command line window, switch to the myWebApp directory, and execute: mvn jetty:run start Jetty server, as shown in the following figure:


Then you can access the application on port 8080. As shown in the following figure:

3. Command description of Maven to create a project
mvn archetype:create or mvn archetype:generate fixed writing
-DgroupId organization identification (package name)
-DartifactId project name
-DarchetypeArtifactId specify ArchetypeId, maven-archetype-quickstart, create a Java Project; maven-archetype-webapp, create a Web Project
-DinteractiveMode whether to use interactive mode
archetype is a built-in plug-in of mvn, create task can create a java project skeleton, DgroupId is the name of the package, DartifactId is the project name, DarchetypeArtifactId is the available mvn project skeleton, currently available skeletons are:
• maven-archetype-archetype
• maven-archetype-j2ee-simple
• maven-archetype-mojo
• maven-archetype -portlet
• maven-archetype-profiles (currently under development)
• maven-archetype-quickstart
• maven-archetype-simple (currently under development)
• maven-archetype-site
• maven-archetype-site-simple
• maven-archetype-webapp
Each skeleton will build a corresponding directory structure and some common files, the most commonly used are maven-archetype-quickstart and maven-archetype-webapp skeletons. The maven-archetype-quickstart skeleton is used to create a Java Project, and the maven-archetype-webapp skeleton is used to create a JavaWeb Project.
It has to be said that Maven is indeed a good project building tool. Mastering Maven is very helpful for project development.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326169355&siteId=291194637